summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-31 15:14:40 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-31 15:14:40 +0800
commit59c899e372a6d5e0309c585de43df015f775ebe7 (patch)
treef7628a1f8b55869487f19b5fdb2eefa13ffc2e3f
parent43ff7feecb63037739306f1a80944a1abfb669ce (diff)
CsvTable keeps log when a row cannot be read
Also, lockfile is unlocked even when app crashes.
-rw-r--r--jimbrella/csv_table.py26
1 files changed, 15 insertions, 11 deletions
diff --git a/jimbrella/csv_table.py b/jimbrella/csv_table.py
index 15263c6..8827e5f 100644
--- a/jimbrella/csv_table.py
+++ b/jimbrella/csv_table.py
@@ -1,5 +1,6 @@
import csv
import os
+import logging
from .lockfile import Lockfile
from .utils import identity
@@ -45,17 +46,22 @@ class CsvTable:
reader = csv.reader(f)
rows = []
# `rows` is a list of 2-tuples
- for row in reader:
+ for ln, row in enumerate(reader):
# for each tuple (k, v) in `rows`,
# it will be unzipped into a dict key-value pair
- rows.append(
- dict(
- [
- (sch["name"], sch["deserializer"](datum))
- for sch, datum in zip(self.schema, row)
- ]
+ try:
+ rows.append(
+ dict(
+ [
+ (sch["name"], sch["deserializer"](datum))
+ for sch, datum in zip(self.schema, row)
+ ]
+ )
)
- )
+ except Exception:
+ logging.warning("%s:%d cannot be read. Skip.", self.path, ln)
+ continue
+
f.close()
return rows
@@ -76,15 +82,13 @@ class CsvTable:
except Exception as e:
# failure occurred on write
# abort write, and write back contents as they were before
- # TODO: keep log
f.close()
f = open(self.path, "w")
f.write(backup)
raise e
finally:
f.close()
-
- self.lockfile.unlock()
+ self.lockfile.unlock()
def _append(self, row) -> list:
"""Append one row, and return the entire updated table."""