Wait for the file to be fully copied before parsing it
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -138,3 +138,4 @@ dmypy.json
|
|||||||
# Cython debug symbols
|
# Cython debug symbols
|
||||||
cython_debug/
|
cython_debug/
|
||||||
|
|
||||||
|
.devcontainer/devcontainer.json
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM python:3
|
FROM python:3.8
|
||||||
|
|
||||||
ADD converter.py /app/
|
ADD converter.py /app/
|
||||||
|
|
||||||
|
|||||||
21
converter.py
21
converter.py
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user