-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestrai.py
More file actions
397 lines (362 loc) · 16 KB
/
Copy pathestrai.py
File metadata and controls
397 lines (362 loc) · 16 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
from tqdm import tqdm
import json
import time
from datetime import date as DT
FORMAT_LOADED = False
FORMAT_IS_META = False
zero_fill = "00000"
META_DICT = {}
dict_formato_non_meta = {"formato": "",
"sample": "",
"id_term_start": "", "id_term_end": "", "verso": "",
"badge_start": "", "badge_end": "", "badge_pos": "",
"data_start": "", "data_end": "", "data_pos": "",
"time_start": "", "time_end": "", "separator": ""}
dict_formato_caricato = {}
dict_formato = {"formato": "konnect",
"sample": "0001 A 0000000000 12/10/2017 05:35 000000",
"id_term_start": 0, "id_term_end": 3, "verso": 5,
"badge_start": 7, "badge_end": 16, "badge_pos": 2,
"data_start": 18, "data_end": 27, "data_pos": 3,
"time_start": 29, "time_end": 33, "separator": " "}
def menuformati(config):
choice = ""
while choice != "q":
print("Scegliere una opzione:")
print("0 - Elenco Formati")
print("1 - Nuovo Formato")
print("2 - Nuovo META-FORMATO")
print("3 - Carica Formato")
print("q - Menu Precedente")
choice = input()
if choice == "0":
elencoformati(config)
elif choice == "1":
nuovoformato(config)
elif choice == "2":
nuovometaformato(config)
elif choice == "3":
caricaformato(config)
def elencoformati(config):
with open(config) as config_file:
cfg = json.load(config_file)
for item in cfg['formati']:
print(item['formato'])
def nuovoformato(config):
print("Definizione nuovo formato")
print("Contate le cifre partendo da ZERO")
print("Se il formato ha un separatore per i vari campi ad esempio uno spazio, \n" +
"allora i valori *_pos devono essere assegnati dando al primo campo il valore 0")
print("Se il formato non ha separatori, allora il valore separatore e i *_pos non vanno attribuiti")
for k in dict_formato.keys():
print("Valore per: " + k)
dict_formato[k] = input()
print(dict_formato)
with open(config, 'r') as config_file:
formati = json.load(config_file)
with open(config, 'w') as config_file:
formati['formati'].append(dict_formato)
json.dump(formati, config_file)
def nuovometaformato(config):
end = True
choice = ""
global META_DICT
META_DICT = {}
META_DICT["formato"] = ""
META_DICT["META"] = True
print("Definiziione META formato")
print("Questa funzione permette di definire un dizionario chiave - valore, \n" +
"ad ogni chiave corrisponde un campo da estrarre" +
"i valori di ogni chiave sono la posizione del carattere iniziale e di quello finale che compongono il campo")
while META_DICT["formato"] == "":
print("Inserire Nome Del formato:")
META_DICT["formato"] = input()
while end:
print("Nome del campo (chiave)?")
chiave = input()
print("Poisizione Carattere iniziale")
cstart = input()
cend = input("Posizone Carattere Finale")
META_DICT[chiave] = [cstart, cend]
while choice != "S" and choice != "s" and choice != "N" and choice != "n":
print("Vuoi inseirere altre chiavi? S/N")
choice = input()
if choice == "N" or choice == "n":
end = False
elif choice != "S" and choice != "s" and choice != "N" and choice != "n":
print("Scelta non valida: S - s / N - n")
with open(config, 'r') as config_file:
formati = json.load(config_file)
with open(config, 'w') as config_file:
formati['formati'].append(META_DICT)
json.dump(formati, config_file)
def caricaformato(config):
global FORMAT_LOADED
global FORMAT_IS_META
global dict_formato_caricato
formato_found = False
quit = False
dict_to_load = {}
while formato_found == False and quit == False:
print("Nome Formato Da caricare:")
nome_formato = input()
with open(config, 'r') as config_file:
cfg = json.load(config_file)
for item in cfg['formati']:
if item['formato'] == nome_formato:
print("Formato Trovato, caricamento...")
formato_found = True
dict_to_load = item
quit = True
break
if formato_found != True:
print("Formato Non Presente, premere invio per altro formato...")
print("Oppure premere q per menu precendente")
quit = input()
if quit == "q":
quit = True
else:
dict_formato_caricato = dict_to_load
print(dict_formato_caricato)
if "META" in dict_formato_caricato.keys():
FORMAT_IS_META = True
print("Questo Formato e un META-FORMATO")
else:
"""for k in dict_formato_caricato.keys():
dict_formato_caricato[k] = dict_to_load[k]"""
print("Formato Standard Caricato")
FORMAT_LOADED = True
def encodeNumberInLine(file, param_number):
output_file = file + "out" + ".txt"
SEP = False
if dict_formato_caricato['separator'] != "":
SEP = True
with open(file + ".txt" ) as f:
size = sum(1 for _ in f)
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
encoded_number_str = zero_fill
if SEP:
params = line.split(dict_formato_caricato['separator'])
number = params[param_number]
else:
number = line[int(dict_formato_caricato['badge_start']):int(dict_formato_caricato['badge_end'])]
number_split = []
for i in range(0, len(number), 2):
number_split.append(number[i:i+2])
for i in range(0, len(number_split)):
if number_split[i] == "00":
number_split[i] = "0"
else:
number_split[i] = str(hex(int(number_split[i])))[2:]
encoded_number_str += (number_split[i])
# print(encoded_number_str)
if SEP:
params[2] = encoded_number_str
new_line = ""
for i in range(0,len(params)-1):
new_line = new_line + params[i] + " "
new_line = new_line + params[len(params)-1]
out.write(new_line)
else:
line.replace(line[int(dict_formato_caricato['badge_start']):int(dict_formato_caricato['badge_end'])+1], encoded_number_str)
except IndexError:
print("\nErrore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
return
def extractByBadge(file, badge_number):
output_file = file + "out" + ".txt"
with open(file + ".txt" ) as f:
size = sum(1 for _ in f)
if dict_formato_caricato['separator'] != "":
print("Con Separatore...")
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
params = line.split(" ")
badge = params[int(dict_formato_caricato['badge_pos'])]
if badge == badge_number:
out.write(line)
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
print("job done: " + output_file + " per risultati")
else:
print("Senza Separatore... " + badge_number)
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
badge = line[int(dict_formato_caricato['badge_start']): int(dict_formato_caricato['badge_end'])+1]
if badge == badge_number:
out.write(line)
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
print("job done: " + output_file + " per risultati")
def extractByDate(file, date):
output_file = file + "out" + ".txt"
with open(file + ".txt" ) as f:
size = sum(1 for _ in f)
if dict_formato_caricato['separator'] != "":
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
params = line.split(" ")
try:
dt = params[int(dict_formato_caricato['data_pos'])]
if date == dt:
out.write(line)
except ValueError:
continue
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
print("job done: " + output_file + " per risultati")
else:
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
dt = line[int(dict_formato_caricato['data_start']):int(dict_formato_caricato['data_end'])+1]
if date == dt:
out.write(line)
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
print("job done: " + output_file + " per risultati")
def splitfromdate(file, dal):
output_file = file + "out" + ".txt"
with open(file + ".txt" ) as f:
size = sum(1 for _ in f)
if dict_formato_caricato['separator'] != "":
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
params = line.split(" ")
dt = params[int(dict_formato_caricato['data_pos'])]
try:
fromdate = time.strptime(dal, "%d/%m/%Y")
linedate = time.strptime(dt, "%d/%m/%Y")
except ValueError:
continue
if linedate >= fromdate:
out.write(line)
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
print("job done: " + output_file + " per risultati")
else:
if len(dal) != 8 and len(dal) != 6:
print("Questo Formato di data non e supportato")
return
else:
with open(output_file, 'w') as out:
with open(file+".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
dt = line[int(dict_formato_caricato['data_start']):int(dict_formato_caricato['data_end'])+1]
try:
if len(dt) == 8:
linedate = DT(int(dt[4:8]), int(dt[2:4]), int(dt[0:2]))
fromdate = DT(int(dal[4:8]), int(dal[2:4]), int(dal[0:2]))
elif len(dt) == 6:
linedate = DT(int(dt[4:6]), int(dt[2:4]), int(dt[0:2]))
fromdate = DT(int(dal[4:6]), int(dal[2:4]), int(dal[0:2]))
except ValueError:
continue
if linedate >= fromdate:
out.write(line)
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
print("job done: " + output_file + " per risultati")
def metaestrai(file):
campo = ""
output_file = file + "out" + ".txt"
with open(file + ".txt" ) as f:
size = sum(1 for _ in f)
print(dict_formato_caricato)
print("Campi presenti nel meta-formato")
for key in dict_formato_caricato.keys():
if key != "META" and key != "formato":
print(key + " : " + str(dict_formato_caricato[key]))
while campo not in dict_formato_caricato.keys():
print("Nome del campo: ")
campo = input()
print("Valore da ricercare:")
value = input()
with open(output_file, 'w') as out:
with open(file + ".txt") as filetxt:
for line_number, line in tqdm(enumerate(filetxt, 1), total=size, unit="lines"):
try:
subline = line[int(dict_formato_caricato[campo][0]): int(dict_formato_caricato[campo][1]) + 1]
if subline == value:
out.write(line)
except IndexError:
print("\n Errore Linea non conforme")
print("errore @: " + str(line_number))
print(line)
continue
if __name__ == "__main__":
choice = ""
file = ""
while choice != "q":
print("Scegliere un opzione:")
print("q - QUIT")
print("0 - Menu Formati")
if FORMAT_LOADED:
print("1 - Converti badge dec -> hex")
print("2 - Estrai solo badge")
print("3 - Estrai solo data")
print("4 - Estrai da data")
print("5 - META-Estrai")
choice = input()
if choice == "0":
menuformati('estrai.json')
elif choice == "1" and FORMAT_LOADED:
print("Nome del file .txt?")
file = input()
encodeNumberInLine(file, int(dict_formato_caricato['badge_pos']))
elif choice == "2" and FORMAT_LOADED:
print("Nome del file .txt?")
file = input()
print("Numero di badge:")
bn = input()
extractByBadge(file, bn)
elif choice == "3" and FORMAT_LOADED:
print("Nome del file .txt?")
file = input()
print("Indicare la data nel formato appropriato")
date = input()
extractByDate(file, date)
elif choice == "4" and FORMAT_LOADED:
print("Nome del file .txt?")
file = input()
print("Indicare la data nel formato appropriato")
date = input()
splitfromdate(file, date)
elif choice == "5" and FORMAT_LOADED and FORMAT_IS_META:
print("Nome del file .txt?")
file = input()
metaestrai(file)