From 59c899e372a6d5e0309c585de43df015f775ebe7 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sun, 31 Oct 2021 15:14:40 +0800 Subject: CsvTable keeps log when a row cannot be read Also, lockfile is unlocked even when app crashes. --- jimbrella/csv_table.py | 26 +++++++++++++++----------- 1 file 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.""" -- cgit v1.2.3