From 43ff7feecb63037739306f1a80944a1abfb669ce Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sun, 31 Oct 2021 14:47:28 +0800 Subject: AdminLog keeps log and skips when an entry cannot be read --- jimbrella/admin_log.py | 61 +++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/jimbrella/admin_log.py b/jimbrella/admin_log.py index 8308336..8b5bb09 100644 --- a/jimbrella/admin_log.py +++ b/jimbrella/admin_log.py @@ -1,5 +1,6 @@ import csv import os +import logging from datetime import datetime from .lockfile import Lockfile from .utils import human_datetime @@ -50,33 +51,41 @@ class AdminLog: with open(self.path) as f: reader = csv.reader(f) logs = [] - for row in reader: - event = row[0] - entry = { - "event": event, - "date": datetime.fromisoformat(row[1]), - "note": row[-1], - } - info = {} - if event in ("TAKEAWAY", "GIVEBACK", "OVERDUE"): - info = { - "key": int(row[2]), - "name": row[3], - "id": row[4], - "phone": row[5], - "email": row[6], + for ln, row in enumerate(reader): + try: + event = row[0] + entry = { + "event": event, + "date": datetime.fromisoformat(row[1]), + "note": row[-1], } - elif event == "ADMIN_MODIFY_DB": - info = { - "admin_name": row[2], - "serial": int(row[3]), - "column": row[4], - "past_value": row[5], - "new_value": row[6], - } - - entry.update(info) - logs.append(entry) + info = {} + if event in ("TAKEAWAY", "GIVEBACK", "OVERDUE"): + info = { + "key": int(row[2]), + "name": row[3], + "id": row[4], + "phone": row[5], + "email": row[6], + } + elif event == "ADMIN_MODIFY_DB": + info = { + "admin_name": row[2], + "serial": int(row[3]), + "column": row[4], + "past_value": row[5], + "new_value": row[6], + } + else: + logging.warning( + "%s:%d is not a recognized event. Skip.", self.path, ln + ) + continue + + entry.update(info) + logs.append(entry) + except Exception: + logging.warning("%s:%d cannot be read. Skip.", self.path, ln) f.close() return logs -- cgit v1.2.3