Mercurial > pyarq-presupuestos
comparison Generic/utils.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 | 65e7ae0d0e63 |
children |
comparison
equal
deleted
inserted
replaced
25:189f8274aecd | 26:16f91684686b |
---|---|
18 ## | 18 ## |
19 ## You should have received a copy of the GNU General Public License | 19 ## You should have received a copy of the GNU General Public License |
20 ## along with this program. If not, see <http://www.gnu.org/licenses/>. | 20 ## along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | 21 |
22 # Modules | 22 # Modules |
23 | |
24 # python 2/3 compatibility | |
25 from __future__ import absolute_import, division, print_function, unicode_literals | |
26 from builtins import str as text | |
27 from six import text_type | |
28 from io import IOBase | |
29 | |
23 import re | 30 import re |
24 import imghdr | 31 import imghdr |
25 import os.path | 32 import os.path |
26 | 33 |
34 | |
27 # add svg to imghdr | 35 # add svg to imghdr |
28 def test_svg(h, f): | 36 def test_svg(h, f): |
29 """SVG """ | 37 """SVG """ |
30 if isinstance(f,file): | 38 if isinstance(f,IOBase): |
31 _pos = f.tell() | 39 _pos = f.tell() |
32 f.seek(0) | 40 f.seek(0) |
33 _h = f.read(32) | 41 _h = f.read(32) |
34 f.seek(-32, 2) | 42 f.seek(-32, 2) |
35 _l = f.read(32) | 43 _l = f.read(32) |
36 f.seek(_pos) | 44 f.seek(_pos) |
37 else: | 45 else: |
38 _h = h | 46 _h = h |
39 _l = h[-32:] | 47 _l = h[-32:] |
40 if "<?xml" in _h and "</svg>" in _l: | 48 if b"<?xml" in _h and b"</svg>" in _l: |
41 return 'svg' | 49 return 'svg' |
42 imghdr.tests.append(test_svg) | 50 imghdr.tests.append(test_svg) |
43 | 51 |
44 # add ico to imghdr | 52 # add ico to imghdr |
45 def test_ico(h, f): | 53 def test_ico(h, f): |
46 """ico image file""" | 54 """ico image file""" |
47 if h[:4] == "\x00\x00\x01\x00": | 55 if h[:4] == b"\x00\x00\x01\x00": |
48 return 'ico' | 56 return 'ico' |
49 imghdr.tests.append(test_ico) | 57 imghdr.tests.append(test_ico) |
50 | 58 |
51 # add wmf to imghdr | 59 # add wmf to imghdr |
52 def test_pdf(h, f): | 60 def test_pdf(h, f): |
53 """pdf file""" | 61 """pdf file""" |
54 if h[:4] == "%PDF": | 62 if h[:4] == b"%PDF": |
55 return 'pdf' | 63 return 'pdf' |
56 imghdr.tests.append(test_pdf) | 64 imghdr.tests.append(test_pdf) |
57 | 65 |
58 # add wmf to imghdr | 66 # add wmf to imghdr |
59 def test_wmf(h, f): | 67 def test_wmf(h, f): |
60 """wmf image library""" | 68 """wmf image library""" |
61 if h[:6] == "\xd7\xcd\xc6\x9a\x00\x00": | 69 if h[:6] == b"\xd7\xcd\xc6\x9a\x00\x00": |
62 return 'wmf' | 70 return 'wmf' |
63 imghdr.tests.append(test_wmf) | 71 imghdr.tests.append(test_wmf) |
64 | 72 |
65 # add dxf to imghdr | 73 # add dxf to imghdr |
66 def test_dxf(h, f): | 74 def test_dxf(h, f): |
67 """AutoCAD DXF: Drawing Interchange Format""" | 75 """AutoCAD DXF: Drawing Interchange Format""" |
68 if isinstance(f,file): | 76 if isinstance(f,IOBase): |
69 _pos = f.tell() | 77 _pos = f.tell() |
70 f.seek(0) | 78 f.seek(0) |
71 _h = f.read(128) | 79 _h = f.read(128) |
72 f.seek(-32, 2) | 80 f.seek(-32, 2) |
73 _l = f.read(32) | 81 _l = f.read(32) |
74 f.seek(_pos) | 82 f.seek(_pos) |
75 else: | 83 else: |
76 _h = h | 84 _h = h |
77 _l = h[-32:] | 85 _l = h[-32:] |
78 _h = _h.replace("\r","") | 86 _h = _h.replace(b"\r",b"") |
79 _l = _l.replace("\r","") | 87 _l = _l.replace(b"\r",b"") |
80 if (" 0\nSECTION\n 2\nHEADER\n" in _h or\ | 88 if (b" 0\nSECTION\n 2\nHEADER\n" in _h or\ |
81 " 0\nSECTION\n 2\nCLASSES\n" in _h or\ | 89 b" 0\nSECTION\n 2\nCLASSES\n" in _h or\ |
82 " 0\nSECTION\n 2\nTABLES\n" in _h or\ | 90 b" 0\nSECTION\n 2\nTABLES\n" in _h or\ |
83 " 0\nSECTION\n 2\nBLOCKS\n" in _h or\ | 91 b" 0\nSECTION\n 2\nBLOCKS\n" in _h or\ |
84 " 0\nSECTION\n 2\nENTITIES\n" in _h or\ | 92 b" 0\nSECTION\n 2\nENTITIES\n" in _h or\ |
85 " 0\nSECTION\n 2\nOBJECTS\n" in _h or\ | 93 b" 0\nSECTION\n 2\nOBJECTS\n" in _h or\ |
86 " 0\nSECTION\n 2\nTHUMBNAILIMAGE\n" in _h) and \ | 94 b" 0\nSECTION\n 2\nTHUMBNAILIMAGE\n" in _h) and \ |
87 _l[-19:] == " 0\nENDSEC\n 0\nEOF\n": | 95 _l[-19:] == b" 0\nENDSEC\n 0\nEOF\n": |
88 return 'dxf' | 96 return 'dxf' |
89 imghdr.tests.append(test_dxf) | 97 imghdr.tests.append(test_dxf) |
90 | 98 |
91 | 99 |
92 def mapping(string, tuple_strings): | 100 def mapping(string, tuple_strings): |
98 from the tuple. | 106 from the tuple. |
99 It is used because the gettext module can not #-#supotr#-# strings as: | 107 It is used because the gettext module can not #-#supotr#-# strings as: |
100 "Invalid type (%s) in record: %s" %(type, record) | 108 "Invalid type (%s) in record: %s" %(type, record) |
101 """ | 109 """ |
102 for _index in range(len(tuple_strings)): | 110 for _index in range(len(tuple_strings)): |
103 string = string.replace("$" + str(_index+1), tuple_strings[_index]) | 111 string = string.replace("$" + text(_index+1), tuple_strings[_index]) |
104 #string = string.replace("$" + str(_index+1), str(tuple[_index])) | |
105 return string | 112 return string |
106 | 113 |
107 def eliminate_duplicates(list): | 114 def eliminate_duplicates(list): |
108 """eliminate_duplicates(list) | 115 """eliminate_duplicates(list) |
109 | 116 |
141 code(False/code) | 148 code(False/code) |
142 False: the code is not valid and can not be corrected | 149 False: the code is not valid and can not be corrected |
143 code: the code or the corrected code | 150 code: the code or the corrected code |
144 """ | 151 """ |
145 _is_valid = True | 152 _is_valid = True |
146 if not isinstance(code, str): | 153 if not isinstance(code, text_type): |
147 print("Not a string, code: " + code + type(code) ) | 154 _tuni = _("Not a text string, code: $1, type: $2") |
155 _uni = mapping(_tuni, (code ,text(type(code)))) | |
156 print(_uni) | |
148 return False, False | 157 return False, False |
149 if code == "": | 158 if code == "": |
150 return False, False | 159 return False, False |
151 try: | 160 try: |
152 _unicode_code = unicode(code, "utf8",'replace') | 161 #_unicode_code = unicode(code, "utf-8",'replace') |
153 _code_utf8 = _unicode_code.encode("utf8",'replace') | 162 _unicode_code = code |
163 _code_utf8 = _unicode_code.encode("utf-8",'replace') | |
154 _code_cp850 = _unicode_code.encode("cp850",'replace') | 164 _code_cp850 = _unicode_code.encode("cp850",'replace') |
155 _unicode_code = unicode(_code_cp850, "cp850",'replace') | 165 _unicode_code = text(_code_cp850, "cp850",'replace') |
156 | 166 |
157 except UnicodeError: | 167 except UnicodeError: |
158 print ("Unicode Error, code: " + code ) | 168 _tuni = _("Unicode Error, code: $1") |
159 return False, False | 169 _uni = mapping(_tuni, (code, )) |
160 if _code_utf8 != code: | 170 print (_uni) |
161 print ("Not in cp950, code: " + code ) | 171 return False, False |
172 if code != _unicode_code: | |
173 _tuni = _("Not in cp850, code: $1") | |
174 _uni = mapping(_tuni, (code, )) | |
175 print (_uni) | |
162 _is_valid = False | 176 _is_valid = False |
163 if _code_utf8 == "": | 177 if _code_utf8 == "": |
164 return False, False | 178 return False, False |
165 code = _code_utf8 | 179 code = _code_utf8 |
166 _code2 = re.sub("[\t \n\r~|\\\]","",code) | 180 _code2 = re.sub("[\t \n\r~|\\\]","",code) |
167 if _code2 != code: | 181 if _code2 != code: |
168 print("Control characters in code: " + code ) | 182 _tuni = _("Control characters in code: $1") |
183 _uni = mapping(_tuni, (code, )) | |
184 print(_uni) | |
169 if _code2 == "": | 185 if _code2 == "": |
170 return False, False | 186 return False, False |
171 _is_valid = False | 187 _is_valid = False |
172 code = _code2 | 188 code = _code2 |
173 if code[-1] == "#": | 189 if code[-1] == "#": |
174 print("# in code: " + code ) | 190 _tuni = _("# in code: $1") |
175 _is_valid = False | 191 _uni = mapping(_tuni, (code, )) |
192 print(_uni) | |
193 _is_valid = False | |
176 while code[-1] == "#": | 194 while code[-1] == "#": |
177 code = code[:-1] | 195 code = code[:-1] |
178 if code == "": | 196 if code == "": |
179 print("Empty code") | 197 _tuni = _("Empty code") |
198 print(_tuni) | |
180 return False, False | 199 return False, False |
181 return _is_valid, code | 200 return _is_valid, code |
182 | 201 |
183 def getFiletype(filename, h=None): | 202 def getFiletype(filename, h=None): |
184 """getFiletype(filename, h=None): | 203 """getFiletype(filename, h=None): |