Mercurial > pyarq-presupuestos
diff Generic/fiebdc.py @ 26:16f91684686b default tip
Upgrade to python 3. Keep python 2/3 compatibility
author | Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es> |
---|---|
date | Tue, 18 Jun 2019 17:50:23 +0200 |
parents | 189f8274aecd |
children |
line wrap: on
line diff
--- a/Generic/fiebdc.py Mon May 20 13:18:33 2019 +0200 +++ b/Generic/fiebdc.py Tue Jun 18 17:50:23 2019 +0200 @@ -22,6 +22,13 @@ # specifications in http://www.fiebdc.org # Modules + +# python 2/3 compatibility +from __future__ import absolute_import, division, print_function, unicode_literals +from builtins import str as text +from six import text_type +from io import open + import time import re import calendar @@ -29,7 +36,6 @@ import unicodedata import hashlib # pyArq-Presupuestos modules -import base from Generic import utils from Generic import globalVars @@ -93,26 +99,26 @@ "437" : "cp437"} self.__file_format = "FIEBDC-3/2007" self.__generator = globalVars.name + " " + globalVars.version - self.__character_set = "850" + self.__character_set = "850" # ¿set default character set to cp1252? self.__pattern = { - "control_tilde" : re.compile(u"((\r\n)| |\t)+~"), - "control_vbar" : re.compile(u"((\r\n)| |\t)+\|"), - "control_backslash" : re.compile(ur"((\r\n)| |\t)+\\"), - "valid_code" : re.compile(u"[^A-Za-z0-9ñÑ.$#%&_]"), - "special_char": re.compile(u"[#%&]"), - "no_float": re.compile(u"[^\-0-9.]"), - "formula" : re.compile(u".*[^0123456789\.()\+\-\*/\^abcdp ].*"), - "comment": re.compile(u"#.*\r\n"), - "empty_line": re.compile(ur"(\r\n) *\r\n"), - "space_before_backslash" : re.compile(ur"( )+\\"), - "space_after_backslash" : re.compile(ur"\\( )+"), - "start_noend_backslash" : re.compile(u"(\r\n\\\.*[^\\\])\r\n"), - "end_oper": re.compile(u"(\+|-|\*|/|/^|@|&|<|>|<=|>=|=|!) *\r\n"), - "matricial_var" : re.compile(u"(\r\n *[%|\$][A-ZÑ].*=.*,) *\r\n"), - "descomposition" : re.compile(u"^([^:]+):(.*)$"), - "var" : re.compile(u"^([$%][A-ZÑ][()0-9, ]*)=(.*)$"), - "after_first_tilde" : re.compile(u"^[^~]*~"), - "end_control" : re.compile(u"((\r\n)| |\t)+$"), + "control_tilde" : re.compile("((\r\n)| |\t)+~"), + "control_vbar" : re.compile("((\r\n)| |\t)+\|"), + "control_backslash" : re.compile(r"((\r\n)| |\t)+\\"), + "valid_code" : re.compile("[^A-Za-z0-9ñÑ.$#%&_]"), + "special_char": re.compile("[#%&]"), + "no_float": re.compile("[^\-0-9.]"), + "formula" : re.compile(".*[^0123456789\.()\+\-\*/\^abcdp ].*"), + "comment": re.compile("#.*\r\n"), + "empty_line": re.compile(r"(\r\n) *\r\n"), + "space_before_backslash" : re.compile(r"( )+\\"), + "space_after_backslash" : re.compile(r"\\( )+"), + "start_noend_backslash" : re.compile("(\r\n\\\.*[^\\\])\r\n"), + "end_oper": re.compile("(\+|-|\*|/|/^|@|&|<|>|<=|>=|=|!) *\r\n"), + "matricial_var" : re.compile("(\r\n *[%|\$][A-ZÑ].*=.*,) *\r\n"), + "descomposition" : re.compile("^([^:]+):(.*)$"), + "var" : re.compile("^([$%][A-ZÑ][()0-9, ]*)=(.*)$"), + "after_first_tilde" : re.compile("^[^~]*~"), + "end_control" : re.compile("((\r\n)| |\t)+$"), } self.__statistics = Statistics() @@ -133,9 +139,9 @@ record ~P """ # "control_tilde" : "((\r\n)| |\t)+~" - string = self.__pattern["control_tilde"].sub(u"~",string) + string = self.__pattern["control_tilde"].sub("~",string) # "control_vbar" : "((\r\n)| |\t)+\|" - string = self.__pattern["control_vbar"].sub(u"|",string) + string = self.__pattern["control_vbar"].sub("|",string) # "control_backslash" : r"((\r\n)| |\t)+\\" #string = self.__pattern["control_backslash"].sub(r"\\",string) return string @@ -146,23 +152,24 @@ Test if the code have invalid characters and try to erase it, if it is posible return a valid code else return a empty string. """ - if not isinstance(code, unicode): - _str = _("Invalid code, it must be a unicode string") - print(_str.encode("utf-8") ) - return u"" + if not isinstance(code, text_type): + _tuni = _("Invalid code, it must be a text string") + print(_tuni) + return "" # Valid chararcter: A-Z a-z 0-9 ñ Ñ . $ # % & _ # "valid_code" : "[^A-Za-z0-9ñÑ.$#%&_]" - _ucode = self.__pattern["valid_code"].sub(u"_", code) + _ucode = self.__pattern["valid_code"].sub("_", code) if _ucode != code: try: - _tstr = _("The code '$1' have invalid characters," \ + _tuni = _("The code '$1' have invalid characters," \ " replaced by '$2'.") - print(utils.mapping(_tstr, (code.encode("utf8"), - _ucode.encode("utf8"))) ) + _uni = utils.mapping(_tuni, (code, _ucode)) + print(_uni) except: - _tstr = _("The code '$1' have invalid characters and can not" \ - " be encoded in utf8.") - print(utils.mapping(_tstr, (code,)).encode("utf-8") ) + _tuni = _("The code '$1' have invalid characters and can not" \ + " be encoded in utf-8.") + _uni = utils.mapping(_tuni, (code,)) + print(_uni) if len(_ucode) == 0: _normalize_code = "" @@ -173,22 +180,22 @@ # 'NFD', _ucode) if unicodedata.category(c) != 'Mn')) # from http://www.leccionespracticas.com/uncategorized/ # eliminar-tildes-con-python-solucionado/ - _ucode = self.__pattern["valid_code"].sub(u"", _normalize_code) + _ucode = self.__pattern["valid_code"].sub("", _normalize_code) if len(_ucode) == 0: _hash_code = hashlib.sha256() _hash_code.update(code.encode('utf-8')) _hexdigest_code = _hash_code.hexdigest() _p_valid_code = self.__pattern["valid_code"] - _ucode = _p_valid_code.sub(u"", _hexdigest_code) + _ucode = _p_valid_code.sub("", _hexdigest_code) code = _ucode - if code == u"##": + if code == "##": # root code is an empty code : set to ROOT - return u"ROOT" + return "ROOT" # the lasts characters can not be <#> or <##> # <##> -> root record in FIEFDC-3 # <#> -> chapter record in FIEFDC-3 if len(code) > 0: - while code[-1] == u"#": + while code[-1] == "#": code = code[:-1] if len(code) == 0: return code @@ -196,12 +203,12 @@ code = code[:20] # only one charecter # % or & if sum([code.count(c) for c in u'#%&']) > 1: - print(utils.mapping(_("The code '$1' contains special "\ - "characters repeated."), - (code.encode("utf8"),)).encode("utf-8") ) + _tuni = _("The code '$1' contains special characters repeated.") + _uni = utils.mapping(_tuni, (code,)) + print(_uni) _i = min([code.find(c) for c in u'#%&']) code = code[:_i+1] + \ - self.__pattern["special_char"].sub(u"", code[_i+1:]) + self.__pattern["special_char"].sub("", code[_i+1:]) return code def parseDate(self, date): @@ -217,11 +224,11 @@ or None if the date format is invalid """ # All characters must be numbers, len <= 8 and not empty string - if not date.isdigit() or len(date) > 8 or date == u"": + if not date.isdigit() or len(date) > 8 or date == "": return None else: if len(date)%2 == 1: # uneven len: add a leading 0 - date = u"0" + date + date = "0" + date if len(date) == 8: _d = int(date[:2]) _m = int(date[2:4]) @@ -251,86 +258,86 @@ """parseRecord(record, interface) record: the record line readed from the file whith the format: - type|field|field|subfield\subfield|... + type|field|field|subfield\\subfield|... [a] nothing or "a" {a} zero or more #-#twice#-# "a" <a> one or more #-#twice#-# "a" Types: V C D Y M N T K L Q J G E X B F A V: Property and Version 1- [File_Owner] - 2- Format_Version[\DDMMYYYY] + 2- Format_Version[\\DDMMYYYY] 3- [Program_Generator] - 4- [Header]\{Title\} + 4- [Header]\\{Title\\} 5- [Chaters_set] 6- [Comment] C: Record: - 1- Code{\Code} + 1- Code{\\Code} 2- [Unit] 3- [Summary] - 4- {Price\} - 5- {Date\} + 4- {Price\\} + 5- {Date\\} 6- [Type] D or Y: DECOMPOSITION or ADD DECOMPOSITION 1- Parent Code - 2- <Child Code\ [Factor]\ [Yield]> + 2- <Child Code\\ [Factor]\\ [Yield]> M or N: MEASURE or ADD MEASURE - 1- [Parent Code\]Child Code + 1- [Parent Code\\]Child Code 2- {Path\} 3- TOTAL MEASURE - 4- {Type\Comment\Unit\Length\Width\Height\} + 4- {Type\\Comment\\Unit\\Length\\Width\\Height\\} 5- [Label] T: Text 1- Code 2- Description text K: Coefficients - 1- { DN \ DD \ DS \ DR \ DI \ DP \ DC \ DM \ DIVISA \ } - 2- CI \ GG \ BI \ BAJA \ IVA - 3- { DRC \ DC \ DRO \ DFS \ DRS \ DFO \ DUO \ DI \ DES \ DN \ - DD \ DS \ DIVISA \ } + 1- { DN \\ DD \\ DS \\ DR \\ DI \\ DP \\ DC \\ DM \\ DIVISA \\ } + 2- CI \\ GG \\ BI \\ BAJA \\ IVA + 3- { DRC \\ DC \\ DRO \\ DFS \\ DRS \\ DFO \\ DUO \\ DI \\ DES \\ DN \\ + DD \\ DS \\ DIVISA \\ } 4- [ n ] L: Sheet of Conditions 1 A) 1- Empty - 2- {Section Code\Section Title} + 2- {Section Code\\Section Title} B) 1- Record Code - 2- {Section Code\Section Text} - 3- {Section Code\RTF file} - 4- {Section Code\HTM file} + 2- {Section Code\\Section Text} + 3- {Section Code\\RTF file} + 4- {Section Code\\HTM file} Q: Sheet of Conditions 2 1- Record Code - 2- {Section Code\Paragraph key\{Field key;}\}| + 2- {Section Code\\Paragraph key\\{Field key;}\\}| J: Sheet of Conditions 3 1- Paragraph code 2- [Paragraph text] 3- [RTF file] 4- [HTML file] G: Grafic info - 1- <grafic_file.ext\> + 1- <grafic_file.ext\\> E: Company 1- company Code 2 [ summary ] 3- [ name ] - 4- { [ type ] \ [ subname ] \ [ address ] \ [ postal_code ] - \ [ town ] \ [ province ] \ [ country ] \ { phone; } - \ { fax; } \ {contact_person; } \ } - 5- [ cif ] \ [ web ] \ [ email ] \ + 4- { [ type ] \\ [ subname ] \\ [ address ] \\ [ postal_code ] + \\ [ town ] \\ [ province ] \\ [ country ] \\ { phone; } + \\ { fax; } \\ {contact_person; } \\ } + 5- [ cif ] \\ [ web ] \\ [ email ] \\ X: Tecnical information A) 1- Empty - 2- < TI_Code \ TI_Descitption \ TI_Unit > + 2- < TI_Code \\ TI_Descitption \\ TI_Unit > B) 1- Record_code - 2- < TI_Code \ TI_value > + 2- < TI_Code \\ TI_value > F: #-#Adjunto#-# File 1- Record code - 2- { Type \ { Filenames; } \ [Description] } + 2- { Type \\ { Filenames; } \\ [Description] } B: Change code 1- Record Code 2- New code A: Labels 1- Record Code - 2- <Label\> + 2- <Label\\> interface: """ # TODO: ~L ~J RTF and HTML files @@ -338,71 +345,72 @@ # TODO: ~P. Registro tipo Descripción Paramétrica. # TODO: ~O. Registro tipo Relación Comercial. # TODO: test records - _field_list = record.split(u"|") + _field_list = record.split("|") self.__statistics.records = self.__statistics.records +1 _budget = self.__budget - if _field_list[0] == u"V": + if _field_list[0] == "V": self.__statistics.V += 1 self._parseV(_field_list) - elif _field_list[0] == u"C": + elif _field_list[0] == "C": self.__statistics.C += 1 self._parseC(_field_list, interface) - elif _field_list[0] == u"D": + elif _field_list[0] == "D": self.__statistics.D += 1 self._parseDY(_field_list, interface) - elif _field_list[0] == u"Y": + elif _field_list[0] == "Y": self.__statistics.Y += 1 self._parseDY(_field_list, interface) - elif _field_list[0] == u"M": + elif _field_list[0] == "M": self.__statistics.M += 1 self._parseMN(_field_list) - elif _field_list[0] == u"N": + elif _field_list[0] == "N": self.__statistics.N += 1 self._parseMN(_field_list) - elif _field_list[0] == u"T": + elif _field_list[0] == "T": self.__statistics.T += 1 self._parseT(_field_list) - elif _field_list[0] == u"K": + elif _field_list[0] == "K": self.__statistics.K += 1 self._parseK(_field_list) - elif _field_list[0] == u"W": + elif _field_list[0] == "W": self.__statistics.W += 1 self._parseW(_field_list) - elif _field_list[0] == u"L": + elif _field_list[0] == "L": self.__statistics.L += 1 self._parseL(_field_list) - elif _field_list[0] == u"Q": + elif _field_list[0] == "Q": self.__statistics.Q += 1 self._parseQ(_field_list) - elif _field_list[0] == u"J": + elif _field_list[0] == "J": self.__statistics.J += 1 self._parseJ(_field_list) - elif _field_list[0] == u"G": + elif _field_list[0] == "G": self.__statistics.G += 1 self._parseG(_field_list) - elif _field_list[0] == u"E": + elif _field_list[0] == "E": self.__statistics.E += 1 self._parseE(_field_list) elif _field_list[0] == "O": self.__statistics.O += 1 - elif _field_list[0] == u"P": + elif _field_list[0] == "P": self.__statistics.P += 1 self._parseP(_field_list) - elif _field_list[0] == u"X": + elif _field_list[0] == "X": self.__statistics.X += 1 self._parseX(_field_list) - elif _field_list[0] == u"B": + elif _field_list[0] == "B": self.__statistics.B += 1 self._parseB(_field_list) - elif _field_list[0] == u"F": + elif _field_list[0] == "F": self.__statistics.F += 1 self._parseF(_field_list) - elif _field_list[0] == u"A": + elif _field_list[0] == "A": self.__statistics.A += 1 self._parseA(_field_list) else: - print(utils.mapping(_("FIEBDC. Unknow record: $1"), - (record[:100],)).encode("utf-8")) + _tuni = _("FIEBDC. Unknow record: $1") + _uni = utils.mapping(_tuni, (record[:100],)) + print(_uni) self.__statistics.unknow += 1 def _parseV(self, field_list): @@ -421,12 +429,14 @@ 9- [Date budget certificate] """ if self.__statistics.records != 1: - print(utils.mapping(_("The 'V' record (Property and Version) "\ + _tuni = _("The 'V' record (Property and Version) "\ "must be the first record in the file but it is the "\ - "number: $1"), - (str(self.__statistics.records),)).encode("utf-8") ) - print(_("The default values were taken and this V record is "\ - "ignored").encode("utf-8") ) + "number: $1") + _uni = utils.mapping(_tuni, (text(self.__statistics.records),)) + print(_uni) + _tuni = _("The default values were taken and this V record is "\ + "ignored") + print(_tuni) return # _____number of fields_____ # Any INFORMATION after last field separator is ignored @@ -435,7 +445,7 @@ # If there are no sufficient fields, the fields are added # with empty value:"" else: - field_list = field_list + [u""]*(10-len(field_list)) + field_list = field_list + [""]*(10-len(field_list)) # control character are erased: end of line, tab, space # only leading and trailing whitespace in owner, generator, comment # _____Fields_____ @@ -448,47 +458,47 @@ _header_title = field_list[4].strip() _header_title = self.delete_control(_header_title) _character_set = self.delete_control_space(field_list[5]) - _comment = field_list[6].strip(u"\t \n\r") + _comment = field_list[6].strip("\t \n\r") _data_type = self.delete_control_space(field_list[7]) _number_certificate = self.delete_control_space(field_list[8]) __date_certificate = self.delete_control_space(field_list[9]) # _____Owner_____ self.__budget.setOwner(_owner) # _____Version-Date_____ - _version_date = _version_date.split(u"\\") + _version_date = _version_date.split("\\") _file_format = _version_date[0] if _file_format in self.__format_list: self.__file_format = _file_format - print(utils.mapping(_("FIEBDC format: $1"), - (_file_format,)).encode("utf-8") ) + _tuni = _("FIEBDC format: $1") + _uni = utils.mapping(_tuni, (_file_format,)) + print(_uni) if len(_version_date) > 1: _date = _version_date[1] - if _date != u"": + if _date != "": _parsed_date = self.parseDate(_date) if _parsed_date is not None: self.__budget.setDate(_parsed_date) # _____Generator_____ # ignored field - print(utils.mapping(_("FIEBDC file generated by $1"), - (_generator,)).encode("utf-8") ) + _tuni = _("FIEBDC file generated by $1") + _uni = utils.mapping(_tuni, (_generator,)) + print(_uni) # _____Header_Title_____ - _header_title = _header_title.split(u"\\") + _header_title = _header_title.split("\\") _header_title = [_title.strip() for _title in _header_title] _header = _header_title.pop(0) - _header = [_item.encode("utf8") for _item in _header] _title = [ ] for _title_index in _header_title: - if _title_index != u"": + if _title_index != "": _title.append(_title_index) - _title = [_item.encode("utf8") for _item in _title] - if _header != u"": + if _header != "": self.__budget.setTitleList([ _header, _title]) # _____Characters_set_____ # field parsed in readFile method # _____Comment_____ - if _comment != u"": - self.__budget.setComment(_comment.encode("utf8")) + if _comment != "": + self.__budget.setComment(_comment) # _____Data type_____ # 1 -> Base data. # 2 -> Budget. @@ -547,16 +557,16 @@ _field1 = self.delete_control_space(field_list[1]) _field2 = self.delete_control_space(field_list[2]) # _____Field 1_____ - if len(_field1) > 0 and _field1[-1] == u"\\": + if len(_field1) > 0 and _field1[-1] == "\\": _field1 = _field1[:-1] # if there are a \ character at the end it must be erased - _percentages = _field1.split(u"\\") + _percentages = _field1.split("\\") if len(_percentages) > 5: _percentages = _percentages[:5] # If there are no sufficient subfields, the subfields are added # with empty value:"" else: - _percentages = _percentages + [u""]*(5-len(_percentages)) + _percentages = _percentages + [""]*(5-len(_percentages)) _percentage_titles = [ "CI", "GG", "BI", "BAJA", "IVA" ] _percentage_dict = {} for _percentage_index in range(len(_percentages)): @@ -573,12 +583,12 @@ _title_num = len(self.__budget.getTitleList()[1]) if _title_num == 0: _title_num = 1 # If the field 2 is empty, the field 0 is readed - if _field2 == u"": + if _field2 == "": # _____Field 0_____ - if _field0[-1] == u"\\": + if _field0[-1] == "\\": _field0 = _field0[:-1] # if there are a \ character at the end it must be erased - _decimal_list = _field0.split(u"\\") + _decimal_list = _field0.split("\\") _decimal_index = 0 if len(_decimal_list)%9 != 0: # if it is not multiple of 9, empty subfield are added @@ -779,7 +789,7 @@ # If there are no sufficient fields, the fields are added # with empty value:"" else: - field_list = field_list + [u""]*(7-len(field_list)) + field_list = field_list + [""]*(7-len(field_list)) # control character are erased: en of line, tab, space # _____Fields_____ _record_type = field_list[0] @@ -790,13 +800,13 @@ _dates = self.delete_control_space(field_list[5]) _type = self.delete_control_space(field_list[6]) # _____Code_____ - _codes = _codes.split(u"\\") + _codes = _codes.split("\\") if len(_codes) > 0: # parse the hierarchy of the first code # hierarchy: 0->root, 1->Chapter/subchapter, 2->other - if len(_codes[0]) > 1 and _codes[0][-2:] == u"##": + if len(_codes[0]) > 1 and _codes[0][-2:] == "##": _hierarchy = 0 - elif len(_codes[0]) > 0 and _codes[0][-1:] == u"#": + elif len(_codes[0]) > 0 and _codes[0][-1:] == "#": _hierarchy = 1 else: _hierarchy = 2 @@ -805,14 +815,15 @@ # maximun len 20 characters _codes = [self.validateCode(_code) for _code in _codes] # empty codes are ignored - while u"" in _codes: - _codes.remove(u"") + while "" in _codes: + _codes.remove("") if len(_codes) > 0: #TODO: test this _code = _codes[0] - _synonyms = [synonym.encode("utf8") for synonym in _codes] + _synonyms = [synonym for synonym in _codes] else: - print(_("Record C without a valid code").encode("utf-8") ) + _tuni = _("Record C without a valid code") + print(_tuni) return # _____Unit_____ # nothing to do @@ -820,13 +831,13 @@ # nothing to do # _____Price_____ and _____Dates_____ # last \ is erased - if len(_dates) > 0 and _dates[-1] == u"\\": + if len(_dates) > 0 and _dates[-1] == "\\": _dates = _dates[:-1] - if len(_prices) > 0 and _prices[-1] == u"\\": + if len(_prices) > 0 and _prices[-1] == "\\": _prices = _prices[:-1] interface.updateGui() - _dates = _dates.split(u"\\") - _prices = _prices.split(u"\\") + _dates = _dates.split("\\") + _prices = _prices.split("\\") # number of prices = number of titles in "V" line # if there are no sufficient prices it takes the last price defined _title_num = len(self.__budget.getTitleList()[1]) @@ -883,63 +894,63 @@ # 3 -> None,MC,MCr,MM,MS,ME,MCu,Mal,ML,M interface.updateGui() if _hierarchy == 0: - if _type == u"OB": + if _type == "OB": _subtype = _type _type = 0 - elif _type == u"0" or _type == u"": - _subtype = u"" + elif _type == "0" or _type == "": + _subtype = "" _type = 0 else: - print(utils.mapping(_("Incorrect type ($1) in the code $2"), - (_type.encode("utf8"), - _code.encode("utf8"))).encode("utf-8") ) + _tuni = _("Incorrect type ($1) in the code $2") + _uni = utils.mapping(_tuni, (_type, _code)) + print(_uni) _type = 0 - _subtype = u"" + _subtype = "" elif _hierarchy == 1: - if _type == u"PU": + if _type == "PU": _subtype = _type _type = 0 - elif _type == u"0" or _type == u"": - _subtype = u"" + elif _type == "0" or _type == "": + _subtype = "" _type = 0 else: - print(utils.mapping(_("Incorrect type ($1) in the code $2"), - (_type.encode("utf8"), - _code.encode("utf8"))).encode("utf-8") ) + _tuni = _("Incorrect type ($1) in the code $2") + _uni = utils.mapping(_tuni, (_type, _code)) + print(_uni) _type = 0 - _subtype = u"" + _subtype = "" else: - if _type == u"EA" or _type == u"EU" or _type == u"EC" or \ - _type == u"EF" or _type == u"PA": + if _type == "EA" or _type == "EU" or _type == "EC" or \ + _type == "EF" or _type == "PA": _subtype = _type _type = 0 - elif _type == u"H": + elif _type == "H": _subtype = _type _type = 1 - elif _type == u"Q" or _type == u"%": + elif _type == "Q" or _type == "%": _subtype = _type _type = 2 - elif _type == u"MC" or _type == u"MCr" or _type == u"MM" or \ - _type == u"MS" or _type == u"ME" or _type == u"MCu" or \ - _type == u"Mal" or _type == u"ML" or _type == u"M": + elif _type == "MC" or _type == "MCr" or _type == "MM" or \ + _type == "MS" or _type == "ME" or _type == "MCu" or \ + _type == "Mal" or _type == "ML" or _type == "M": _subtype = _type _type = 3 - elif _type == u"0" or _type == u"1" or _type == u"2" or \ - _type == u"3": - _subtype = u"" + elif _type == "0" or _type == "1" or _type == "2" or \ + _type == "3": + _subtype = "" _type = int(_type) - elif _type == u"": - _subtype = u"" + elif _type == "": + _subtype = "" _type = 0 else: - print(utils.mapping(_("Incorrect type ($1) in the code $2"), - (_type.encode("utf8"), - _code.encode("utf8"))).encode("utf-8") ) + _tuni = _("Incorrect type ($1) in the code $2") + _uni = utils.mapping(_tuni, (_type, _code)) + print(_uni) _type = 0 - _subtype = u"" - self.__budget.setRecord(_code.encode("utf8"), _synonyms, _hierarchy, - _unit.encode("utf8"), _summary.encode("utf8"), - _prices, _dates, _type, _subtype.encode("utf8")) + _subtype = "" + self.__budget.setRecord(_code, _synonyms, _hierarchy, + _unit, _summary, + _prices, _dates, _type, _subtype) self.__statistics.valid = self.__statistics.valid + 1 def _parseDY(self, field_list, interface): @@ -958,7 +969,7 @@ # If there are no sufficient fields, the fields are added # with empty value:"" else: - field_list = field_list + [u""]*(3-len(field_list)) + field_list = field_list + [""]*(3-len(field_list)) # control character are erased: end of line, tab, space # _____Fields_____ _record_type = field_list[0] @@ -971,7 +982,7 @@ _code = self.validateCode(_code) # _____children_____ # TODO: test the number of decimals in factor an yield values - _children = _children.split(u"\\") + _children = _children.split("\\") _children_list = [ ] _child_index = 0 interface.updateGui() @@ -983,37 +994,37 @@ # _____child_code_____ _child_code = self.validateCode(_child_code) # _____factor_____ - if _factor != u"": + if _factor != "": try: _factor = float(_factor) except ValueError: - print(utils.mapping(_("ValueError loadig the "\ + _tuni = _("ValueError loadig the "\ "descomposition of the record $1, the factor "\ "of the child $2 must be a float number and "\ - "can not be $3, seted default value 1.0"), - (_code.encode("utf8"), _child_code.encode("utf8"), - _factor.encode("utf8"))).encode("utf-8") ) + "can not be $3, seted default value 1.0") + _uni = utils.mapping(_tuni, (_code, _child_code, _factor)) + print(_uni) _factor = 1.0 #____yield___ - if _yield != u"": + if _yield != "": try: _yield = float(_yield) except ValueError: - print(utils.mapping(_("ValueError loading the "\ + _tuni = _("ValueError loading the "\ "descomposition of the record $1, the yield of "\ "the child $2, must be a float number and can"\ - "not be $3, seted default value 1.0"), - (_code.encode("utf8"), _child_code.encode("utf8"), - _factor.encode("utf8"))).encode("utf-8") ) + "not be $3, seted default value 1.0") + _uni = utils.mapping(_tuni, (_code, _child_code, _factor)) + print(_uni) _yield = 1.0 - if _child_code != u"" and _code != u"": + if _child_code != "" and _code != "": _children_list.append([_child_code, _factor, _yield ]) - if _record_type == u"D": - _position = _child_index / 3 + if _record_type == "D": + _position = _child_index // 3 else: #_record_type == "Y" _position = -1 - self.__budget.setTree(_code.encode("utf8"), - _child_code.encode("utf8"), _position, _factor, + self.__budget.setTree(_code, + _child_code, _position, _factor, _yield, "", "", "", "") _child_index = _child_index + 3 interface.updateGui() @@ -1044,7 +1055,7 @@ # invalid characters are also erased _code = self.validateCode(_code) # _____Text_____ - self.__budget.setText(_code.encode("utf8"), _text.encode("utf8")) + self.__budget.setText(_code, _text) self.__statistics.valid = self.__statistics.valid + 1 def _parseMN(self, field_list): @@ -1052,10 +1063,10 @@ field_list: field list of the record 0- M or N: MEASURE or ADD MEASURE - 1- [Parent Code\]Child Code - 2- {Path\} + 1- [Parent Code\\]Child Code + 2- {Path\\} 3- TOTAL MEASURE - 4- {Type\Comment\Unit\Length\Width\Height\} + 4- {Type\\Comment\\Unit\\Length\\Width\\Height\\} 5- [Label] """ # _____Number of fields_____ @@ -1066,7 +1077,7 @@ # If there are no sufficient fields, the fields are added # with empty value:"" else: - field_list = field_list + [u""]*(6-len(field_list)) + field_list = field_list + [""]*(6-len(field_list)) # control character are erased: end of line, tab, space # _____Fields_____ _record_type = field_list[0] @@ -1076,37 +1087,35 @@ _lines = self.delete_control(field_list[4]) _label = self.delete_control_space(field_list[5]) # _____Codes_____ - _code_list = _codes.split(u"\\") + _code_list = _codes.split("\\") # "#" and "##" characters at the end of the code are erased # invalid characters are also erased if len(_code_list) == 2: _parent_code = self.validateCode(_code_list[0]) - if _parent_code == u"": + if _parent_code == "": _parent_code = None - else: - _parent_code = _parent_code.encode("utf8") - _child_code = self.validateCode(_code_list[1]) + _child_code = self.validateCode(_code_list[1]) elif len(_code_list) == 1: - _child_code = self.validateCode(_code_list[0]) + _child_code = self.validateCode(_code_list[0]) _parent_code = None else: - print(utils.mapping(_("Invalid codes in $1 record, codes $2"), - (_record_type.encode("utf8"), - _codes.encode("utf8"))).encode("utf-8") ) + _tuni = _("Invalid codes in $1 record, codes $2") + _uni = utils.mapping(_tuni, (_record_type, _codes)) + print(_uni) return - if _child_code == u"": - print(utils.mapping(_("Empty child code in $1 record, codes: "\ - "$2"), (_record_type.encode("utf8"), - _codes.encode("utf8"))).encode("utf-8") ) + if _child_code == "": + _tuni = _("Empty child code in $1 record, codes: $2") + _uni = utils.mapping(_tuni, (_record_type, _codes)) + print(_uni) return if _parent_code == None: # Empty parent code. No-estructured measures. pass # _____Path_____ - _path_list = _path.split( u"\\" ) + _path_list = _path.split( "\\" ) if len(_path_list) > 0: - while len(_path_list) > 0 and _path_list[-1] == u"": + while len(_path_list) > 0 and _path_list[-1] == "": _path_list = _path_list[:-1] if len(_path_list) == 0: # Empty path. No-estructured measures. Path fixed to -2 @@ -1116,9 +1125,9 @@ try: _path = int(_path) except ValueError: - print(utils.mapping(_("Invalid path in $1 record, "\ - "codes $2"), (_record_type.encode("utf8"), - _codes.encode("utf8"))).encode("utf-8") ) + _tuni = _("Invalid path in $1 record, codes $2") + _uni = utils.mapping(_tuni, (_record_type, _codes)) + print(_uni) return if _path > 0: _path -= 1 @@ -1128,21 +1137,21 @@ try: _total = float(_total) except ValueError: - print(utils.mapping(_("Invalid Total Measure value in $1 "\ - "record, codes $2. Total fixed to 0."), - (_record_type.encode("utf8"), - _codes.encode("utf8"))).encode("utf-8") ) + _tuni = _("Invalid Total Measure value in $1 "\ + "record, codes $2. Total fixed to 0.") + _uni = utils.mapping(_tuni, (_record_type, _codes)) + print(_uni) _total = 0 # _____Measure lines_____ - _lines = _lines.split(u"\\") + _lines = _lines.split("\\") _line_index = 0 _line_list = [ ] while _line_index < len(_lines)-6: _linetype = _lines[_line_index] - if _linetype == u"": + if _linetype == "": _linetype = 0 - elif _linetype == u"1" or _linetype == u"2" or \ - _linetype == u"3": + elif _linetype == "1" or _linetype == "2" or \ + _linetype == "3": _linetype = int(_linetype) else: _linetype = 0 @@ -1150,47 +1159,48 @@ if _linetype == 3: # "formula": ".*[^0123456789\.()\+\-\*/\^abcdp ].*" if self.__pattern["formula"].match(_comment): - print(utils.mapping(_("The comment is not a formula or "\ - "its have invalid characters, in the $1 record, "\ - "codes $2"), (_record_type.encode("utf8"), - _codes.encode("utf8"))).encode("utf-8") ) + _tuni = _("The comment is not a formula or "\ + "its have invalid characters, in the $1 record, "\ + "codes $2") + _uni = utils.mapping(_tuni, (_record_type, _codes)) + print(_uni) return else: - _formula = _comment.encode("utf8") + _formula = _comment _comment = "" else: _formula = "" - _comment = _comment.encode("utf8") _units = _lines[_line_index + 2] - _units = self.__pattern["no_float"].sub(u"", _units) + _units = self.__pattern["no_float"].sub("", _units) _length = _lines[_line_index + 3] - _length = self.__pattern["no_float"].sub(u"", _length) + _length = self.__pattern["no_float"].sub("", _length) _width = _lines[_line_index + 4] - _width = self.__pattern["no_float"].sub(u"", _width) + _width = self.__pattern["no_float"].sub("", _width) _height = _lines[_line_index + 5] - _height = self.__pattern["no_float"].sub(u"", _height) + _height = self.__pattern["no_float"].sub("", _height) try: - if _units != u"": + if _units != "": _units = float(_units) - if _length != u"": _length = float(_length) - if _width != u"": _width = float(_width) - if _height != u"": _height = float(_height) + if _length != "": _length = float(_length) + if _width != "": _width = float(_width) + if _height != "": _height = float(_height) except ValueError: - print(utils.mapping(_("The measure values are not float "\ - "numbers, code $1"), - (_codes.encode("utf8"),)).encode("utf-8") ) + _tuni = _("The measure values are not float "\ + "numbers, code $1") + _uni = utils.mapping(_tuni, (_codes,)) + print(_uni) return # Prevent subfield units remains empty. - if (_units == u"" and (_length != u"" or _width != u"" - or _height != u"")): + if (_units == "" and (_length != "" or _width != "" + or _height != "")): _units = 1.0 _line_list.append([_linetype, _comment, _units, _length, _width, _height, _formula]) _line_index = _line_index + 6 - self.__budget.setTree(_parent_code, _child_code.encode("utf8"), _path, "", "", - _total, _line_list, _label.encode("utf8"), - _record_type.encode("utf8")) + self.__budget.setTree(_parent_code, _child_code, _path, "", "", + _total, _line_list, _label, + _record_type) self.__statistics.valid = self.__statistics.valid + 1 def _parseW(self, field_list): @@ -1212,9 +1222,9 @@ # _____Fields_____ _code_fields = field_list[0] # last \ is erased - if len(_code_fields) and _code_fields[-1] == u"\\": + if len(_code_fields) and _code_fields[-1] == "\\": _code_fields = _code_fields[:-1] - _code_fields = _code_fields.split(u"\\") + _code_fields = _code_fields.split("\\") _field_dict = {} _field_index = 0 while _field_index < len(_code_fields)-1: @@ -1226,10 +1236,8 @@ #"control": "[\t \n\r]" _field_code = self.delete_control_space(_field_code) # _____section_title_____ - if _field_code != u"": - _e_field_code = _field_code.encode("utf8") - _e_field_title = _field_title.encode("utf8") - _field_dict[_e_field_code] = _e_field_title + if _field_code != "": + _field_dict[_field_code] = _field_title _field_index = _field_index + 2 self.__budget.setSheetFields(_field_dict) self.__statistics.valid = self.__statistics.valid +1 @@ -1253,7 +1261,7 @@ if len(field_list) < 3: return _code = field_list[1] - if _code == u"": + if _code == "": # A: Section Titles # Any INFORMATION after last field separator is ignored # The record must have 3 fields @@ -1263,9 +1271,9 @@ # _____Fields_____ _section_codes = field_list[1] # last \ is erased - if len(_section_codes) and _section_codes[-1] == u"\\": + if len(_section_codes) and _section_codes[-1] == "\\": _section_codes = _section_codes[:-1] - _section_codes = _section_codes.split(u"\\") + _section_codes = _section_codes.split("\\") _section_dict = {} _section_index = 0 while _section_index < len(_section_codes)-1: @@ -1278,10 +1286,8 @@ _section_code = self.delete_control_space(_section_code) # _____section_title_____ _section_title = self.delete_control_space(_section_title) - if _section_code != u"": - _e_section_code = _section_code.encode("utf8") - _e_section_title = _section_title.encode("utf8") - _section_dict[_e_section_code] = _e_section_title + if _section_code != "": + _section_dict[_section_code] = _section_title _section_index = _section_index + 2 self.__budget.setSheetSections(_section_dict) self.__statistics.valid = self.__statistics.valid +1 @@ -1299,16 +1305,16 @@ # invalid characters are also erased _record_code = self.validateCode(_record_code) _scodes_text = field_list[1] - if _scodes_text == u"": + if _scodes_text == "": # TODO: rtf and html files - _str = "Html and rtf files not yet implemented in ~L record" - print(_str.encode("utf-8") ) + _uni = "Html and rtf files not yet implemented in ~L record" + print(_uni) else: # _____Section-code_Section-text_____ # last \ is erased - if len(_scodes_text) and _scodes_text[-1] == u"\\": + if len(_scodes_text) and _scodes_text[-1] == "\\": _scodes_text = _scodes_text[:-1] - _scodes_text = _scodes_text.split(u"\\") + _scodes_text = _scodes_text.split("\\") _paragraph_dict = {} _section_dict = {} _section_index = 0 @@ -1320,18 +1326,14 @@ # _____section_code_____ _section_code = self.delete_control_space(_section_code) # _____section_text_____ - if _section_code != u"" and _section_text != u"": + if _section_code != "" and _section_text != "": #-# paragraph #-# - _paragraph_code = _record_code + _section_code + u"*" - _e_paragraph_code = _paragraph_code.encode("utf8") - _e_section_text = _section_text.encode("utf8") - _paragraph_dict[_e_paragraph_code] = _e_section_text - _e_section_code = _section_code.encode("utf8") - _section_dict[_e_section_code] = _e_paragraph_code + _paragraph_code = _record_code + _section_code + "*" + _paragraph_dict[_paragraph_code] = _section_text + _section_dict[_section_code] = _paragraph_code _section_index = _section_index + 2 self.__budget.setSheetParagraphs(_paragraph_dict) - self.__budget.setSheetRecord(_record_code.encode("utf8"), "*", - _section_dict) + self.__budget.setSheetRecord(_record_code, "*", _section_dict) self.__statistics.valid = self.__statistics.valid +1 def _parseQ(self, field_list): @@ -1360,9 +1362,9 @@ _record_code = self.validateCode(_record_code) _scodes_pkey = field_list[1] # last \ is erased - if len(_scodes_pkey) and _scodes_pkey[-1] == u"\\": + if len(_scodes_pkey) and _scodes_pkey[-1] == "\\": _scodes_pkey = _scodes_pkey[:-1] - _scodes_pkey = _scodes_pkey.split(u"\\") + _scodes_pkey = _scodes_pkey.split("\\") _field_dict = {} _section_index = 0 while _section_index < len(_scodes_pkey) -1: @@ -1378,24 +1380,21 @@ # _____Fields keys_____ _field_keys = self.delete_control_space(_field_keys) # last ; is erased - if len(_field_keys) and _field_keys[-1] == u";": + if len(_field_keys) and _field_keys[-1] == ";": _field_keys = _field_keys[:-1] - _field_keys_list = _scodes_pkey.split(u";") + _field_keys_list = _scodes_pkey.split(";") for _field_key in _field_keys_list: - if _field_key != u"" and _section_code != u"" and \ - _paragraph_key != u"": + if _field_key != "" and _section_code != "" and \ + _paragraph_key != "": if _field_key in _field_dict: _section_dict = _field_dict[_field_key] else: _section_dict = {} _field_dict[_field_key] = _section_dict - _e_section_code = _section_code.encode("utf8") - _e_paragraph_code = _paragraph_code.encode("utf8") - _section_dict[_e_section_code] = _e_paragraph_code + _section_dict[_section_code] = _paragraph_code _section_index = _section_index + 3 - for _field, _section_dict in _field_dict.iteritems(): - self.__budget.setSheetRecord(_record_code.encode("utf8"), - _field.encode("utf8"), _section_dict) + for _field, _section_dict in _field_dict.items(): + self.__budget.setSheetRecord(_record_code, _field, _section_dict) self.__statistics.valid = self.__statistics.valid +1 def _parseJ(self, field_list): @@ -1422,13 +1421,12 @@ _paragraph_code = self.delete_control_space(field_list[0]) # _____Paragraph text_____ _paragraph_text = field_list[1] - if _paragraph_text == u"": + if _paragraph_text == "": # TODO: rtf and html files - _str = "Html and rtf files not yet implemented in ~J record" - print(_str.encode("utf-8") ) + _uni = "Html and rtf files not yet implemented in ~J record" + print(_uni) else: - self.__budget.setSheetParagraph(paragraph_code.encode("utf8"), - paragraph_text.encode("utf8")) + self.__budget.setSheetParagraph(paragraph_code, paragraph_text) self.__statistics.valid = self.__statistics.valid +1 def _parseG(self, field_list): @@ -1458,18 +1456,17 @@ _grafic_files = self.delete_control(field_list[1]) # _____subfields_____ # last \ is erased - if len(_grafic_files) and _grafic_files[-1] == u"\\": + if len(_grafic_files) and _grafic_files[-1] == "\\": _grafic_files = _grafic_files[:-1] - _grafic_file_list = _grafic_files.split(u"\\") + _grafic_file_list = _grafic_files.split("\\") _tested_grafic_file_list = [] for _grafic_file in _grafic_file_list: - _str_grafic_file = _grafic_file.encode("utf8") - _path = os.path.dirname(self.__filename).encode("utf8") - _grafic_file_path = os.path.join(_path, _str_grafic_file) + _path = os.path.dirname(self.__filename) + _grafic_file_path = os.path.join(_path, _grafic_file) if os.path.exists(_grafic_file_path): _tested_grafic_file_list.append(_grafic_file_path) else: - _name_ext = os.path.splitext(_str_grafic_file) + _name_ext = os.path.splitext(_grafic_file) _grafic_file_name = _name_ext[0] _grafic_file_ext = _name_ext[1] _grafic_file_name_u = _grafic_file_name.upper() @@ -1493,12 +1490,12 @@ elif os.path.exists(_grafic_file_path_ll): _tested_grafic_file_list.append(_grafic_file_path_ll) else: - print(utils.mapping(_("The file $1 do not exist"), - (_grafic_file_path.decode("utf8"),)).encode("utf-8") ) + _tuni = _("The file $1 do not exist") + _uni = utils.mapping(_tuni, (_grafic_file_path,)) + print(_uni) if len(_grafic_file_list) > 0: for _grafic_file in _tested_grafic_file_list: - self.__budget.addFile(_record_code.encode("utf8"), - _grafic_file, "img", "") + self.__budget.addFile(_record_code, _grafic_file, "img", "") self.__statistics.valid = self.__statistics.valid +1 def _parseE(self, field_list): @@ -1523,11 +1520,11 @@ # If there are no sufficient fields, the fields are added # with empty value:"" else: - field_list = field_list[1:] + [u""]*(6-len(field_list)) + field_list = field_list[1:] + [""]*(6-len(field_list)) # _____Fields_____ # _____company Code_____ _company_code = self.delete_control_space(field_list[0]) - if _company_code == u"": + if _company_code == "": return # _____Summary_____ @@ -1538,15 +1535,15 @@ _local_offices = self.delete_control(field_list[3]) # _____subfields of local_offices_____ # last \ is erased - if len(_local_offices) and _local_offices[-1] == u"\\": + if len(_local_offices) and _local_offices[-1] == "\\": _local_offices = _local_offices[:-1] - _local_offices_list = _local_offices.split(u"\\") + _local_offices_list = _local_offices.split("\\") # If there are no sufficent subfields, the subfields are added # whith empty value _nsub = len(_local_offices_list) % 10 if _nsub != 0: _local_offices_list = _local_offices_list + \ - [u""]*(10-len(field_list)) + [""]*(10-len(field_list)) _local_offices = [] _local_offices_index = 0 while _local_offices_index < len(_local_offices_list)-9: @@ -1560,49 +1557,47 @@ _country = _local_offices_list[_local_offices_index+6] _phone = _local_offices_list[_local_offices_index+7] # last ; is erased - if len(_phone) and _phone[-1] == u";": + if len(_phone) and _phone[-1] == ";": _phone = _phone[:-1] - _phone_list = _phone.split(u";") - _phone_list = [_phone.encode("utf8") for _phone in _phone_list] + _phone_list = _phone.split(";") _fax = _local_offices_list[_local_offices_index+8] # last ; is erased - if len(_fax) and _fax[-1] == u";": + if len(_fax) and _fax[-1] == ";": _fax = _fax[:-1] - _fax_list = _fax.split(u";") - _fax_list = [_fax.encode("utf8") for _fax in _fax_list] + _fax_list = _fax.split(";") _contact_person = _local_offices_list[_local_offices_index+9] - if _type != u"" or _subname != u"" or _address != u"" or \ - _postal_code != u"" or _town != u"" or _province != u"" or \ - _country != u"" or _phone != u"" or _fax != u"" or \ - _contact_person != u"": - _local_offices.append([_type.encode("utf8"), - _subname.encode("utf8"), - _address.encode("utf8"), - _postal_code.encode("utf8"), - _town.encode("utf8"), - _province.encode("utf8"), - _country.encode("utf8"), + if _type != "" or _subname != "" or _address != "" or \ + _postal_code != "" or _town != "" or _province != "" or \ + _country != "" or _phone != "" or _fax != "" or \ + _contact_person != "": + _local_offices.append([_type, + _subname, + _address, + _postal_code, + _town, + _province, + _country, _phone_list, _fax_list, - _contact_person.encode("utf8")]) + _contact_person]) _local_offices_index = _local_offices_index + 10 # _____cif web email_____ _c_w_e = self.delete_control_space(field_list[4]) # last \ is erased - if len(_c_w_e) and _c_w_e[-1] == u"\\": + if len(_c_w_e) and _c_w_e[-1] == "\\": _c_w_e = _c_w_e[:-1] - _c_w_e_list = _c_w_e.split(u"\\") + _c_w_e_list = _c_w_e.split("\\") # _____subfields_____ # If there are no sufficient fields, the fields are added # with empty value:"" - _c_w_e_list = _c_w_e_list + [u""]*(3-len(_c_w_e_list)) + _c_w_e_list = _c_w_e_list + [""]*(3-len(_c_w_e_list)) _cif = _c_w_e_list[0] _web = _c_w_e_list[1] _email = _c_w_e_list[2] - self.__budget.setCompany(_company_code.encode("utf8"), - _sumamary.encode("utf8"), _name.encode("utf8"), - _local_offices, _cif.encode("utf8"), - _web.encode("utf8"), _email.encode("utf8")) + self.__budget.setCompany(_company_code, + _sumamary, _name, + _local_offices, _cif, + _web, _email) self.__statistics.valid = self.__statistics.valid +1 def _parseX(self, field_list): @@ -1631,35 +1626,34 @@ # "control": "[\t \n\r]" _field_1 = self.delete_control_space(field_list[0]) _field_2 = self.delete_control_space(field_list[1]) - if _field_1 == u"": + if _field_1 == "": # A) - _field_2_list = _field_2.split(u"\\") + _field_2_list = _field_2.split("\\") _ti_index = 0 while _ti_index < len(_field_2_list)-3: _ti_code = _field_2_list[_ti_index] _ti_description = _field_2_list[_ti_index+1] _ti_unit = _field_2_list[_ti_index+2] if _ti_code != "": - self.__budget.addTecInfo(_ti_code.encode("utf8"), - _ti_description.encode("utf8"), - _ti_unit.encode("utf8")) + self.__budget.addTecInfo(_ti_code, + _ti_description, + _ti_unit) _ti_index = _ti_index + 3 else: # B) # "#" and "##" characters at the end of the code are erased # invalid characters are also erased _record_code = self.validateCode(_field_1) - _field_2_list = _field_2.split(u"\\") + _field_2_list = _field_2.split("\\") _ti_index = 0 _ti_dict = {} while _ti_index < len(_field_2_list)-2: _ti_code = _field_2_list[_ti_index] _ti_value = _field_2_list[_ti_index+1] - if _ti_code != u"" and _ti_value != u"": - _ti_dict[_ti_code.encode("utf8")] = _ti_value.encode("utf8") + if _ti_code != "" and _ti_value != "": + _ti_dict[_ti_code] = _ti_value _ti_index = _ti_index + 2 - self.__budget.setTecnicalInformation(_record_code.encode("utf8"), - _ti_dict) + self.__budget.setTecnicalInformation(_record_code, _ti_dict) self.__statistics.valid = self.__statistics.valid +1 def _parseF(self, field_list): @@ -1689,16 +1683,16 @@ _files = self.delete_control(field_list[1]) # _____subfields_____ # last \ is erased - if len(_files) and _files[-1] == u"\\": + if len(_files) and _files[-1] == "\\": _files = _files[:-1] - _files_list = _files.split(u"\\") + _files_list = _files.split("\\") # adding empty subfiels if necesary if len(_files_list)%3 > 0: - _files_list.extend[u""]*(3 - len(_files_list)%3) + _files_list.extend[""]*(3 - len(_files_list)%3) _file_index = 0 _tested_files_list = [] while _file_index < len(_files_list)-3: - _type = _files_list[_file_index].replace(u" ",u"") + _type = _files_list[_file_index].replace(" ","") ## _types = { ## "0": _("others"), ## "1": _("características técnicas y de fabricación"), @@ -1714,23 +1708,23 @@ ## "etc. de empresa"), ## "11": _("certificado/s de empresa"), ## "12": _("obras realizadas")} - _types = [u"0", u"1", u"2", u"3", u"4", u"5", u"6", u"7", u"8", - u"9", u"10", u"11", u"12"] + _types = ["0", "1", "2", "3", "4", "5", "6", "7", "8", + "9", "10", "11", "12"] if not _type in _types: - _type = u"0" + _type = "0" _filenames = _files_list[_file_index + 1] _description = _files_list[_file_index + 2] _file_index += 3 - if len(_filenames) and _filenames[-1] == u";": + if len(_filenames) and _filenames[-1] == ";": _files = _files[:-1] - _filenames_list = _filenames.split(u";") + _filenames_list = _filenames.split(";") _path = os.path.dirname(self.__filename) for _filename in _filenames_list: - _file_path = os.path.join(_path, _filename.encode("utf8")) + _file_path = os.path.join(_path, _filename) if os.path.exists(_file_path): - _tested_files_list.append([_file_path, _type.encode("utf8"), - _description.encode("utf8")]) + _tested_files_list.append([_file_path, _type, + _description]) else: _name_ext = os.path.splitext(_filename) _file_name = _name_ext[0] @@ -1749,26 +1743,27 @@ _file_path_ll = os.path.join(_path, _ll) if os.path.exists(_file_path_uu): _tested_files_list.append([_file_path_uu, - _type.encode("utf8"), - _description.encode("utf8")]) + _type, + _description]) elif os.path.exists(_grafic_file_path_ul): _tested_files_list.append([_file_path_ul, - _type.encode("utf8"), - _description.encode("utf8")]) + _type, + _description]) elif os.path.exists(_grafic_file_path_lu): _tested_files_list.append([_file_path_lu, - _type.encode("utf8"), - _description.encode("utf8")]) + _type, + _description]) elif os.path.exists(_grafic_file_path_ll): _tested_files_list.append([_file_path_ll, - _type.encode("utf8"), - _description.encode("utf8")]) + _type, + _description]) else: - print(utils.mapping(_("The file $1 do not exist"), - (_file_path,)).encode("utf-8") ) + _tuni = _("The file $1 do not exist") + _uni = utils.mapping(_tuni, (_file_path,)) + print(_uni) if len(_tested_files_list) > 0: for _file in _tested_files_list: - self.__budget.addFile(_record_code.encode("utf8"), _file[0], + self.__budget.addFile(_record_code, _file[0], _file[1], _file[2]) self.__statistics.valid = self.__statistics.valid +1 @@ -1829,13 +1824,13 @@ # _____Labels_____ # last \ is erased # TODO: change the others parsers to this: - while len(_labels) > 0 and _labels[-1] == u"\\": + while len(_labels) > 0 and _labels[-1] == "\\": _labels = _labels[:-1] # replace "_" to " " - _labels = _labels.replace(u"_",u" ") - _label_list = _labels.split(u"\\") + _labels = _labels.replace("_"," ") + _label_list = _labels.split("\\") for _label in _label_list: - self.__budget.addLabel(_code.encode("utf8"), _label.encode("utf8")) + self.__budget.addLabel(_code, _label) self.__statistics.valid = self.__statistics.valid + 1 def _parseP(self, field_list): @@ -1856,13 +1851,13 @@ if len(field_list) > 2: # delete control caracters and spaces _family_code = self.delete_control_space(field_list[1]) - if _family_code == u"": # A)Global paremetric record + if _family_code == "": # A)Global paremetric record # The record must have 3 or 4 fields if len(field_list) > 4: field_list = field_list[0:4] field_list = field_list[1:] if len(field_list) == 2: - field_list.append(u"") + field_list.append("") if len(field_list) != 3: return else: # B)Family Parametric record @@ -1871,22 +1866,24 @@ field_list = field_list[0:3] field_list = field_list[1:] if len(field_list) != 2: - print(_("PyArq hates parametric DLLs").encode("utf-8") ) + _tuni = _("PyArq hates parametric DLLs") + print(_uni) return else: return # _____Description_____ _description = field_list[1] - if _description == u"": - print(_("PyArq hates parametric DLLs").encode("utf-8") ) + if _description == "": + _tuni = _("PyArq hates parametric DLLs") + print(_tuni) return # Adding last end of line - _description = _description + u"\r\n" + _description = _description + "\r\n" # Delete comments # "comment" : "#.*\r\n" - _description = self.__pattern["comment"].sub(u"\r\n",_description) + _description = self.__pattern["comment"].sub("\r\n",_description) # Tabs to spaces - _description = _description.replace(u"\t",u" ") + _description = _description.replace("\t"," ") # Delete empty lines # "empty_line": r"(\r\n) *\r\n" while self.__pattern["empty_line"].search(_description): @@ -1895,12 +1892,12 @@ # Delete spaces before and after / # "space_before_backslash" : r"( )+\\" _description = self.__pattern["space_before_backslash"].sub( - ur"\\",_description) + r"\\",_description) # "space_after_backslash" : r"\\( )+" _description = self.__pattern["space_after_backslash"].sub( - ur"\\",_description) + r"\\",_description) # Join lines that start but not end with / - _description = u"\r\n" + _description # add leading end of line + _description = "\r\n" + _description # add leading end of line # "start_noend_backslash": "(\r\n\\\.*[^\\\])\r\n" while self.__pattern["start_noend_backslash"].search(_description): _description = self.__pattern["start_noend_backslash"].sub( @@ -1916,8 +1913,8 @@ lambda x: x.groups()[0], _description) _description = _description[2:] # remove leading end of line #_description = re.sub(r"\\( )+",r"\\",_description) - _lines = _description.split(u"\r\n") - _final_description = u"" + _lines = _description.split("\r\n") + _final_description = "" _pass_line = 0 for index in range(len(_lines)): _line = _lines[index] @@ -1925,39 +1922,39 @@ if len(_line) != 0: # Delete empty lines if _pass_line > 0: _pass_line = _pass_line -1 - _line = u"" + _line = "" elif _line.isspace(): - _line = u"" - elif _line[0] != u"\\": + _line = "" + elif _line[0] != "\\": # Delete spaces out "" delimiter _list = _line.split(u'"') - _final_line = u"" + _final_line = "" for index1 in range(len(_list)): if index1 % 2 != 0: _parcial_line = u'"' + _list[index1] else: - _parcial_line = _list[index1].replace(u" ",u"") + _parcial_line = _list[index1].replace(" ","") _parcial_line = u'"' + _parcial_line _final_line = _final_line + _parcial_line _line = _final_line[1:] _lines[index] = _line # parse data - if len(_line) > 2 and _line[:2] == u"::": + if len(_line) > 2 and _line[:2] == "::": # Delete spaces out " delimiter #print("__PRECIO__" + _line[2:]) pass - elif len(_line) > 2 and _line[:2] == u"%:": + elif len(_line) > 2 and _line[:2] == "%:": # Delete spaces out " delimiter #print("__%AUX__" + _line[2:]) pass - elif len(_line) > 3 and _line[:2] == u"%%:": + elif len(_line) > 3 and _line[:2] == "%%:": # Delete spaces out " delimiter #print("__%%AUX__" + _line[2:] ) pass elif self.__pattern["var"].search(_line): # Delete spaces out " delimiter #print( "line =", _line ) - while _line.count(u'"') % 2 == 1 and \ + while _line.count('"') % 2 == 1 and \ index + _pass_line + 1 < len(_lines) -1: _line = _line + _lines[index + _pass_line + 1] _pass_line = _pass_line + 1 @@ -1965,7 +1962,7 @@ if _search is not None: _var0 = _search.groups()[0] _var1 = _search.groups()[1] - _var = _var0 + u" = " + _var1 + _var = _var0 + " = " + _var1 #print("__VAR__" + str(_var) ) pass else: @@ -1978,84 +1975,85 @@ if _search is not None: _var0 = _search.groups()[0] _var1 = _search.groups()[1] - _var = _var0 + u":" + _var1 + _var = _var0 + ":" + _var1 #print( "__Descomposición__" + str(_var) ) pass else: #print("no __Descomposición__", _line ) pass else: - _str = "Parametric: code: " + \ - _family_code.encode("utf8") - print(_str.encode("utf-8") ) - _str = "******* Desconocido *** : " + _line - print(_str.encode("utf-8") ) + _tuni = _("Parametric: code: $1") + _uni = utils.mapping(_tuni, (_family_code,)) + print(_uni) + _tuni = _("******* Desconocido *** : $1") + _uni = utils.mapping(_tuni, (_line,)) + print(_uni) if index-10 > 0: - print("-11 : " + _lines[index-11].encode("utf8") ) + print("-11 : " + _lines[index-11] ) if index-10 > 0: - print("-10 : " + _lines[index-10].encode("utf8") ) + print("-10 : " + _lines[index-10] ) if index-9 > 0: - print("-9 : " + _lines[index-9].encode("utf8") ) + print("-9 : " + _lines[index-9] ) if index-8 > 0: - print("-8 : " + _lines[index-8].encode("utf8") ) + print("-8 : " + _lines[index-8] ) if index-7 > 0: - print("-7 : " + _lines[index-7].encode("utf8") ) + print("-7 : " + _lines[index-7] ) if index-6 > 0: - print("-6 : " + _lines[index-6].encode("utf8") ) + print("-6 : " + _lines[index-6] ) if index-5 > 0: - print("-5 : " + _lines[index-5].encode("utf8") ) + print("-5 : " + _lines[index-5] ) if index-4 > 0: - print("-4 : " + _lines[index-4].encode("utf8") ) + print("-4 : " + _lines[index-4] ) if index-3 > 0: - print("-3 : " + _lines[index-3].encode("utf8") ) + print("-3 : " + _lines[index-3] ) if index-2 > 0: - print("-2 : " + _lines[index-2].encode("utf8") ) + print("-2 : " + _lines[index-2] ) if index-1 > 0: - print("-1 : " + _lines[index-1].encode("utf8") ) - print(("-0 :" + _lines[index-0]).encode("utf-8") ) + print("-1 : " + _lines[index-1] ) + print(("-0 :" + _lines[index-0]) ) pass else: - _parameter_list = _line.split(u"\\")[1:-1] + _parameter_list = _line.split("\\")[1:-1] if len(_parameter_list) >= 2: - if _parameter_list[0] == u"C" or \ - _parameter_list[0] == u"COMENTARIO": + if _parameter_list[0] == "C" or \ + _parameter_list[0] == "COMENTARIO": #print( "__COMENTARIO__" + _parameter_list[1]) self.__budget.setParametricSelectComment( - _family_code.encode("utf8"), - _parameter_list[1].encode("utf8")) - elif _parameter_list[0] == u"R" or \ - _parameter_list[0] == u"RESUMEN": + _family_code, + _parameter_list[1]) + elif _parameter_list[0] == "R" or \ + _parameter_list[0] == "RESUMEN": #print( "__RESUMEN__" + _parameter_list[1]) self.__budget.setParametricSummary( - _family_code.encode("utf8"), - _parameter_list[1].encode("utf8")) - elif _parameter_list[0] == u"T" or \ - _parameter_list[0] == u"TEXTO": + _family_code, + _parameter_list[1]) + elif _parameter_list[0] == "T" or \ + _parameter_list[0] == "TEXTO": #print( "__TEXTO__" + _parameter_list[1]) self.__budget.setParametricText( - _family_code.encode("utf8"), - _parameter_list[1].encode("utf8")) - elif _parameter_list[0] == u"P" or \ - _parameter_list[0] == u"PLIEGO": + _family_code, + _parameter_list[1]) + elif _parameter_list[0] == "P" or \ + _parameter_list[0] == "PLIEGO": #print( "__PLIEGO__" + str(_parameter_list[1:]) ) pass - elif _parameter_list[0] == u"K" or \ - _parameter_list[0] == u"CLAVES": + elif _parameter_list[0] == "K" or \ + _parameter_list[0] == "CLAVES": #print( "__CLAVES__" + str(_parameter_list[1:]) ) pass - elif _parameter_list[0] == u"F" or \ - _parameter_list[0] == u"COMERCIAL": + elif _parameter_list[0] == "F" or \ + _parameter_list[0] == "COMERCIAL": #print( "__COMERCIAL__" + str(_parameter_list[1:]) ) pass else: #print( "==PARAMETRO==" + str(_parameter_list[:]) ) pass - _final_description = _final_description + _line + u"\r\n" + _final_description = _final_description + _line + "\r\n" #print( _line ) # Delete last empty line _description = _final_description[:-2] - _lines = _description.split(u"\r\n") + _lines = _description.split("\r\n") for _line in _lines: pass #print( _line ) @@ -2088,25 +2086,29 @@ interface.readFile_set_statistics(self.__statistics) _time = time.time() try: - _file = open(self.__filename, 'r') + _file = open(self.__filename, 'rb') except IOError: - _str = utils.mapping("IOError: $1", (self.__filename,)) - print( _str.encode("utf-8") ) + _tuni = "IOError: $1" + _uni = utils.mapping(_tuni, (self.__filename,)) + print(_uni) return None _filesize = float(os.path.getsize(self.__filename)) if _filesize == 0.0: - _str = utils.mapping("Empty File: $1", (self.__filename,)) - print( _str.encode("utf-8") ) + _tuni = "Empty File: $1" + _uni = utils.mapping(_uni, (self.__filename,)) + print(_uni) # Todo: Create empty budget return None self.__budget.filename = self.__filename - interface.readFile_send_message(utils.mapping(_("Loading file $1"), - (self.__filename,)).encode("utf-8")) + _tuni = _("Loading file $1") + _uni = utils.mapping(_tuni, (self.__filename,)) + interface.readFile_send_message(_uni) interface.readFile_progress(_file.tell() / _filesize) _buffer = _file.read(1000) + _dbuffer = text(_buffer, self.__character_set) interface.updateGui() # set codepage from V record - _record_list = _buffer.split("~") + _record_list = _dbuffer.split("~") registro_V = _record_list[1] # ~V|[PROPIEDAD_ARCHIVO]|VERSION_FORMATO[\DDMMAAAA]|[PROGRAMA_EMISION]| # [CABECERA]\{ ROTULO_IDENTIFICACION \}|[JUEGO_CARACTERES]| @@ -2120,36 +2122,36 @@ # remove leading spaces if _version in self.__character_sets_dict: self.__character_set = self.__character_sets_dict[_version] - interface.readFile_send_message(utils.mapping( - _("FIEBDC character encoding: $1"), - (self.__character_set,)).encode("utf8")) + _tuni = _("FIEBDC character encoding: $1") + _uni = utils.mapping(_tuni, (self.__character_set,)) + interface.readFile_send_message(_uni) else: - interface.readFile_send_message(utils.mapping( - _("This Character encoding do not exist in "\ - "FIEBDC3! Default Character encoding: $1"), - (self.__character_set,)).encode("utf-8")) + _tuni = _("This Character encoding do not exist in "\ + "FIEBDC3! Default Character encoding: $1") + _uni = utils.mapping(_tuni, (self.__character_set,)) + interface.readFile_send_message(_uni) else: - interface.readFile_send_message(utils.mapping(_( - "This V record dot have a character encoding! "\ - "Default character encoding: $1"), - (self.__character_set,)).encode("utf-8")) + _tuni = _("This V record dot have a character encoding! "\ + "Default character encoding: $1") + _uni = utils.mapping(_tuni, (self.__character_set,)) + interface.readFile_send_message(_uni) else: - interface.readFile_send_message(utils.mapping(_( - "Not 'V' record in File! Default character encoding: "\ - "$1"), (self.__character_set,)).encode("utf-8")) - _buffer = unicode(_buffer, self.__character_set) + _tuni = _("Not 'V' record in File! Default character encoding: $1") + _uni = utils.mapping(_tuni, (self.__character_set,)) + interface.readFile_send_message(_uni) + _dbuffer = text(_buffer, self.__character_set) interface.updateGui() # Any INFORMATION between the beginning of the file and the # beginning of the first registry “~” is ignored #"after_first_tilde" : "^[^~]*~" - _buffer = self.__pattern["after_first_tilde"].sub("",_buffer) - while _buffer != u"" and not self.__cancel: + _dbuffer = self.__pattern["after_first_tilde"].sub("",_dbuffer) + while _dbuffer != "" and not self.__cancel: #-# the blank characters (32), tabs (9) and end of line (13 and 10) # before the separators '~', '|' are erased. # Before separator \ not deleted because it affects the reading of # the record ~P - _buffer = self.eraseControlCharacters(_buffer) - _record_list = _buffer.split(u"~") + _dbuffer = self.eraseControlCharacters(_dbuffer) + _record_list = _dbuffer.split("~") # The last record can be incomplete unless it is the last one of # the file #if len(_record_list) > 1: @@ -2161,9 +2163,9 @@ # The blank characters (32), tabs (9) and end of line # (13 and 10) at the end of the file are ignored. #"end_control" : "((\r\n)| |\t)+$" - _record_list[-1] = self.__pattern["end_control"].sub(u"", + _record_list[-1] = self.__pattern["end_control"].sub("", _record_list[-1]) - _last_record = u"" + _last_record = "" for record in _record_list: if self.__cancel: break @@ -2172,8 +2174,8 @@ interface.readFile_progress(_file.tell() / _filesize) _buffer2 = _file.read(100000) interface.updateGui() - _buffer2 = unicode(_buffer2, self.__character_set) - _buffer = _last_record + _buffer2 + _dbuffer2 = text(_buffer2, self.__character_set) + _dbuffer = _last_record + _dbuffer2 interface.updateGui() _file.close() if self.__cancel: @@ -2182,13 +2184,12 @@ else: self.__statistics.time = time.time()-_time if self.__statistics.O > 0: - _str = utils.mapping( - _("$1 unsuported record type O: Comercial Relationship"), - (str(self.__statistics.O,))) - interface.readFile_send_message(_str.encode("utf-8")) + _tuni = _("$1 unsuported record type O: Comercial Relationship") + _uni = utils.mapping(_tuni, (text(self.__statistics.O,))) + interface.readFile_send_message(_uni) if self.__statistics.valid == 0: - _str = _("This file is not a valid FIBDC3 file") - interface.readFile_send_message(_str.encode("utf-8")) + _tuni = _("This file is not a valid FIBDC3 file") + interface.readFile_send_message(_tuni) return None interface.readFile_end() self._testBudget(self.__budget, interface) @@ -2201,7 +2202,8 @@ Test and repair budget object after read it from bc3 file """ # TODO: more to do here - print( _("Testing budget ...").encode("utf-8") ) + _tuni = _("Testing budget ...") + print(_tuni) # Add price to records without price _iter = budget.iter() _titlelist = budget.getTitleList()[1] @@ -2220,17 +2222,18 @@ _price = [0.0, _root.getDate(_len_prices + _index)] budget.addPriceToRecord(_price,_record) interface.updateGui() - print(_("End Test").encode("utf-8")) + _tuni = _("End Test") + print(_tuni) def delete_control_space(self, text): text = self.delete_control(text) - text = text.replace(u" ", u"") + text = text.replace(" ", "") return text def delete_control(self, text): - text = text.replace(u"\t", u"") - text = text.replace(u"\r", u"") - text = text.replace(u"\n", u"") + text = text.replace("\t", "") + text = text.replace("\r", "") + text = text.replace("\n", "") return text class Interface(object): @@ -2278,7 +2281,7 @@ print( message ) """ - print( message.encode("utf-8") ) + print(message) def readFile_progress(self, percent): """progress(percent) @@ -2295,8 +2298,10 @@ The readFile method end successfully """ self.endSuccessfully == True - print(self.__statistics.encode("utf-8")) - print(("progreso = " + str(self.__progress)).encode("utf-8")) + print(self.__statistics) + _tuni = "progreso = $1" + _uni = utils.mapping(_tuni, (text(self.__progress,))) + print(_uni) def readFile_cancel(self): """readFile_cancel() @@ -2304,8 +2309,11 @@ The readFile method is canceled """ self.endSuccessfully == False - print( _("Process terminated").encode("utf-8") ) - print(("progreso = " + str(self.__progress)).encode("utf-8")) + _tuni = _("Process terminated") + print(_tuni) + _tuni = "progreso = $1" + _uni = utils.mapping(_tuni, (text(self.__progress,))) + print(_tuni) def updateGui(self): """updateGui(self) @@ -2385,10 +2393,13 @@ return self.str() def str(self): - _str = utils.mapping(_("Time to load: $1 seconds"), - (("%.2f" %(self.time)),)) + "\n" + \ - utils.mapping(_("Records/Valid Records: $1/$2"), - (str(self.records), str(self.valid))) + "\n" +\ + _tuni1 = _("Time to load: $1 seconds") + _uni1 = utils.mapping(_tuni1, (("%.2f" %(self.time)),)) + _tuni2 = _("Records/Valid Records: $1/$2") + _uni2 = utils.mapping(_tuni2, + (text(self.records), text(self.valid))) + _uni = _uni1 + "\n" + \ + _uni2 + "\n" +\ "V: %s\n" %(self.V,) + \ "C: %s\n" %(self.C,) + \ "D: %s\n" %(self.D,) + \ @@ -2410,5 +2421,5 @@ "F: %s\n" %(self.F,) + \ "A: %s\n" %(self.A,) + \ "?: %s\n" %(self.unknow,) - return _str.encode("utf8") + return _uni