comparison Generic/fiebdc.py @ 21:f7e0cc58737f

Default interface in readFile method
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Sun, 14 Sep 2014 17:23:02 +0200
parents f5ec50b625d1
children 65e7ae0d0e63
comparison
equal deleted inserted replaced
20:f5ec50b625d1 21:f7e0cc58737f
51 "__character_set": character_set of the file 51 "__character_set": character_set of the file
52 "__generator": program which the file is created 52 "__generator": program which the file is created
53 "__cancel": Boolean value, True mean that the read process must stop 53 "__cancel": Boolean value, True mean that the read process must stop
54 "__filename": The filename of the fiebdc file that is readed 54 "__filename": The filename of the fiebdc file that is readed
55 "__pattern": re compiled pattern dict 55 "__pattern": re compiled pattern dict
56 "__statistics": Statistics object, records number
56 Methods: 57 Methods:
57 __init__(self, filename=None, budget=None) 58 __init__(self, filename=None, budget=None)
58 cancel(self) 59 cancel(self)
59 eraseControlCharacters(self, string) 60 eraseControlCharacters(self, string)
60 validateCode(self, code) 61 validateCode(self, code)
115 "descomposition" : re.compile(u"^([^:]+):(.*)$"), 116 "descomposition" : re.compile(u"^([^:]+):(.*)$"),
116 "var" : re.compile(u"^([$%][A-ZÑ][()0-9, ]*)=(.*)$"), 117 "var" : re.compile(u"^([$%][A-ZÑ][()0-9, ]*)=(.*)$"),
117 "after_first_tilde" : re.compile(u"^[^~]*~"), 118 "after_first_tilde" : re.compile(u"^[^~]*~"),
118 "end_control" : re.compile(u"((\r\n)| |\t)+$"), 119 "end_control" : re.compile(u"((\r\n)| |\t)+$"),
119 } 120 }
121 self.__statistics = Statistics()
120 122
121 def cancel(self): 123 def cancel(self):
122 """def cancel(self) 124 """def cancel(self)
123 125
124 Sets the "__cancel" attribute to True, It stops the read process. 126 Sets the "__cancel" attribute to True, It stops the read process.
320 # TODO: test ~Q ~J ~G 322 # TODO: test ~Q ~J ~G
321 # TODO: ~P. Registro tipo Descripción Paramétrica. 323 # TODO: ~P. Registro tipo Descripción Paramétrica.
322 # TODO: ~O. Registro tipo Relación Comercial. 324 # TODO: ~O. Registro tipo Relación Comercial.
323 # TODO: test records 325 # TODO: test records
324 _field_list = record.split(u"|") 326 _field_list = record.split(u"|")
325 self._record_number = self._record_number +1 327 self.__statistics.records = self.__statistics.records +1
326 _budget = self.__budget 328 _budget = self.__budget
327 if _field_list[0] == u"V": 329 if _field_list[0] == u"V":
328 self._record_V_number += 1 330 self.__statistics.V += 1
329 self._parseV(_field_list) 331 self._parseV(_field_list)
330 elif _field_list[0] == u"C": 332 elif _field_list[0] == u"C":
331 self._record_C_number += 1 333 self.__statistics.C += 1
332 self._parseC(_field_list) 334 self._parseC(_field_list)
333 elif _field_list[0] == u"D": 335 elif _field_list[0] == u"D":
334 self._record_D_number += 1 336 self.__statistics.D += 1
335 self._parseDY(_field_list) 337 self._parseDY(_field_list)
336 elif _field_list[0] == u"Y": 338 elif _field_list[0] == u"Y":
337 self._record_Y_number += 1 339 self.__statistics.Y += 1
338 self._parseDY(_field_list) 340 self._parseDY(_field_list)
339 elif _field_list[0] == u"M": 341 elif _field_list[0] == u"M":
340 self._record_M_number += 1 342 self.__statistics.M += 1
341 self._parseMN(_field_list) 343 self._parseMN(_field_list)
342 elif _field_list[0] == u"N": 344 elif _field_list[0] == u"N":
343 self._record_N_number += 1 345 self.__statistics.N += 1
344 self._parseMN(_field_list) 346 self._parseMN(_field_list)
345 elif _field_list[0] == u"T": 347 elif _field_list[0] == u"T":
346 self._record_T_number += 1 348 self.__statistics.T += 1
347 self._parseT(_field_list) 349 self._parseT(_field_list)
348 elif _field_list[0] == u"K": 350 elif _field_list[0] == u"K":
349 self._record_K_number += 1 351 self.__statistics.K += 1
350 self._parseK(_field_list) 352 self._parseK(_field_list)
351 elif _field_list[0] == u"W": 353 elif _field_list[0] == u"W":
352 self._record_W_number += 1 354 self.__statistics.W += 1
353 self._parseW(_field_list) 355 self._parseW(_field_list)
354 elif _field_list[0] == u"L": 356 elif _field_list[0] == u"L":
355 self._record_L_number += 1 357 self.__statistics.L += 1
356 self._parseL(_field_list) 358 self._parseL(_field_list)
357 elif _field_list[0] == u"Q": 359 elif _field_list[0] == u"Q":
358 self._record_Q_number += 1 360 self.__statistics.Q += 1
359 self._parseQ(_field_list) 361 self._parseQ(_field_list)
360 elif _field_list[0] == u"J": 362 elif _field_list[0] == u"J":
361 self._record_J_number += 1 363 self.__statistics.J += 1
362 self._parseJ(_field_list) 364 self._parseJ(_field_list)
363 elif _field_list[0] == u"G": 365 elif _field_list[0] == u"G":
364 self._record_G_number += 1 366 self.__statistics.G += 1
365 self._parseG(_field_list) 367 self._parseG(_field_list)
366 elif _field_list[0] == u"E": 368 elif _field_list[0] == u"E":
367 self._record_E_number += 1 369 self.__statistics.E += 1
368 self._parseE(_field_list) 370 self._parseE(_field_list)
369 elif _field_list[0] == "O": 371 elif _field_list[0] == "O":
370 self._record_O_number += 1 372 self.__statistics.O += 1
371 elif _field_list[0] == u"P": 373 elif _field_list[0] == u"P":
372 self._record_P_number += 1 374 self.__statistics.P += 1
373 self._parseP(_field_list) 375 self._parseP(_field_list)
374 elif _field_list[0] == u"X": 376 elif _field_list[0] == u"X":
375 self._record_X_number += 1 377 self.__statistics.X += 1
376 self._parseX(_field_list) 378 self._parseX(_field_list)
377 elif _field_list[0] == u"B": 379 elif _field_list[0] == u"B":
378 self._record_B_number += 1 380 self.__statistics.B += 1
379 self._parseB(_field_list) 381 self._parseB(_field_list)
380 elif _field_list[0] == u"F": 382 elif _field_list[0] == u"F":
381 self._record_F_number += 1 383 self.__statistics.F += 1
382 self._parseF(_field_list) 384 self._parseF(_field_list)
383 elif _field_list[0] == u"A": 385 elif _field_list[0] == u"A":
384 self._record_A_number += 1 386 self.__statistics.A += 1
385 self._parseA(_field_list) 387 self._parseA(_field_list)
386 else: 388 else:
387 self._record_Unknow_number += 1 389 self.__statistics.unknow += 1
388 390
389 def _parseV(self, field_list): 391 def _parseV(self, field_list):
390 """_parseV(self, field_list) 392 """_parseV(self, field_list)
391 393
392 field_list: field list of the record 394 field_list: field list of the record
399 6- [Comment] 401 6- [Comment]
400 7- [Data type] 402 7- [Data type]
401 8- [Number budget certificate] 403 8- [Number budget certificate]
402 9- [Date budget certificate] 404 9- [Date budget certificate]
403 """ 405 """
404 if self._record_number != 1: 406 if self.__statistics.records != 1:
405 print utils.mapping(_("The 'V' record (Property and Version) "\ 407 print utils.mapping(_("The 'V' record (Property and Version) "\
406 "must be the first record in the file but it is the "\ 408 "must be the first record in the file but it is the "\
407 "number: $1"), (self._record_number,)) 409 "number: $1"), (self.__statistics.records,))
408 print _("The default values were taken and this V record is "\ 410 print _("The default values were taken and this V record is "\
409 "ignored") 411 "ignored")
410 return 412 return
411 # _____number of fields_____ 413 # _____number of fields_____
412 # Any INFORMATION after last field separator is ignored 414 # Any INFORMATION after last field separator is ignored
492 self.__budget.setBudgetype(_data_type) 494 self.__budget.setBudgetype(_data_type)
493 self.__budget.setCertificateOrder(_number_certificate) 495 self.__budget.setCertificateOrder(_number_certificate)
494 self.__budget.setCertificateDate(_parsed_date_cerfificate) 496 self.__budget.setCertificateDate(_parsed_date_cerfificate)
495 elif _data_type != "": 497 elif _data_type != "":
496 self.__budget.setBudgeType(_data_type) 498 self.__budget.setBudgeType(_data_type)
497 self.num_valid_record = self.num_valid_record + 1 499 self.__statistics.valid = self.__statistics.valid + 1
498 500
499 def _parseK(self, field_list): 501 def _parseK(self, field_list):
500 """_parseK(self, field_list) 502 """_parseK(self, field_list)
501 503
502 field_list: field list of the record 504 field_list: field list of the record
734 "DCD" : _DI, 736 "DCD" : _DI,
735 "DIVISA": _DIVISA} 737 "DIVISA": _DIVISA}
736 _decimal_index = _decimal_index + 13 738 _decimal_index = _decimal_index + 13
737 self.__budget.setDecimals(_percentage_dict, 739 self.__budget.setDecimals(_percentage_dict,
738 (_decimal_index//13)) 740 (_decimal_index//13))
739 self.num_valid_record = self.num_valid_record +1 741 self.__statistics.valid = self.__statistics.valid +1
740 742
741 def _parseC(self, field_list): 743 def _parseC(self, field_list):
742 """_parseC(self, field_list) 744 """_parseC(self, field_list)
743 745
744 field_list: field list of the record 746 field_list: field list of the record
911 _type = 0 913 _type = 0
912 _subtype = u"" 914 _subtype = u""
913 self.__budget.setRecord(_code.encode("utf8"), _synonyms, _hierarchy, 915 self.__budget.setRecord(_code.encode("utf8"), _synonyms, _hierarchy,
914 _unit.encode("utf8"), _summary.encode("utf8"), 916 _unit.encode("utf8"), _summary.encode("utf8"),
915 _prices, _dates, _type, _subtype.encode("utf8")) 917 _prices, _dates, _type, _subtype.encode("utf8"))
916 self.num_valid_record = self.num_valid_record + 1 918 self.__statistics.valid = self.__statistics.valid + 1
917 919
918 def _parseDY(self, field_list): 920 def _parseDY(self, field_list):
919 """_parseDY(self, field_list) 921 """_parseDY(self, field_list)
920 922
921 field_list: field list of the record 923 field_list: field list of the record
982 else: #_record_type == "Y" 984 else: #_record_type == "Y"
983 _position = -1 985 _position = -1
984 self.__budget.setTree(_code.encode("utf8"), _child_code.encode("utf8"), _position, _factor, 986 self.__budget.setTree(_code.encode("utf8"), _child_code.encode("utf8"), _position, _factor,
985 _yield, "", "", "", "") 987 _yield, "", "", "", "")
986 _child_index = _child_index + 3 988 _child_index = _child_index + 3
987 self.num_valid_record = self.num_valid_record +1 989 self.__statistics.valid = self.__statistics.valid +1
988 990
989 def _parseT(self, field_list): 991 def _parseT(self, field_list):
990 """_parseT(self, field_list) 992 """_parseT(self, field_list)
991 993
992 field_list: field list of the record 994 field_list: field list of the record
1010 # "#" and "##" characters at the end of the code are erased 1012 # "#" and "##" characters at the end of the code are erased
1011 # invalid characters are also erased 1013 # invalid characters are also erased
1012 _code = self.validateCode(_code) 1014 _code = self.validateCode(_code)
1013 # _____Text_____ 1015 # _____Text_____
1014 self.__budget.setText(_code.encode("utf8"), _text.encode("utf8")) 1016 self.__budget.setText(_code.encode("utf8"), _text.encode("utf8"))
1015 self.num_valid_record = self.num_valid_record + 1 1017 self.__statistics.valid = self.__statistics.valid + 1
1016 1018
1017 def _parseMN(self, field_list): 1019 def _parseMN(self, field_list):
1018 """_parseMN(self, field_list) 1020 """_parseMN(self, field_list)
1019 1021
1020 field_list: field list of the record 1022 field_list: field list of the record
1149 _line_list.append([_linetype, _comment, _units, 1151 _line_list.append([_linetype, _comment, _units,
1150 _length, _width, _height, _formula]) 1152 _length, _width, _height, _formula])
1151 _line_index = _line_index + 6 1153 _line_index = _line_index + 6
1152 self.__budget.setTree(_parent_code, _child_code.encode("utf8"), _path, "", "", 1154 self.__budget.setTree(_parent_code, _child_code.encode("utf8"), _path, "", "",
1153 _total, _line_list, _label.encode("utf8"), _record_type.encode("utf8")) 1155 _total, _line_list, _label.encode("utf8"), _record_type.encode("utf8"))
1154 self.num_valid_record = self.num_valid_record + 1 1156 self.__statistics.valid = self.__statistics.valid + 1
1155 1157
1156 def _parseW(self, field_list): 1158 def _parseW(self, field_list):
1157 """_parseW(self, field_list) 1159 """_parseW(self, field_list)
1158 1160
1159 field_list: field list of the record 1161 field_list: field list of the record
1188 # _____section_title_____ 1190 # _____section_title_____
1189 if _field_code != u"": 1191 if _field_code != u"":
1190 _field_dict[_field_code.encode("utf8")] = _field_title.encode("utf8") 1192 _field_dict[_field_code.encode("utf8")] = _field_title.encode("utf8")
1191 _field_index = _field_index + 2 1193 _field_index = _field_index + 2
1192 self.__budget.setSheetFields(_field_dict) 1194 self.__budget.setSheetFields(_field_dict)
1193 self.num_valid_record = self.num_valid_record +1 1195 self.__statistics.valid = self.__statistics.valid +1
1194 1196
1195 def _parseL(self, field_list): 1197 def _parseL(self, field_list):
1196 """_parseL(self, field_list) 1198 """_parseL(self, field_list)
1197 1199
1198 field_list: field list of the record 1200 field_list: field list of the record
1238 _section_title = self.delete_control_space(_section_title) 1240 _section_title = self.delete_control_space(_section_title)
1239 if _section_code != u"": 1241 if _section_code != u"":
1240 _section_dict[_section_code.encode("utf8")] = _section_title.encode("utf8") 1242 _section_dict[_section_code.encode("utf8")] = _section_title.encode("utf8")
1241 _section_index = _section_index + 2 1243 _section_index = _section_index + 2
1242 self.__budget.setSheetSections(_section_dict) 1244 self.__budget.setSheetSections(_section_dict)
1243 self.num_valid_record = self.num_valid_record +1 1245 self.__statistics.valid = self.__statistics.valid +1
1244 1246
1245 else: 1247 else:
1246 # Any INFORMATION after last field separator is ignored 1248 # Any INFORMATION after last field separator is ignored
1247 # The record must have 5 fields 1249 # The record must have 5 fields
1248 if len(field_list) > 5: 1250 if len(field_list) > 5:
1281 _paragraph_dict[ _paragraph_code.encode("utf8") ] = _section_text.encode("utf8") 1283 _paragraph_dict[ _paragraph_code.encode("utf8") ] = _section_text.encode("utf8")
1282 _section_dict[_section_code.encode("utf8")] = _paragraph_code.encode("utf8") 1284 _section_dict[_section_code.encode("utf8")] = _paragraph_code.encode("utf8")
1283 _section_index = _section_index + 2 1285 _section_index = _section_index + 2
1284 self.__budget.setSheetParagraphs(_paragraph_dict) 1286 self.__budget.setSheetParagraphs(_paragraph_dict)
1285 self.__budget.setSheetRecord(_record_code.encode("utf8"), "*", _section_dict) 1287 self.__budget.setSheetRecord(_record_code.encode("utf8"), "*", _section_dict)
1286 self.num_valid_record = self.num_valid_record +1 1288 self.__statistics.valid = self.__statistics.valid +1
1287 1289
1288 def _parseQ(self, field_list): 1290 def _parseQ(self, field_list):
1289 """_parseQ(self, field_list) 1291 """_parseQ(self, field_list)
1290 1292
1291 field_list: field list of the record 1293 field_list: field list of the record
1342 _field_dict[_field_key] = _section_dict 1344 _field_dict[_field_key] = _section_dict
1343 _section_dict[_section_code.encode("utf8")] = _paragraph_code.encode("utf8") 1345 _section_dict[_section_code.encode("utf8")] = _paragraph_code.encode("utf8")
1344 _section_index = _section_index + 3 1346 _section_index = _section_index + 3
1345 for _field, _section_dict in _field_dict.iteritems(): 1347 for _field, _section_dict in _field_dict.iteritems():
1346 self.__budget.setSheetRecord(_record_code.encode("utf8"), _field.encode("utf8"), _section_dict) 1348 self.__budget.setSheetRecord(_record_code.encode("utf8"), _field.encode("utf8"), _section_dict)
1347 self.num_valid_record = self.num_valid_record +1 1349 self.__statistics.valid = self.__statistics.valid +1
1348 1350
1349 def _parseJ(self, field_list): 1351 def _parseJ(self, field_list):
1350 """_parseJ(self, field_list) 1352 """_parseJ(self, field_list)
1351 1353
1352 field_list: field list of the record 1354 field_list: field list of the record
1373 if _paragraph_text == u"": 1375 if _paragraph_text == u"":
1374 # TODO: rtf and html files 1376 # TODO: rtf and html files
1375 print "Html and rtf files not yet implemented in ~J record" 1377 print "Html and rtf files not yet implemented in ~J record"
1376 else: 1378 else:
1377 self.__budget.setSheetParagraph(paragraph_code.encode("utf8"), paragraph_text.encode("utf8")) 1379 self.__budget.setSheetParagraph(paragraph_code.encode("utf8"), paragraph_text.encode("utf8"))
1378 self.num_valid_record = self.num_valid_record +1 1380 self.__statistics.valid = self.__statistics.valid +1
1379 1381
1380 def _parseG(self, field_list): 1382 def _parseG(self, field_list):
1381 """_parseG(self, field_list) 1383 """_parseG(self, field_list)
1382 1384
1383 field_list: field list of the record 1385 field_list: field list of the record
1442 print utils.mapping(_("The file $1 do not exist"), 1444 print utils.mapping(_("The file $1 do not exist"),
1443 (_grafic_file_path,)) 1445 (_grafic_file_path,))
1444 if len(_grafic_file_list) > 0: 1446 if len(_grafic_file_list) > 0:
1445 for _grafic_file in _tested_grafic_file_list: 1447 for _grafic_file in _tested_grafic_file_list:
1446 self.__budget.addFile(_record_code.encode("utf8"), _grafic_file, "img", "") 1448 self.__budget.addFile(_record_code.encode("utf8"), _grafic_file, "img", "")
1447 self.num_valid_record = self.num_valid_record +1 1449 self.__statistics.valid = self.__statistics.valid +1
1448 1450
1449 def _parseE(self, field_list): 1451 def _parseE(self, field_list):
1450 """_parseE(self, field_list) 1452 """_parseE(self, field_list)
1451 1453
1452 field_list: field list of the record 1454 field_list: field list of the record
1541 _email = _c_w_e_list[2] 1543 _email = _c_w_e_list[2]
1542 self.__budget.setCompany(_company_code.encode("utf8"), 1544 self.__budget.setCompany(_company_code.encode("utf8"),
1543 _sumamary.encode("utf8"), _name.encode("utf8"), 1545 _sumamary.encode("utf8"), _name.encode("utf8"),
1544 _local_offices, _cif.encode("utf8"), 1546 _local_offices, _cif.encode("utf8"),
1545 _web.encode("utf8"), _email.encode("utf8")) 1547 _web.encode("utf8"), _email.encode("utf8"))
1546 self.num_valid_record = self.num_valid_record +1 1548 self.__statistics.valid = self.__statistics.valid +1
1547 1549
1548 def _parseX(self, field_list): 1550 def _parseX(self, field_list):
1549 """_parseX(self, field_list) 1551 """_parseX(self, field_list)
1550 1552
1551 field_list: field list of the record 1553 field_list: field list of the record
1596 _ti_value = _field_2_list[_ti_index+1] 1598 _ti_value = _field_2_list[_ti_index+1]
1597 if _ti_code != u"" and _ti_value != u"": 1599 if _ti_code != u"" and _ti_value != u"":
1598 _ti_dict[_ti_code.encode("utf8")] = _ti_value.encode("utf8") 1600 _ti_dict[_ti_code.encode("utf8")] = _ti_value.encode("utf8")
1599 _ti_index = _ti_index + 2 1601 _ti_index = _ti_index + 2
1600 self.__budget.setTecnicalInformation(_record_code.encode("utf8"), _ti_dict) 1602 self.__budget.setTecnicalInformation(_record_code.encode("utf8"), _ti_dict)
1601 self.num_valid_record = self.num_valid_record +1 1603 self.__statistics.valid = self.__statistics.valid +1
1602 1604
1603 def _parseF(self, field_list): 1605 def _parseF(self, field_list):
1604 """_parseF(self, field_list) 1606 """_parseF(self, field_list)
1605 1607
1606 field_list: field list of the record 1608 field_list: field list of the record
1701 print utils.mapping(_("The file $1 do not exist"), 1703 print utils.mapping(_("The file $1 do not exist"),
1702 (_file_path,)) 1704 (_file_path,))
1703 if len(_tested_files_list) > 0: 1705 if len(_tested_files_list) > 0:
1704 for _file in _tested_file_list: 1706 for _file in _tested_file_list:
1705 self.__budget.addFile(_record_code.encode("utf8"), _file[0], file[1], file[2]) 1707 self.__budget.addFile(_record_code.encode("utf8"), _file[0], file[1], file[2])
1706 self.num_valid_record = self.num_valid_record +1 1708 self.__statistics.valid = self.__statistics.valid +1
1707 1709
1708 def _parseB(self, field_list): 1710 def _parseB(self, field_list):
1709 """_parseB(self, field_list) 1711 """_parseB(self, field_list)
1710 1712
1711 field_list: field list of the record 1713 field_list: field list of the record
1730 # invalid characters are also erased 1732 # invalid characters are also erased
1731 _code = self.validateCode(_code) 1733 _code = self.validateCode(_code)
1732 _new_code = self.validateCode(_new_code) 1734 _new_code = self.validateCode(_new_code)
1733 # change code 1735 # change code
1734 self.__budget.changeCode(_code, _new_code) 1736 self.__budget.changeCode(_code, _new_code)
1735 self.num_valid_record = self.num_valid_record + 1 1737 self.__statistics.valid = self.__statistics.valid + 1
1736 1738
1737 def _parseA(self, field_list): 1739 def _parseA(self, field_list):
1738 """_parseA(self, field_list) 1740 """_parseA(self, field_list)
1739 1741
1740 field_list: field list of the record 1742 field_list: field list of the record
1767 # replace "_" to " " 1769 # replace "_" to " "
1768 _labels = _labels.replace(u"_",u" ") 1770 _labels = _labels.replace(u"_",u" ")
1769 _label_list = _labels.split(u"\\") 1771 _label_list = _labels.split(u"\\")
1770 for _label in _label_list: 1772 for _label in _label_list:
1771 self.__budget.addLabel(_code.encode("utf8"), _label.encode("utf8")) 1773 self.__budget.addLabel(_code.encode("utf8"), _label.encode("utf8"))
1772 self.num_valid_record = self.num_valid_record + 1 1774 self.__statistics.valid = self.__statistics.valid + 1
1773 1775
1774 def _parseP(self, field_list): 1776 def _parseP(self, field_list):
1775 """_parseP(self, field_list) 1777 """_parseP(self, field_list)
1776 1778
1777 field_list: Parametric record 1779 field_list: Parametric record
1968 _description = _final_description[:-2] 1970 _description = _final_description[:-2]
1969 _lines = _description.split(u"\r\n") 1971 _lines = _description.split(u"\r\n")
1970 for _line in _lines: 1972 for _line in _lines:
1971 pass 1973 pass
1972 #print _line 1974 #print _line
1973 self.num_valid_record = self.num_valid_record + 1 1975 self.__statistics.valid = self.__statistics.valid + 1
1974 1976
1975 def readFile(self, budget=None, filename=None, interface=None): 1977 def readFile(self, budget=None, filename=None, interface=None):
1976 """readFile(self, budget=None, filename=None) 1978 """readFile(self, budget=None, filename=None)
1977 1979
1978 filename: the filename of the fiebdc file 1980 filename: the filename of the fiebdc file
1979 budget: base.obra object 1981 budget: base.obra object
1980 interface: a object to send messages 1982 interface: a object to send messages
1981 must have printf(message) progress(percent) 1983 must have readFile_send_message(message)
1982 recordStatistics(...) 1984 readFile_set_statistics(statistics)
1985 readFile_progress(percent)
1986 readFile_end()
1987 readFile_cancel()
1983 Return the budget objetc or None if the file can be readed 1988 Return the budget objetc or None if the file can be readed
1984 """ 1989 """
1985 if not filename is None and not budget is None: 1990 if not filename is None and not budget is None:
1986 self.__filename = filename 1991 self.__filename = filename
1987 self.__budget = budget 1992 self.__budget = budget
1988 self.__budget.filename = self.__filename 1993 self.__budget.filename = self.__filename
1989 if self.__filename is None or self.__budget is None or self.__cancel: 1994 if self.__filename is None or self.__budget is None or self.__cancel:
1990 return None 1995 return None
1991 if not os.path.exists(self.__filename): 1996 if not os.path.exists(self.__filename):
1992 return None 1997 return None
1998 if interface is None:
1999 interface = Interface()
2000 interface.readFile_set_statistics(self.__statistics)
1993 _time = time.time() 2001 _time = time.time()
1994 try: 2002 try:
1995 _file = open(self.__filename, 'r') 2003 _file = open(self.__filename, 'r')
1996 except IOError: 2004 except IOError:
1997 print utils.mapping("IOError: $1", (self.__filename,)) 2005 print utils.mapping("IOError: $1", (self.__filename,))
1998 return None 2006 return None
1999 self.__budget.filename = self.__filename 2007 self.__budget.filename = self.__filename
2000 self._record_number = 0 2008 interface.readFile_send_message(utils.mapping(_("Loading file $1"),
2001 self.num_valid_record = 0 2009 (self.__filename,)))
2002 self._record_V_number = 0
2003 self._record_C_number = 0
2004 self._record_D_number = 0
2005 self._record_Y_number = 0
2006 self._record_M_number = 0
2007 self._record_N_number = 0
2008 self._record_T_number = 0
2009 self._record_K_number = 0
2010 self._record_W_number = 0
2011 self._record_L_number = 0
2012 self._record_Q_number = 0
2013 self._record_J_number = 0
2014 self._record_G_number = 0
2015 self._record_E_number = 0
2016 self._record_O_number = 0
2017 self._record_P_number = 0
2018 self._record_X_number = 0
2019 self._record_B_number = 0
2020 self._record_F_number = 0
2021 self._record_A_number = 0
2022 self._record_Unknow_number = 0
2023 print utils.mapping(_("Loading file $1"), (self.__filename,))
2024 _filesize = float(os.path.getsize(self.__filename)) 2010 _filesize = float(os.path.getsize(self.__filename))
2025 interface.progress(_file.tell() / _filesize) 2011 interface.readFile_progress(_file.tell() / _filesize)
2026 _buffer = _file.read(1000) 2012 _buffer = _file.read(1000)
2027 # set codepage from V record 2013 # set codepage from V record
2028 _record_list = _buffer.split("~") 2014 _record_list = _buffer.split("~")
2029 registro_V = _record_list[1] 2015 registro_V = _record_list[1]
2030 # ~V|[PROPIEDAD_ARCHIVO]|VERSION_FORMATO[\DDMMAAAA]|[PROGRAMA_EMISION]| 2016 # ~V|[PROPIEDAD_ARCHIVO]|VERSION_FORMATO[\DDMMAAAA]|[PROGRAMA_EMISION]|
2037 if len(registro_V) > 5: 2023 if len(registro_V) > 5:
2038 _version = registro_V[5].strip() 2024 _version = registro_V[5].strip()
2039 # remove leading spaces 2025 # remove leading spaces
2040 if _version in self.__character_sets_dict: 2026 if _version in self.__character_sets_dict:
2041 self.__character_set = self.__character_sets_dict[_version] 2027 self.__character_set = self.__character_sets_dict[_version]
2042 print utils.mapping(_("FIEBDC character encoding: $1"),(self.__character_set,)) 2028 interface.readFile_send_message(utils.mapping(
2029 _("FIEBDC character encoding: $1"),
2030 (self.__character_set,)))
2043 else: 2031 else:
2044 print utils.mapping(_("This Character encoding do not exist in "\ 2032 interface.readFile_send_message(utils.mapping(
2045 "FIEBDC3! Default Character encoding: $1"), 2033 _("This Character encoding do not exist in "\
2046 (self.__character_set,)) 2034 "FIEBDC3! Default Character encoding: $1"),
2035 (self.__character_set,)))
2047 else: 2036 else:
2048 print utils.mapping(_("This V record dot have a character encoding! "\ 2037 interface.readFile_send_message(utils.mapping(_(
2038 "This V record dot have a character encoding! "\
2049 "Default character encoding: $1"), 2039 "Default character encoding: $1"),
2050 (self.__character_set,)) 2040 (self.__character_set,)))
2051 else: 2041 else:
2052 print utils.mapping(_("Not 'V' record in File! Default character encoding: "\ 2042 interface.readFile_send_message(utils.mapping(_(
2053 "$1"), (self.__character_set,)) 2043 "Not 'V' record in File! Default character encoding: "\
2044 "$1"), (self.__character_set,)))
2054 _buffer = unicode(_buffer, self.__character_set) 2045 _buffer = unicode(_buffer, self.__character_set)
2055 # Any INFORMATION between the beginning of the file and the 2046 # Any INFORMATION between the beginning of the file and the
2056 # beginning of the first registry “~” is ignored 2047 # beginning of the first registry “~” is ignored
2057 #"after_first_tilde" : "^[^~]*~" 2048 #"after_first_tilde" : "^[^~]*~"
2058 _buffer = self.__pattern["after_first_tilde"].sub("",_buffer) 2049 _buffer = self.__pattern["after_first_tilde"].sub("",_buffer)
2078 _last_record = u"" 2069 _last_record = u""
2079 for record in _record_list: 2070 for record in _record_list:
2080 if self.__cancel: 2071 if self.__cancel:
2081 break 2072 break
2082 self.parseRecord(record) 2073 self.parseRecord(record)
2083 interface.progress(_file.tell() / _filesize) 2074 interface.readFile_progress(_file.tell() / _filesize)
2084 _buffer2 = _file.read(100000) 2075 _buffer2 = _file.read(100000)
2085 _buffer2 = unicode(_buffer2, self.__character_set) 2076 _buffer2 = unicode(_buffer2, self.__character_set)
2086 _buffer = _last_record + _buffer2 2077 _buffer = _last_record + _buffer2
2087 _file.close() 2078 _file.close()
2088 if self.__cancel: 2079 if self.__cancel:
2089 print _("Process terminated") 2080 interface.readFile_cancel()
2090 return None 2081 return None
2091 else: 2082 else:
2092 print utils.mapping(_("Time to load: $1 seconds"), 2083 self.__statistics.time = time.time()-_time
2093 (("%.2f" %(time.time()-_time)),)) 2084 if self.__statistics.O > 0:
2094 print utils.mapping(_("Records/Valid Records: $1/$2"), 2085 interface.readFile_send_message(
2095 (self._record_number, self.num_valid_record)) 2086 utils.mapping(_("$1 unsuported record type O: "\
2096 if self._record_O_number > 0: 2087 "Comercial Relationship"), (self.__statistics.O,)))
2097 print utils.mapping(_("$1 unsuported record type O: "\ 2088 if self.__statistics.valid == 0:
2098 "Comercial Relationship"), (self._record_O_number,)) 2089 interface.readFile_send_message(_("This file is not a valid FIBDC3 file"))
2099 if self.num_valid_record == 0:
2100 print _("This file is not a valid FIBDC3 file")
2101 return None 2090 return None
2102 _str = "" 2091 interface.readFile_end()
2103 for type in \
2104 [("V", self._record_V_number),
2105 ("C", self._record_C_number),
2106 ("D", self._record_D_number),
2107 ("Y", self._record_Y_number),
2108 ("M", self._record_M_number),
2109 ("N", self._record_N_number),
2110 ("T", self._record_T_number),
2111 ("K", self._record_K_number),
2112 ("W", self._record_W_number),
2113 ("L", self._record_L_number),
2114 ("Q", self._record_Q_number),
2115 ("J", self._record_J_number),
2116 ("G", self._record_G_number),
2117 ("E", self._record_E_number),
2118 ("O", self._record_O_number),
2119 ("P", self._record_P_number),
2120 ("X", self._record_X_number),
2121 ("B", self._record_B_number),
2122 ("F", self._record_F_number),
2123 ("A", self._record_A_number),
2124 ("?", self._record_Unknow_number)]:
2125 _str = _str + "%s: %s\n" %(type[0], type[1])
2126 print _str
2127 self._testBudget(self.__budget) 2092 self._testBudget(self.__budget)
2128 return self.__budget 2093 return self.__budget
2129 2094
2130 def _testBudget(self, budget): 2095 def _testBudget(self, budget):
2131 """testBudget(self,budget) 2096 """testBudget(self,budget)
2162 def delete_control(self, text): 2127 def delete_control(self, text):
2163 text = text.replace(u"\t", u"") 2128 text = text.replace(u"\t", u"")
2164 text = text.replace(u"\r", u"") 2129 text = text.replace(u"\r", u"")
2165 text = text.replace(u"\n", u"") 2130 text = text.replace(u"\n", u"")
2166 return text 2131 return text
2132
2133 class Interface(object):
2134 """fiebdc.Interface
2135
2136 Description:
2137 An example interface
2138 Constructor:
2139 fiebdc.Interface()
2140 Ancestry:
2141 +-- object
2142 +-- Interface
2143 Atributes:
2144 "__progress": The progress percentage
2145 "__statistics": The record statistics
2146 Methods:
2147 __init__(self)
2148 readFile_send_message(message)
2149 readFile_progress(percent)
2150 readFile_set_statistics(statistics)
2151 readFile_end()
2152 readFile_cancel()
2153
2154 """
2155 def __init__(self):
2156 self.__progress = 0.0
2157 self.__statistics = Statistics()
2158
2159 def readFile_set_statistics(self, statistics):
2160 """readFile_set_statistics(statistics)
2161
2162 statistics: record statistics from readFile method
2163
2164 sets record statistics
2165 """
2166 self.__statistics = statistics
2167
2168 def readFile_send_message(self, message):
2169 """readFile_send_message(message)
2170
2171 message: mesage from readFile method
2172
2173 print message
2174 """
2175 print message
2176
2177 def readFile_progress(self, percent):
2178 """progress(percent)
2179
2180 percent: Percentage executed.
2181
2182 Sets progress
2183 """
2184 self.__progress = percent
2185
2186 def readFile_end(self):
2187 """readFile_end()
2188
2189 The readFile method end successfully
2190 """
2191 print self.__statistics
2192
2193 def readFile_cancel(self):
2194 """readFile_cancel()
2195
2196 The readFile method is canceled
2197 """
2198 print _("Process terminated")
2199
2200 class Statistics(object):
2201 """fiebdc.Statistics
2202
2203 Description:
2204 BC3 Statistics. Records types.
2205 Constructor:
2206 fiebdc.Statistics()
2207 Ancestry:
2208 +-- object
2209 +-- Statistics
2210 Atributes:
2211 "records": number of records
2212 "valid": number of valid records
2213 "V": number of V records
2214 "C": number of C records
2215 "D":number of D records
2216 "Y":number of Y records
2217 "M":number of M records
2218 "N":number of N records
2219 "T":number of T records
2220 "K":number of K records
2221 "W":number of W records
2222 "L":number of L records
2223 "Q":number of Q records
2224 "J": number of J records
2225 "G":number of G records
2226 "E":number of E records
2227 "O":number of O records
2228 "P":number of P records
2229 "X":number of X records
2230 "B":number of B records
2231 "F":number of F records
2232 "A":number of A records
2233 "unknow": number of Unknow records
2234 "time": Time to load
2235
2236 Methods:
2237 __init__(self)
2238
2239 """
2240 def __init__(self):
2241 self.records = 0
2242 self.valid = 0
2243 self.V = 0
2244 self.C = 0
2245 self.D = 0
2246 self.Y = 0
2247 self.M = 0
2248 self.N = 0
2249 self.T = 0
2250 self.K = 0
2251 self.W = 0
2252 self.L = 0
2253 self.Q = 0
2254 self.J = 0
2255 self.G = 0
2256 self.E = 0
2257 self.O = 0
2258 self.P = 0
2259 self.X = 0
2260 self.B = 0
2261 self.F = 0
2262 self.A = 0
2263 self.unknow = 0
2264 self.time = 0.0
2265
2266 def __str__(self):
2267 return utils.mapping(_("Time to load: $1 seconds"),
2268 (("%.2f" %(self.time)),)) + "\n" + \
2269 utils.mapping(_("Records/Valid Records: $1/$2"),
2270 (self.records, self.valid)) + "\n" +\
2271 "V: %s\n" %(self.V,) + \
2272 "C: %s\n" %(self.C,) + \
2273 "D: %s\n" %(self.D,) + \
2274 "Y: %s\n" %(self.Y,) + \
2275 "M: %s\n" %(self.M,) + \
2276 "N: %s\n" %(self.N,) + \
2277 "T: %s\n" %(self.T,) + \
2278 "K: %s\n" %(self.K,) + \
2279 "W: %s\n" %(self.W,) + \
2280 "L: %s\n" %(self.L,) + \
2281 "Q: %s\n" %(self.Q,) + \
2282 "J: %s\n" %(self.J,) + \
2283 "G: %s\n" %(self.G,) + \
2284 "E: %s\n" %(self.E,) + \
2285 "O: %s\n" %(self.O,) + \
2286 "P: %s\n" %(self.P,) + \
2287 "X: %s\n" %(self.X,) + \
2288 "B: %s\n" %(self.B,) + \
2289 "F: %s\n" %(self.F,) + \
2290 "A: %s\n" %(self.A,) + \
2291 "?: %s\n" %(self.unknow,)
2292