Mercurial > pyarq-presupuestos
comparison Generic/utils.py @ 23:65e7ae0d0e63
GTK2 to GTK3
author | Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es> |
---|---|
date | Thu, 02 May 2019 16:31:17 +0200 |
parents | a7b9f7e7dfa4 |
children | 16f91684686b |
comparison
equal
deleted
inserted
replaced
22:7bd4ca56607d | 23:65e7ae0d0e63 |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 ## File utils.py | 3 ## File utils.py |
4 ## This file is part of pyArq-Presupuestos. | 4 ## This file is part of pyArq-Presupuestos. |
5 ## | 5 ## |
6 ## Copyright (C) 2010-2013 Miguel Ángel Bárcena Rodríguez | 6 ## Copyright (C) 2010-2019 Miguel Ángel Bárcena Rodríguez |
7 ## <miguelangel@obraencurso.es> | 7 ## <miguelangel@obraencurso.es> |
8 ## | 8 ## |
9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify | 9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify |
10 ## it under the terms of the GNU General Public License as published by | 10 ## it under the terms of the GNU General Public License as published by |
11 ## the Free Software Foundation, either version 3 of the License, or | 11 ## the Free Software Foundation, either version 3 of the License, or |
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 import re | 23 import re |
24 import imghdr | 24 import imghdr |
25 import os.path | |
26 | |
27 # add svg to imghdr | |
28 def test_svg(h, f): | |
29 """SVG """ | |
30 if isinstance(f,file): | |
31 _pos = f.tell() | |
32 f.seek(0) | |
33 _h = f.read(32) | |
34 f.seek(-32, 2) | |
35 _l = f.read(32) | |
36 f.seek(_pos) | |
37 else: | |
38 _h = h | |
39 _l = h[-32:] | |
40 if "<?xml" in _h and "</svg>" in _l: | |
41 return 'svg' | |
42 imghdr.tests.append(test_svg) | |
43 | |
44 # add ico to imghdr | |
45 def test_ico(h, f): | |
46 """ico image file""" | |
47 if h[:4] == "\x00\x00\x01\x00": | |
48 return 'ico' | |
49 imghdr.tests.append(test_ico) | |
50 | |
51 # add wmf to imghdr | |
52 def test_pdf(h, f): | |
53 """pdf file""" | |
54 if h[:4] == "%PDF": | |
55 return 'pdf' | |
56 imghdr.tests.append(test_pdf) | |
25 | 57 |
26 # add wmf to imghdr | 58 # add wmf to imghdr |
27 def test_wmf(h, f): | 59 def test_wmf(h, f): |
28 """wmf image library""" | 60 """wmf image library""" |
29 if h[:6] == "\xd7\xcd\xc6\x9a\x00\x00": | 61 if h[:6] == "\xd7\xcd\xc6\x9a\x00\x00": |
55 _l[-19:] == " 0\nENDSEC\n 0\nEOF\n": | 87 _l[-19:] == " 0\nENDSEC\n 0\nEOF\n": |
56 return 'dxf' | 88 return 'dxf' |
57 imghdr.tests.append(test_dxf) | 89 imghdr.tests.append(test_dxf) |
58 | 90 |
59 | 91 |
60 def mapping(string, tuple): | 92 def mapping(string, tuple_strings): |
61 """mapping(string, tuple) | 93 """mapping(string, tuple) |
62 | 94 |
63 string: a message string | 95 string: a message string |
64 tuple: a truple with string items | 96 tuple_strings: a truple with string items |
65 Return the string replacing the $[n] words whith its corresponding value | 97 Return the string replacing the $[n] words whith its corresponding value |
66 from the tuple. | 98 from the tuple. |
67 It is used because the gettext module can not #-#supotr#-# strings as: | 99 It is used because the gettext module can not #-#supotr#-# strings as: |
68 "Invalid type (%s) in record: %s" %(type, record) | 100 "Invalid type (%s) in record: %s" %(type, record) |
69 """ | 101 """ |
70 for _index in range(len(tuple)): | 102 for _index in range(len(tuple_strings)): |
71 string = string.replace("$" + str(_index+1), str(tuple[_index])) | 103 string = string.replace("$" + str(_index+1), tuple_strings[_index]) |
104 #string = string.replace("$" + str(_index+1), str(tuple[_index])) | |
72 return string | 105 return string |
73 | 106 |
74 def eliminate_duplicates(list): | 107 def eliminate_duplicates(list): |
75 """eliminate_duplicates(list) | 108 """eliminate_duplicates(list) |
76 | 109 |
109 False: the code is not valid and can not be corrected | 142 False: the code is not valid and can not be corrected |
110 code: the code or the corrected code | 143 code: the code or the corrected code |
111 """ | 144 """ |
112 _is_valid = True | 145 _is_valid = True |
113 if not isinstance(code, str): | 146 if not isinstance(code, str): |
114 print "Not a string, code:", code, type(code) | 147 print("Not a string, code: " + code + type(code) ) |
115 return False, False | 148 return False, False |
116 if code == "": | 149 if code == "": |
117 | |
118 return False, False | 150 return False, False |
119 try: | 151 try: |
120 _unicode_code = unicode(code, "utf8",'replace') | 152 _unicode_code = unicode(code, "utf8",'replace') |
121 _code_utf8 = _unicode_code.encode("utf8",'replace') | 153 _code_utf8 = _unicode_code.encode("utf8",'replace') |
122 _code_cp850 = _unicode_code.encode("cp850",'replace') | 154 _code_cp850 = _unicode_code.encode("cp850",'replace') |
123 _unicode_code = unicode(_code_cp850, "cp850",'replace') | 155 _unicode_code = unicode(_code_cp850, "cp850",'replace') |
124 | 156 |
125 except UnicodeError: | 157 except UnicodeError: |
126 print "Unicode Error, code:", code | 158 print ("Unicode Error, code: " + code ) |
127 return False, False | 159 return False, False |
128 if _code_utf8 != code: | 160 if _code_utf8 != code: |
129 print "Not in cp950, code:", code | 161 print ("Not in cp950, code: " + code ) |
130 _is_valid = False | 162 _is_valid = False |
131 if _code_utf8 == "": | 163 if _code_utf8 == "": |
132 return False, False | 164 return False, False |
133 code = _code_utf8 | 165 code = _code_utf8 |
134 _code2 = re.sub("[\t \n\r~|\\\]","",code) | 166 _code2 = re.sub("[\t \n\r~|\\\]","",code) |
135 if _code2 != code: | 167 if _code2 != code: |
136 print "Control characters in code:", code | 168 print("Control characters in code: " + code ) |
137 if _code2 == "": | 169 if _code2 == "": |
138 return False, False | 170 return False, False |
139 _is_valid = False | 171 _is_valid = False |
140 code = _code2 | 172 code = _code2 |
141 if code[-1] == "#": | 173 if code[-1] == "#": |
142 print "# in code:", code | 174 print("# in code: " + code ) |
143 _is_valid = False | 175 _is_valid = False |
144 while code[-1] == "#": | 176 while code[-1] == "#": |
145 code = code[:-1] | 177 code = code[:-1] |
146 if code == "": | 178 if code == "": |
147 print "Empty code" | 179 print("Empty code") |
148 return False, False | 180 return False, False |
149 return _is_valid, code | 181 return _is_valid, code |
150 | 182 |
151 def getFiletype(filename, h=None): | 183 def getFiletype(filename, h=None): |
152 """getFiletype(filename, h=None): | 184 """getFiletype(filename, h=None): |
153 | 185 |
154 filename: the filename to test | 186 filename: the filename to test |
155 h: raw string, if h is not None the filename is ignored and h is assumed | 187 h: raw string, if h is not None the filename is ignored and h is assumed |
156 to contain the byte stream to test | 188 to contain the byte stream to test |
157 """ | 189 |
190 valid types: | |
191 "image", "wmf", "dxf", "pdf" , "video", | |
192 "office-document", "office-presentation", "office-spreadsheet", | |
193 "html", "rtf", "txt" | |
194 """ | |
195 _ext = os.path.splitext(filename)[1][1:].lower() | |
196 | |
197 _video_types = ["avi", "mp4", "m4p", "m4v2", "m4v","amv", "mpg", "m2v", | |
198 "mp2", "mpe", "mpv", "mpeg", "ogg", "ogv", "webm", "mkv", | |
199 "ogm", "flv", "f4v", "f4p", "f4a", "f4b", "vob", "drc", | |
200 "mts", "m2ts", "mov", "qt", "wmv", "yuv", "rm", "rmvb", | |
201 "asf", "svi", "3gp", "3g2", "mxf", "roq", "nsv"] | |
202 _document_types = ["doc", "docx", "odt"] | |
203 _spreadsheet_types = ["xls", "xlsx", "ods"] | |
204 _presentation_types = ["pps", "ppsx", "ppt", "pptx", "odp"] | |
205 _html_types = ["html", "xhtml"] | |
206 if _ext in _video_types: | |
207 return "video" | |
208 elif _ext in _document_types: | |
209 return "office-document" | |
210 elif _ext in _spreadsheet_types: | |
211 return "office-spreadsheet" | |
212 elif _ext in _presentation_types: | |
213 return "office-presentation" | |
214 elif _ext in _html_types: | |
215 return "html" | |
216 elif _ext == "rtf": | |
217 return "rtf" | |
218 elif _ext == "txt": | |
219 return "txt" | |
158 _type = imghdr.what(filename, h) | 220 _type = imghdr.what(filename, h) |
159 _image_types = ["rgb", "gif", "pbm", "pgm", "ppm", "tiff", "rast", "xbm", | 221 _image_types = ["rgb", "gif", "pbm", "pgm", "ppm" ,"tiff", "tif", "rast", |
160 "jpeg", "bmp", "png", "wmf"] | 222 "xbm", "jpeg", "jpg", "bmp", "png", "webp", "exr", |
161 if _type in _image_types: | 223 "ico", "svg"] |
224 if _type in _image_types and _ext in _image_types: | |
162 return "image" | 225 return "image" |
163 elif _type == "dxf": | 226 elif _type == "wmf" and _ext == "wmf": |
227 return "wmf" | |
228 elif _type == "dxf" and _ext == "dxf": | |
164 return "dxf" | 229 return "dxf" |
165 ## _video_types = ["avi", "mpg", "mkv", "ogm"] | 230 |
166 ## elif _type in _video_types: | 231 elif _type == "pdf" and _ext == "pdf": |
167 ## return "video" | 232 return "pdf" |
168 ## elif _type == "pdf": | 233 return None |
169 ## return "pdf" | |
170 ## elif _type == "ppt" or _type == "odp": | |
171 ## return "presentation" | |
172 else: | |
173 return None |