-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
101 lines (82 loc) · 2.22 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from vzpbot import VZPBot
from model import Model, TooBigFile, NotSupportedExtension
from glob import glob
from datetime import datetime
from openpyxl.utils.exceptions import InvalidFileException
import os
from zipfile import BadZipFile
from pathlib import Path
import time
from datetime import datetime
import json
import sys
# #
# # ###### # ##### ###### ##### ####
# # # # # # # # # #
####### ##### # # # ##### # # ####
# # # # ##### # ##### #
# # # # # # # # # #
# # ###### ###### # ###### # # ####
def load_config():
conf_file = 'run.json'
if len(sys.argv) > 1:
conf_file = sys.argv[1]
# Base config
with open(conf_file, encoding='utf8') as config_file:
config = json.load(config_file)
# Finish
return config
def str_datetime():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# #
# # # #### ##### # #
# # # # # # # # #
# # # # # # # ####
# # # # # ##### # #
# # # # # # # # #
## ## #### # # # #
conf = load_config()
vzp = VZPBot()
vzp.init()
while True:
files = glob(conf['path_inbox'] + "\\*.*")
for file in files:
#
# Init
#
file_dir, file_name = os.path.split(file)
print("\n[{:s}] {:s}".format(str_datetime(), file_name))
try:
model = Model(file)
except (BadZipFile, ValueError, OverflowError, OSError, InvalidFileException, KeyError, TooBigFile, NotSupportedExtension) as e:
print("[{:s}] {:s}: {:s}".format(str_datetime(), type(e).__name__, str(e)))
continue
#
# Process all entries
#
while True:
try:
entry = model.next()
if not entry:
break
entry = vzp.fetch_insurance(entry)
except Exception as e:
entry['insurance_type'] = 'ERROR'
entry['insurance_text'] = "%s: %s" % (type(e).__name__, str(e))
model.persist(entry)
#
# Save & move files
#
try:
model.save("{:s}\\{:s}".format(conf['path_done'], file_name))
os.replace(
file,
"{:s}\\{:s}".format(conf['path_backup'], file_name)
)
except PermissionError:
print('ERROR: File is opened in another application!')
#
# Wait for new files
#
print("[{:s}] ...".format(str_datetime()))
time.sleep(5)