-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifferenceParserUTM.py
103 lines (92 loc) · 3.87 KB
/
DifferenceParserUTM.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
102
103
import xml.dom.minidom as minidom
import logging
import time
import sys
try:
from xlsxwriter.workbook import Workbook
except ImportError as err:
print(err)
time.sleep(20)
PATH_ = 'C:\\PythonProgs\\ForReports\\Difference\\'
def get_all_text(node) -> str:
if node.nodeType == node.TEXT_NODE:
value = node.data
else:
value = ''.join(get_all_text(child_node) for child_node in node.childNodes)
return value
def get_closing_balance(xml):
try:
with minidom.parse(xml) as doc:
xml_tuple = ("row_id", "col_account_id", "col_login",
"col_full_name", "col_charges_total", "col_payments",
"col_closing_balance")
y = []
for row_el in doc.getElementsByTagName("row"):
row = []
for tag_name in xml_tuple:
child_el = row_el.getElementsByTagName(tag_name)[0]
child_text = get_all_text(child_el) or "No data"
row.append(child_text)
y.append(row)
except (Exception, TypeError, AttributeError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
log.info("Исходящий баланс на позапрошлый месяц проверен")
yield tuple(y)
def get_initial_balance(xml):
try:
with minidom.parse(xml) as doc:
x = []
for row_el in doc.getElementsByTagName("row"):
child_el = row_el.getElementsByTagName("col_initial_balance")[0]
child_text = get_all_text(child_el)
x.append(child_text)
except (Exception, TypeError, AttributeError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
log.info("Входящий баланс на прошлый месяц проверен")
yield tuple(x)
def result(xml1, xml2):
try:
y_ = next(get_closing_balance(xml1))
x_ = next(get_initial_balance(xml2))
c = [(*close_value, init_value) \
for close_value, init_value in zip(y_, x_) \
if init_value != close_value[6]]
except (TypeError, AttributeError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
log.info(f"Всего записей с разницей найдено: {len(c)}")
try:
with Workbook('C:\\PythonProgs\\ForReports\\Difference\\Result.xlsx',\
{'strings_to_numbers': True}) as workbook:
worksheet = workbook.add_worksheet()
for row_num, row_data in enumerate(c[:-1]):
for col_num, col_data in enumerate(row_data):
worksheet.set_column(col_num, 5, 20)
worksheet.write(row_num+1, col_num, col_data)
except (TypeError, AttributeError):
log.error("Exception occurred", exc_info=True)
time.sleep(20)
else:
log.info("Экспортировано в xlsx успешно")
input("Нажмите Enter для выхода.")
if __name__ == "__main__":
try:
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
f_format = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
handler.setFormatter(f_format)
log.addHandler(handler)
except Exception as exc:
print(exc)
time.sleep(20)
else:
document1 = ''.join([PATH_, sys.argv[1]])
document2 = ''.join([PATH_, sys.argv[2]])
result(document1, document2)