Wait for the file to be fully copied before parsing it

This commit is contained in:
2022-01-12 10:13:01 +10:00
parent d93f0869a7
commit 23f68f19e8
3 changed files with 32 additions and 16 deletions

1
.gitignore vendored
View File

@@ -138,3 +138,4 @@ dmypy.json
# Cython debug symbols # Cython debug symbols
cython_debug/ cython_debug/
.devcontainer/devcontainer.json

View File

@@ -1,4 +1,4 @@
FROM python:3 FROM python:3.8
ADD converter.py /app/ ADD converter.py /app/

View File

@@ -12,10 +12,10 @@ import watchdog.events
import watchdog.observers import watchdog.observers
DATE_FORMAT = "%d/%m/%Y" DATE_FORMAT = "%d/%m/%Y"
#WATCH_DIR = os.path.dirname(os.path.realpath(__file__))
WATCH_DIR = '/mnt/data/' WATCH_DIR = '/mnt/data/'
PATTERN = '*.qfx' PATTERN = '*.qfx'
BACKUP_DIR = 'Imported' BACKUP_DIR = 'Imported'
CONVERTED_DIR = 'Converted'
class Handler(watchdog.events.PatternMatchingEventHandler): class Handler(watchdog.events.PatternMatchingEventHandler):
@@ -69,13 +69,28 @@ class Handler(watchdog.events.PatternMatchingEventHandler):
return path return path
def on_created(self, event): def on_created(self, event):
qfx = OfxParser.parse(open(event.src_path, encoding="latin-1"), fail_fast=False) 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) statement = Handler.get_statement_from_qfx(qfx)
path = Path(event.src_path) path = Path(event.src_path)
path.resolve() path.resolve()
out_file = str(path.parent / ('converted' + path.stem + '.csv')) converted_dir = path.parent / CONVERTED_DIR
if not converted_dir.exists():
converted_dir.mkdir()
out_file = str(path.parent / CONVERTED_DIR / ('converted' + path.stem + '.csv'))
Handler.write_csv(statement, out_file) Handler.write_csv(statement, out_file)
#Now move the input file to backup #Now move the input file to backup