From 2aec216e663bf6a106fe1d19187060e9ad75bf42 Mon Sep 17 00:00:00 2001 From: Shane Frischkorn Date: Thu, 13 Jan 2022 14:14:25 +1000 Subject: [PATCH] Pull the account name from the db so it can process multiple accounts. Change print statements to proper logging statements --- .vscode/launch.json | 9 ++++++++- converter.py | 33 +++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 2b2502c..61a0e17 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,14 @@ "request": "launch", "program": "${file}", "console": "integratedTerminal", - "justMyCode": false + "justMyCode": false, + "env": { + "DB_HOST": "192.168.0.2", + "DB_PORT": "27017", + "DB_NAME": "hsbc_converter", + "DB_COL": "imported_transactions", + "WATCH_DIR": "/mnt/d/tmp/hsbc/" + } } ] } \ No newline at end of file diff --git a/converter.py b/converter.py index f74536c..c41487c 100644 --- a/converter.py +++ b/converter.py @@ -9,7 +9,7 @@ from pathlib import Path from decimal import Decimal from bson.decimal128 import Decimal128 - +import logging import os import pymongo import time @@ -30,12 +30,17 @@ CONVERTED_DIR = 'Converted' MONGO_URL = os.environ['DB_HOST'] MONGO_PORT = os.environ['DB_PORT'] MONGO_DB = os.environ['DB_NAME'] -MONGO_COL = os.environ['DB_COL'] +MONGO_COL = 'hsbc_converter' +ACCOUNT_COL = 'accounts' MONGO_URL = "mongodb://{}:{}".format(MONGO_URL, MONGO_PORT) myclient = pymongo.MongoClient(MONGO_URL) mydb = myclient[MONGO_DB] mongo_col = mydb[MONGO_COL] +account_col = mydb[ACCOUNT_COL] + +logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO) +logging.basicConfig(format='ERROR: %(asctime)s - %(message)s', level=logging.ERROR) class Handler(watchdog.events.PatternMatchingEventHandler): def __init__(self): @@ -45,10 +50,10 @@ class Handler(watchdog.events.PatternMatchingEventHandler): @staticmethod def write_csv(statement, out_file): - print("Writing: " + out_file) + logging.info("Writing: " + out_file) if len(statement) == 0: - print("No transactions to write.") + logging.info("No transactions to write.") return fields = ['date', 'memo', 'category', 'amount', 'name'] @@ -85,6 +90,10 @@ class Handler(watchdog.events.PatternMatchingEventHandler): @staticmethod def get_statement_from_qfx(qfx): + account = account_col.find_one({"number": qfx.account.number}) + if account is None: + logging.error("No account for account number {} exists. Create one and re-process the file".format(qfx.account.number)) + statement = [] for transaction in qfx.account.statement.transactions: @@ -96,7 +105,7 @@ class Handler(watchdog.events.PatternMatchingEventHandler): 'memo' : transaction.memo, 'category': 'Uncategorised', 'amount': transaction.amount, - 'name': 'HSBC Everyday Global' + 'name': account['name'] } #mongo needs the decimal values in Decimal128, so create a version for it @@ -107,7 +116,7 @@ class Handler(watchdog.events.PatternMatchingEventHandler): statement.append(line) result = mongo_col.insert_one(line_d128) - print("New db entry stored: {}".format(result.inserted_id)) + logging.info("New db entry stored: {}".format(result.inserted_id)) return statement @@ -121,7 +130,7 @@ class Handler(watchdog.events.PatternMatchingEventHandler): return path def on_created(self, event): - print('File found: {}'.format(event.src_path)) + logging.info('File found: {}'.format(event.src_path)) fileExists = False timeout = 0 @@ -131,15 +140,15 @@ class Handler(watchdog.events.PatternMatchingEventHandler): timeout += 1 if timeout > 10: - print('Timeout waiting for file {} to exist. Aborting processing.'.format(event.src_path)) + logging.error('Timeout waiting for file {} to exist. Aborting processing.'.format(event.src_path)) return historicalSize = -1 while (historicalSize != os.path.getsize(event.src_path)): historicalSize = os.path.getsize(event.src_path) - print('waiting for copy to finish....') + logging.info('waiting for copy to finish....') time.sleep(1) - print("file copy has now finished") + logging.info("file copy has now finished") with open(event.src_path, 'r') as file: @@ -167,7 +176,7 @@ class Handler(watchdog.events.PatternMatchingEventHandler): if not destination.exists(): path.replace(destination) - print("Processing successfully finished for {}".format(event.src_path)) + logging.info("Processing successfully finished for {}".format(event.src_path)) if __name__ == "__main__": event_handler = Handler() @@ -175,7 +184,7 @@ if __name__ == "__main__": observer.schedule(event_handler, path=WATCH_DIR, recursive=False) observer.start() - print('Converter running, waiting for files in {}'.format(WATCH_DIR)) + logging.info('Converter running, waiting for files in {}'.format(WATCH_DIR)) try: while True: time.sleep(1)