-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
321 lines (301 loc) · 16 KB
/
utils.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import io
import json
import os
import textwrap
import configs
from operator import itemgetter
import xml.etree.ElementTree as ET
from parsed_file import ParsedXML
from mapping import Mapping
from tabulate import tabulate
import checks as CK
def createDirectories():
if not os.path.isdir(configs.JSON_PATH):
os.mkdir(configs.JSON_PATH)
print("\nCreated jsons directory.")
else:
print("\nJsons directory already exists.")
if not os.path.isdir(configs.SUMMARY_PATH):
os.mkdir(configs.SUMMARY_PATH)
print("Created workflows summary directory.")
else:
print("Workflows summary directory already exists.")
if not os.path.isdir(configs.TABLE_PATH):
os.mkdir(configs.TABLE_PATH)
print("Created tables directory.\n")
else:
print("Tables directory already exists.\n")
def parseXMLFile():
workflow = ET.parse(configs.FILE_PATH)
print(f"Parsing {configs.FILE_NAME} ...")
parsed_xml = workflow.getroot()
print("Parsing completed.\n")
return parsed_xml
def createParsedXML(input_xml):
parsed_xml = ParsedXML(input_xml)
parsed_xml.createWorkflowField()
parsed_xml.createAllKeys()
parsed_xml.createRemainingFields()
parsed_xml.createWorkflowName()
return parsed_xml
def createMappings(field_list) -> list:
mapping_list = []
for mapping_name, fields in field_list:
mapping = Mapping(mapping_name)
message_checked = False
for sub_field in fields:
if sub_field.tag == 'parameters':
mapping.createParameters(sub_field)
if sub_field.tag == 'transformations':
for abstract_transformation in sub_field:
if abstract_transformation.get('name') == 'Exp_INPUT_VALORI':
exp_fields = abstract_transformation[0][0][0]
mapping.createRuleKeys(exp_fields)
elif abstract_transformation.get('name') == 'Exp_OUTPUT_LKP_ATTIVAZ_CONTROLLO':
exp_fields = abstract_transformation[0][0][0]
mapping.createFlag(exp_fields)
mapping.createMessage(exp_fields)
if mapping.getMessage() is not None:
message_checked = True
elif abstract_transformation.get('name') == 'Expression' or \
abstract_transformation.get('name') == 'Exp_OUTPUT_LKP' or \
abstract_transformation.get('name') == 'Expression1':
exp_fields = abstract_transformation[0][0][0]
mapping.createFlag(exp_fields)
elif abstract_transformation.get(configs.TYPE_STRING) == 'source:SourceTx':
mapping.createSourceTables(abstract_transformation)
elif abstract_transformation.get(configs.TYPE_STRING) == 'target:TargetTx':
mapping.createTargetTables(abstract_transformation)
elif abstract_transformation.get(
configs.TYPE_STRING) == 'joiner:JoinerTx' and abstract_transformation.get(
'name') == 'Joiner':
join_interfaces = abstract_transformation[0]
mapping.createFirstJoiners(
abstract_transformation, join_interfaces)
elif abstract_transformation.get(
configs.TYPE_STRING) == 'joiner:JoinerTx' and abstract_transformation.get(
'name') == 'Joiner1':
join_interfaces = abstract_transformation[0]
mapping.createSecondJoiners(
abstract_transformation, join_interfaces)
elif abstract_transformation.get(
configs.TYPE_STRING) == 'lookup:LookupTx' and abstract_transformation.get(
'name') == configs.LKP_TABLE_NAME:
mapping.updateLookup(
abstract_transformation.get('name'))
if not message_checked and (
abstract_transformation.get(configs.TYPE_STRING) == 'expression:ExpressionTx' or
abstract_transformation.get('name').lower() == 'custom_message'):
exp_fields = abstract_transformation[0][0][0]
mapping.createMessage(exp_fields)
mapping_list.append(mapping)
return mapping_list
def addMappingsToJSON(mappings, output_file) -> dict:
for mapping in mappings:
output_file[mapping.getName()] = {"Parametri": mapping.getParameter(),
"Chiavi controllo": mapping.getRuleKeys(),
"Tabelle sorgenti": mapping.getSourceTables(),
"Tabelle target": mapping.getTargetTables(),
"Join": mapping.getJoiners(),
"Messaggio": mapping.getMessage(),
"Flag attivo": mapping.getFlag(),
"Tabella lookup": mapping.getLookup()}
return output_file
def sortJSONByRuleKeys(output_file) -> dict:
sorted_output_file = {}
dict_to_sort = {}
for entry in output_file:
key = (int(output_file.get(entry)['Chiavi controllo'][1][1]), int(
output_file.get(entry)['Chiavi controllo'][2][1]))
dict_to_sort[key] = entry
sorted_keys = sorted(dict_to_sort, key=itemgetter(0, 1))
for key in sorted_keys:
mapping_name = dict_to_sort[key]
sorted_output_file[mapping_name] = output_file.get(mapping_name)
return sorted_output_file
def initializeParser() -> [str, str, str, dict, dict, str]:
createDirectories()
output_file = {}
input_xml = parseXMLFile()
parsed_xml = createParsedXML(input_xml)
workflow_name = parsed_xml.getWorkflowName()
json_path_file = os.path.join(configs.JSON_PATH, workflow_name + '.json')
summary_path_file = os.path.join(
configs.SUMMARY_PATH, workflow_name + '.txt')
table_path_file = os.path.join(
configs.TABLE_PATH, workflow_name + '_table.txt')
for field_list in [parsed_xml.getFolderFields(), parsed_xml.getProjectFields(), parsed_xml.getMappingFields()]:
mappings = createMappings(field_list)
output_file = addMappingsToJSON(mappings, output_file)
return json_path_file, summary_path_file, table_path_file, output_file, parsed_xml.getAllKeys(), workflow_name
def generateJSON(json_path, output_file, all_keys) -> None:
with open(json_path, 'w') as f:
try:
sorted_output_file = sortJSONByRuleKeys(output_file)
except:
print(
"Rule keys missing. Can't sort .json entries, the output file won't be sorted")
json.dump(output_file, f, ensure_ascii=True, indent=4)
else:
if len(sorted_output_file.keys()) != len(all_keys):
json.dump(output_file, f, ensure_ascii=True, indent=4)
else:
json.dump(sorted_output_file, f, ensure_ascii=True, indent=4)
def generateTXT(json_path, txt_path) -> None:
with open(json_path) as json_file:
with open(txt_path, 'a', encoding='utf-8') as txt_file:
txt_file.seek(0, 0)
txt_file.truncate()
mappings = json.load(json_file)
for mapping in mappings:
output_mapping_info = io.StringIO()
output_mapping_info.write('\n')
output_mapping_info.write(mapping)
output_mapping_info.write('\n')
try:
dt_riferimento = mappings.get(mapping)['Parametri'][0][0]
dt_riferimento_value = mappings.get(mapping)[
'Parametri'][0][1]
abi = mappings.get(mapping)['Parametri'][1][0]
abi_value = mappings.get(mapping)[
'Parametri'][1][1]
except:
output_mapping_info.write("Nessun parametro impostato")
else:
output_mapping_info.write(
f"I parametri del mapping sono {dt_riferimento}={dt_riferimento_value} e {abi}={abi_value}")
output_mapping_info.write('\n')
try:
id_sistema = mappings.get(
mapping)['Chiavi controllo'][0][0]
id_progr_controllo = mappings.get(
mapping)['Chiavi controllo'][1][0]
id_progr_occorrenza = mappings.get(
mapping)['Chiavi controllo'][2][0]
id_sistema_value = mappings.get(
mapping)['Chiavi controllo'][0][1]
id_progr_controllo_value = mappings.get(
mapping)['Chiavi controllo'][1][1]
id_progr_occorrenza_value = mappings.get(
mapping)['Chiavi controllo'][2][1]
except:
output_mapping_info.write(
"Non ci sono chiavi di controllo")
else:
output_mapping_info.write(
f"Le chiavi di controllo sono {id_sistema}={id_sistema_value}, {id_progr_controllo}={id_progr_controllo_value} e {id_progr_occorrenza}={id_progr_occorrenza_value}")
output_mapping_info.write('\n')
output_mapping_info.write("Le tabelle sorgenti sono:")
output_mapping_info.write('\n')
count_string = 1
for source_table in mappings.get(mapping)['Tabelle sorgenti']:
table_name = source_table[0]
table_filter = source_table[1]
output_mapping_info.write(
f"\t- {table_name} con filtro {table_filter}")
if count_string != len(mappings.get(mapping)['Tabelle sorgenti']):
output_mapping_info.write('\n')
count_string += 1
else:
count_string = 1
output_mapping_info.write('\n')
output_mapping_info.write("Le tabelle target sono:")
output_mapping_info.write('\n')
for target_table in mappings.get(mapping)['Tabelle target']:
table_name = target_table[0]
table_sql = target_table[1]
output_mapping_info.write(
f"\t- {table_name} con pre-SQL {table_sql}")
if count_string != len(mappings.get(mapping)['Tabelle target']):
output_mapping_info.write('\n')
count_string += 1
else:
count_string = 1
output_mapping_info.write('\n')
if len(mappings.get(mapping)['Join']) == 0:
output_mapping_info.write("Non ci sono Join nel mapping\n")
else:
output_mapping_info.write("Le tabelle di Join sono:")
output_mapping_info.write('\n')
joiner_list = mappings.get(mapping)['Join']
detail_label = joiner_list[0][0]
if joiner_list[0][1] == "ABI":
detail_table = "Read_MAPPA_GRUPPO_CCB"
else:
detail_table = joiner_list[0][1]
master_label = joiner_list[1][0]
master_table = joiner_list[1][1]
first_join_key = joiner_list[2][1]
if len(mappings.get(mapping)['Join']) == 3:
output_mapping_info.write(
f"\t- {master_table} ({master_label}) e {detail_table} ({detail_label}) con chiave")
output_mapping_info.write('\n')
output_mapping_info.write(f"\t\t {first_join_key}")
output_mapping_info.write('\n')
elif len(mappings.get(mapping)['Join']) == 5:
second_detail_label = joiner_list[3][0]
second_detail_table = joiner_list[3][1]
second_join_key = joiner_list[4][1]
output_mapping_info.write(
f"\t- {master_table} ({master_label}) e {detail_table} ({detail_label}) con chiave")
output_mapping_info.write('\n')
output_mapping_info.write(f"\t\t {first_join_key}")
output_mapping_info.write('\n')
output_mapping_info.write(
f"\t- L'output del precedente Join e\' in join con {second_detail_table} ({second_detail_label}) con chiave")
output_mapping_info.write('\n')
output_mapping_info.write(f"\t\t {second_join_key}")
output_mapping_info.write('\n')
try:
message = mappings.get(mapping)['Messaggio'].replace(
"\r", "").replace("\n", " ")
except:
message = mappings.get(mapping)['Messaggio']
if message is None:
output_mapping_info.write(
'Campo MESSAGGIO non valorizzato\n')
else:
output_mapping_info.write(
'Il valore del campo MESSAGGIO e\':')
output_mapping_info.write('\n')
wrapped_text = textwrap.wrap(message, width=120)
for text in wrapped_text:
output_mapping_info.write(f'\t{text}\n')
lookup_name = mappings.get(mapping)['Tabella lookup']
flag = mappings.get(mapping)['Flag attivo']
if flag == 1:
output_mapping_info.write(
f"FLG_ATTIVO collegato da {lookup_name}")
else:
output_mapping_info.write(
f"FLG_ATTIVO potrebbe non essere collegato da {lookup_name}")
output_mapping_info.write('\n')
txt_file.write(output_mapping_info.getvalue())
def generateTable(json_path, table_path) -> None:
table = [['Mapping', 'Parametri', 'Filtro',
'Coerenza Chiavi e PRESQL', 'Link FLG_ATTIVO e LOOKUP', 'MESSAGGIO valido']]
with open(json_path) as json_file:
mappings = json.load(json_file)
for mapping in mappings:
param_checked = CK.check_parameters(
mappings.get(mapping)['Parametri'])
filter_checked = CK.check_filter(
mappings.get(mapping)['Tabelle sorgenti'])
keys_sql_checked = CK.check_keys_and_SQL(mappings.get(
mapping)["Chiavi controllo"], mappings.get(mapping)['Tabelle target'])
flag_checked = CK.check_flag(mappings.get(
mapping)['Flag attivo'], mappings.get(mapping)['Tabella lookup'])
message_checked = CK.check_message(
mappings.get(mapping)['Messaggio'])
table.append([mapping, param_checked, filter_checked,
keys_sql_checked, flag_checked, message_checked])
with open(table_path, 'w', encoding='utf-8') as txt_file:
txt_file.write('La "X" su Filtro può essere dato da\n')
txt_file.write('\t- La mancanza del campo ABI_BANCA su una vista\n')
txt_file.write(f'\t- Il filtro su DATA_RIFERIMENTO non è scritto in questo modo \'{configs.DATE_STRING}\'\n')
txt_file.write(
'La "X" sul Coerenza Chiavi e PRESQL può essere dato da\n')
txt_file.write(
f'\t- La condizione su ABI nel pre-SQL non è scritto in questo modo \'ABI IN {configs.ABI_STRING}\'\n\n')
txt_file.write(tabulate(table, headers='firstrow',
tablefmt='fancy_grid', showindex=range(1, len(mappings.keys()) + 1)))