From e25a75ab2785a65a0f0d0b5e713c612f06ac84f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Tue, 4 Apr 2023 22:34:41 +0200 Subject: [PATCH] Handle all bank loading gracefully. --- src/Makefile | 2 +- src/bank-fio.scm | 27 +++++++++++++++------------ src/csv-simple.scm | 11 +++++++++-- src/environment.scm | 6 +++--- src/members-payments.scm | 34 +++++++++++++++++++++++----------- 5 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/Makefile b/src/Makefile index 6939b15..e998474 100644 --- a/src/Makefile +++ b/src/Makefile @@ -213,7 +213,7 @@ bank-fio.import.scm: $(BANK-FIO-SOURCES) MEMBERS-PAYMENTS-SOURCES=members-payments.scm bank-account.import.scm \ dictionary.import.scm member-fees.import.scm \ - period.import.scm configuration.import.scm + period.import.scm configuration.import.scm utils.import.scm members-payments.o: members-payments.import.scm members-payments.import.scm: $(MEMBERS-PAYMENTS-SOURCES) diff --git a/src/bank-fio.scm b/src/bank-fio.scm index 35a6b28..1a09e46 100644 --- a/src/bank-fio.scm +++ b/src/bank-fio.scm @@ -61,17 +61,20 @@ ;; Loads Fio bank accound statement. (define (bank-fio-parse fn) - (with-progress% - #t fn - (let* ((csv (csv-parse fn)) - (head+body (csv-split-header csv)) - (head (car head+body)) - (body (cadr head+body)) - (numrow (assoc "accountId" head)) - (num (if numrow (cadr numrow) "ERROR")) - (bankrow (assoc "bankId" head)) - (bank (if bankrow (cadr bankrow) "ERROR"))) - (make-bank-account num bank - (map make-fio-transaction body))))) + (let ((csv (with-progress% #t fn (csv-parse fn)))) + (if csv + (let* ((head+body (csv-split-header csv)) + (head (car head+body)) + (body (cadr head+body)) + (numrow (assoc "accountId" head)) + (num (if numrow (cadr numrow) "ERROR")) + (bankrow (assoc "bankId" head)) + (bank (if bankrow (cadr bankrow) "ERROR"))) + (make-bank-account num bank + (map make-fio-transaction body))) + (let () + (print "Fio: cannot load account " fn) + #f + )))) ) diff --git a/src/csv-simple.scm b/src/csv-simple.scm index 0daa7f0..8d00f61 100644 --- a/src/csv-simple.scm +++ b/src/csv-simple.scm @@ -38,6 +38,7 @@ (chicken keyword) (chicken io) (chicken irregex) + (chicken condition) testing progress) @@ -103,8 +104,14 @@ ;; Loads given CSV file and parses its lines into lists (define (csv-parse fn . args) - (let ((lines (read-lines (open-input-file fn)))) - (apply csv-parse-lines lines args))) + (call/cc + (lambda (ret) + (with-exception-handler + (lambda (ex) + (ret #f)) + (lambda () + (let ((lines (read-lines (open-input-file fn)))) + (apply csv-parse-lines lines args))))))) ;; Splits CSV into header and body based on the first empty row. (define (csv-split-header csv) diff --git a/src/environment.scm b/src/environment.scm index 829f40f..0453ede 100644 --- a/src/environment.scm +++ b/src/environment.scm @@ -43,8 +43,8 @@ (let* ((edvar (get-environment-variable "EDITOR")) (editor (or edvar "editor")) (pid (process-run editor (list file-path)))) - (process-wait pid) - (clrscr) - (flush-output))) + (let-values (((a b c) (process-wait pid))) + (clrscr) + (flush-output)))) ) diff --git a/src/members-payments.scm b/src/members-payments.scm index ee9152e..b0859f3 100644 --- a/src/members-payments.scm +++ b/src/members-payments.scm @@ -42,6 +42,7 @@ (chicken sort) (chicken process-context) (chicken pathname) + (chicken condition) bank-account member-record members-base @@ -49,7 +50,8 @@ dictionary member-fees period - configuration) + configuration + utils) ;; Exchange rates (define exchange-rates-lookup-table @@ -103,9 +105,15 @@ ;; Reads the payments (define (load-accounts-list apikeys) - (map (compose car string-split) - (read-lines - (open-input-file apikeys)))) + (call/cc + (lambda (ret) + (with-exception-handler + (lambda (ex) + (ret #f)) + (lambda () + (map (compose car string-split) + (read-lines + (open-input-file apikeys)))))))) ;; Loads all accounts - it expects .csv files in given directory. (define (load-accounts accounts-list dir) @@ -117,13 +125,17 @@ ;; accounts and processes transactions. (define (members-payments-process mb apikeys-file dir) (if apikeys-file - (let* ((accounts (load-accounts - (load-accounts-list apikeys-file) - dir))) - (map member-sort-payments - (foldl members-payments-process-bank - mb - accounts))) + (let* ((acc-list (load-accounts-list apikeys-file)) + (accounts (if acc-list (load-accounts acc-list dir) #f))) + (if accounts + (map member-sort-payments + (foldl members-payments-process-bank + mb + (filter identity + accounts))) + (let () + (print "Warning: no accounts loaded!") + mb))) mb)) ;; Adds all balances - payments are converted to CZK in member-payments-total