comparison Generic/utils.py @ 1:2ac1551ad2ab version 0.0.0

add code
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Sun, 31 Oct 2010 20:07:33 +0100
parents
children a7b9f7e7dfa4
comparison
equal deleted inserted replaced
0:a1703c4f2990 1:2ac1551ad2ab
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ## File utils.py
4 ## This file is part of pyArq-Presupuestos.
5 ##
6 ## Copyright (C) 2010 Miguel Ángel Bárcena Rodríguez
7 ## <miguelangel@obraencurso.es>
8 ##
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
11 ## the Free Software Foundation, either version 3 of the License, or
12 ## (at your option) any later version.
13 ##
14 ## pyArq-Presupuestos is distributed in the hope that it will be useful,
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ## GNU General Public License for more details.
18 ##
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/>.
21
22 # Modules
23 import re
24 import imghdr
25
26 # add wmf to imghdr
27 def test_wmf(h, f):
28 """wmf image library"""
29 if h[:6] == "\xd7\xcd\xc6\x9a\x00\x00":
30 return 'wmf'
31 imghdr.tests.append(test_wmf)
32
33 # add dxf to imghdr
34 def test_dxf(h, f):
35 """AutoCAD DXF: Drawing Interchange Format"""
36 if isinstance(f,file):
37 _pos = f.tell()
38 f.seek(0)
39 _h = f.read(128)
40 f.seek(-32, 2)
41 _l = f.read(32)
42 f.seek(_pos)
43 else:
44 _h = h
45 _l = h[-32:]
46 _h = _h.replace("\r","")
47 _l = _l.replace("\r","")
48 if (" 0\nSECTION\n 2\nHEADER\n" in _h or\
49 " 0\nSECTION\n 2\nCLASSES\n" in _h or\
50 " 0\nSECTION\n 2\nTABLES\n" in _h or\
51 " 0\nSECTION\n 2\nBLOCKS\n" in _h or\
52 " 0\nSECTION\n 2\nENTITIES\n" in _h or\
53 " 0\nSECTION\n 2\nOBJECTS\n" in _h or\
54 " 0\nSECTION\n 2\nTHUMBNAILIMAGE\n" in _h) and \
55 _l[-19:] == " 0\nENDSEC\n 0\nEOF\n":
56 return 'dxf'
57 imghdr.tests.append(test_dxf)
58
59
60 def mapping(string, tuple):
61 """mapping(string, tuple)
62
63 string: a message string
64 tuple: a truple with string items
65 Return the string replacing the $[n] words whith its corresponding value
66 from the tuple.
67 It is used because the gettext module can not #-#supotr#-# strings as:
68 "Invalid type (%s) in record: %s" %(type, record)
69 """
70 for _index in range(len(tuple)):
71 string = string.replace("$" + str(_index+1), str(tuple[_index]))
72 return string
73
74 def eliminate_duplicates(list):
75 """eliminate_duplicates(list)
76
77 Return a copy of the list without duplicate values
78 """
79 _result = [ ]
80 for item in list:
81 if item not in _result:
82 _result.append(item)
83 return _result
84
85 def is_valid_code(code):
86 """is_valid_code(code)
87
88 code: a string code
89 Funtion to test if a record code is valid
90 A valid code must fulfill:
91 - Be a not empty string
92 - The valid characters are the defined in MSdos 6.0 including .$#%&_
93 What it means? I am not sure, so I test if all the character
94 are in cp850
95 - Cannot contain the following characters
96 <~> separator of records if FIEBDC-3
97 <|> separator of fields if FIEBDC-3
98 <\> separator of subfield in FIEBDC-3
99 <\t> tab -> control character
100 < > space -> control character
101 <\n> end of line -> control character
102 <\r> end of line -> control character
103 - Cannot end with <#> or <##>, root and chapter code record
104 It return a tuple (is_valid, code)
105 is_valid (True/False)
106 True: the code is valid
107 False: the code is not valid
108 code(False/code)
109 False: the code is not valid and can not be corrected
110 code: the code or the corrected code
111 """
112 _is_valid = True
113 if not isinstance(code, str):
114 return False, False
115 if code == "":
116 return False, False
117 try:
118 _unicode_code = unicode(code, "utf8")
119 _code_cp850 = _unicode_code.encode("cp850")
120 _unicode_code = unicode(_code_cp850, "cp850")
121 _code_utf8 = _unicode_code.encode("utf8")
122 except UnicodeError:
123 return False, False
124 if _code_utf8 != code:
125 _is_valid = False
126 if _code_utf8 == "":
127 return False, False
128 code = _code_utf8
129 _code2 = re.sub("[\t \n\r~|\\\]","",code)
130 if _code2 != code:
131 if _code2 == "":
132 return False, False
133 _is_valid = False
134 code = _code2
135 if code[-1] == "#":
136 _is_valid = False
137 while code[-1] == "#":
138 code = code[:-1]
139 if code == "":
140 return False, False
141 return _is_valid, code
142
143 def getFiletype(filename, h=None):
144 """getFiletype(filename, h=None):
145
146 filename: the filename to test
147 h: raw string, if h is not None the filename is ignored and h is assumed
148 to contain the byte stream to test
149 """
150 _type = imghdr.what(filename, h)
151 _image_types = ["rgb", "gif", "pbm", "pgm", "ppm", "tiff", "rast", "xbm",
152 "jpeg", "bmp", "png", "wmf"]
153 if _type in _image_types:
154 return "image"
155 elif _type == "dxf":
156 return "dxf"
157 ## _video_types = ["avi", "mpg", "mkv", "ogm"]
158 ## elif _type in _video_types:
159 ## return "video"
160 ## elif _type == "pdf":
161 ## return "pdf"
162 ## elif _type == "ppt" or _type == "odp":
163 ## return "presentation"
164 else:
165 return None