diff --git a/.gitignore b/.gitignore index f8b73e7..8b94239 100644 --- a/.gitignore +++ b/.gitignore @@ -138,3 +138,4 @@ dmypy.json # Cython debug symbols cython_debug/ +.devcontainer/devcontainer.json diff --git a/Dockerfile b/Dockerfile index 05a092b..87c65e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3 +FROM python:3.8 ADD converter.py /app/ diff --git a/converter.py b/converter.py index 8b2abe9..887517f 100644 --- a/converter.py +++ b/converter.py @@ -12,10 +12,10 @@ import watchdog.events import watchdog.observers DATE_FORMAT = "%d/%m/%Y" -#WATCH_DIR = os.path.dirname(os.path.realpath(__file__)) WATCH_DIR = '/mnt/data/' PATTERN = '*.qfx' BACKUP_DIR = 'Imported' +CONVERTED_DIR = 'Converted' class Handler(watchdog.events.PatternMatchingEventHandler): @@ -69,25 +69,40 @@ class Handler(watchdog.events.PatternMatchingEventHandler): return path def on_created(self, event): - qfx = OfxParser.parse(open(event.src_path, encoding="latin-1"), fail_fast=False) - statement = Handler.get_statement_from_qfx(qfx) + print('File found: {}'.format(event.src_path)) + + historicalSize = -1 + while (historicalSize != os.path.getsize(event.src_path)): + historicalSize = os.path.getsize(event.src_path) + print('waiting....') + time.sleep(1) + print("file copy has now finished") + + with open(event.src_path, 'r') as file: + + qfx = OfxParser.parse(file, fail_fast=False) + statement = Handler.get_statement_from_qfx(qfx) - path = Path(event.src_path) - path.resolve() + path = Path(event.src_path) + path.resolve() - out_file = str(path.parent / ('converted' + path.stem + '.csv')) - Handler.write_csv(statement, out_file) + converted_dir = path.parent / CONVERTED_DIR + if not converted_dir.exists(): + converted_dir.mkdir() - #Now move the input file to backup - archive_file_dir = path.parent / BACKUP_DIR - archive_file = (path.stem + '{:04d}' + path.suffix) - destination = Handler.unique_path(archive_file_dir, archive_file) + out_file = str(path.parent / CONVERTED_DIR / ('converted' + path.stem + '.csv')) + Handler.write_csv(statement, out_file) - if not archive_file_dir.exists(): - archive_file_dir.mkdir() + #Now move the input file to backup + archive_file_dir = path.parent / BACKUP_DIR + archive_file = (path.stem + '{:04d}' + path.suffix) + destination = Handler.unique_path(archive_file_dir, archive_file) - if not destination.exists(): - path.replace(destination) + if not archive_file_dir.exists(): + archive_file_dir.mkdir() + + if not destination.exists(): + path.replace(destination) if __name__ == "__main__":