annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
1 #!/usr/bin/python
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
3 ## File utils.py
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
4 ## This file is part of pyArq-Presupuestos.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
5 ##
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
6 ## Copyright (C) 2010-2019 Miguel Ángel Bárcena Rodríguez
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
7 ## <miguelangel@obraencurso.es>
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
8 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
10 ## it under the terms of the GNU General Public License as published by
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
12 ## (at your option) any later version.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
13 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
14 ## pyArq-Presupuestos is distributed in the hope that it will be useful,
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
17 ## GNU General Public License for more details.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
18 ##
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
20 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
21
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
22 # Modules
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
23
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
24 # python 2/3 compatibility
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
25 from __future__ import absolute_import, division, print_function, unicode_literals
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
26 from builtins import str as text
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
27 from six import text_type
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
28 from io import IOBase
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
29
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
30 import re
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
31 import imghdr
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
32 import os.path
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
33
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
34
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
35 # add svg to imghdr
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
36 def test_svg(h, f):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
37 """SVG """
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
38 if isinstance(f,IOBase):
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
39 _pos = f.tell()
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
40 f.seek(0)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
41 _h = f.read(32)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
42 f.seek(-32, 2)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
43 _l = f.read(32)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
44 f.seek(_pos)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
45 else:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
46 _h = h
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
47 _l = h[-32:]
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
48 if b"<?xml" in _h and b"</svg>" in _l:
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
49 return 'svg'
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
50 imghdr.tests.append(test_svg)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
51
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
52 # add ico to imghdr
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
53 def test_ico(h, f):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
54 """ico image file"""
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
55 if h[:4] == b"\x00\x00\x01\x00":
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
56 return 'ico'
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
57 imghdr.tests.append(test_ico)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
58
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
59 # add wmf to imghdr
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
60 def test_pdf(h, f):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
61 """pdf file"""
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
62 if h[:4] == b"%PDF":
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
63 return 'pdf'
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
64 imghdr.tests.append(test_pdf)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
65
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
66 # add wmf to imghdr
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
67 def test_wmf(h, f):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
68 """wmf image library"""
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
69 if h[:6] == b"\xd7\xcd\xc6\x9a\x00\x00":
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
70 return 'wmf'
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
71 imghdr.tests.append(test_wmf)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
72
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
73 # add dxf to imghdr
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
74 def test_dxf(h, f):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
75 """AutoCAD DXF: Drawing Interchange Format"""
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
76 if isinstance(f,IOBase):
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
77 _pos = f.tell()
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
78 f.seek(0)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
79 _h = f.read(128)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
80 f.seek(-32, 2)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
81 _l = f.read(32)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
82 f.seek(_pos)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
83 else:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
84 _h = h
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
85 _l = h[-32:]
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
86 _h = _h.replace(b"\r",b"")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
87 _l = _l.replace(b"\r",b"")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
88 if (b" 0\nSECTION\n 2\nHEADER\n" in _h or\
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
89 b" 0\nSECTION\n 2\nCLASSES\n" in _h or\
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
90 b" 0\nSECTION\n 2\nTABLES\n" in _h or\
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
91 b" 0\nSECTION\n 2\nBLOCKS\n" in _h or\
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
92 b" 0\nSECTION\n 2\nENTITIES\n" in _h or\
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
93 b" 0\nSECTION\n 2\nOBJECTS\n" in _h or\
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
94 b" 0\nSECTION\n 2\nTHUMBNAILIMAGE\n" in _h) and \
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
95 _l[-19:] == b" 0\nENDSEC\n 0\nEOF\n":
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
96 return 'dxf'
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
97 imghdr.tests.append(test_dxf)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
98
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
99
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
100 def mapping(string, tuple_strings):
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
101 """mapping(string, tuple)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
102
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
103 string: a message string
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
104 tuple_strings: a truple with string items
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
105 Return the string replacing the $[n] words whith its corresponding value
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
106 from the tuple.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
107 It is used because the gettext module can not #-#supotr#-# strings as:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
108 "Invalid type (%s) in record: %s" %(type, record)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
109 """
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
110 for _index in range(len(tuple_strings)):
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
111 string = string.replace("$" + text(_index+1), tuple_strings[_index])
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
112 return string
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
113
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
114 def eliminate_duplicates(list):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
115 """eliminate_duplicates(list)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
116
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
117 Return a copy of the list without duplicate values
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
118 """
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
119 _result = [ ]
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
120 for item in list:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
121 if item not in _result:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
122 _result.append(item)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
123 return _result
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
124
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
125 def is_valid_code(code):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
126 """is_valid_code(code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
127
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
128 code: a string code
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
129 Funtion to test if a record code is valid
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
130 A valid code must fulfill:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
131 - Be a not empty string
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
132 - The valid characters are the defined in MSdos 6.0 including .$#%&_
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
133 What it means? I am not sure, so I test if all the character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
134 are in cp850
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
135 - Cannot contain the following characters
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
136 <~> separator of records if FIEBDC-3
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
137 <|> separator of fields if FIEBDC-3
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
138 <\> separator of subfield in FIEBDC-3
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
139 <\t> tab -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
140 < > space -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
141 <\n> end of line -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
142 <\r> end of line -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
143 - Cannot end with <#> or <##>, root and chapter code record
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
144 It return a tuple (is_valid, code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
145 is_valid (True/False)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
146 True: the code is valid
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
147 False: the code is not valid
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
148 code(False/code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
149 False: the code is not valid and can not be corrected
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
150 code: the code or the corrected code
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
151 """
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
152 _is_valid = True
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
153 if not isinstance(code, text_type):
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
154 _tuni = _("Not a text string, code: $1, type: $2")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
155 _uni = mapping(_tuni, (code ,text(type(code))))
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
156 print(_uni)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
157 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
158 if code == "":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
159 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
160 try:
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
161 #_unicode_code = unicode(code, "utf-8",'replace')
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
162 _unicode_code = code
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
163 _code_utf8 = _unicode_code.encode("utf-8",'replace')
17
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
164 _code_cp850 = _unicode_code.encode("cp850",'replace')
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
165 _unicode_code = text(_code_cp850, "cp850",'replace')
17
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
166
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
167 except UnicodeError:
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
168 _tuni = _("Unicode Error, code: $1")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
169 _uni = mapping(_tuni, (code, ))
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
170 print (_uni)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
171 return False, False
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
172 if code != _unicode_code:
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
173 _tuni = _("Not in cp850, code: $1")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
174 _uni = mapping(_tuni, (code, ))
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
175 print (_uni)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
176 _is_valid = False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
177 if _code_utf8 == "":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
178 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
179 code = _code_utf8
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
180 _code2 = re.sub("[\t \n\r~|\\\]","",code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
181 if _code2 != code:
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
182 _tuni = _("Control characters in code: $1")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
183 _uni = mapping(_tuni, (code, ))
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
184 print(_uni)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
185 if _code2 == "":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
186 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
187 _is_valid = False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
188 code = _code2
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
189 if code[-1] == "#":
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
190 _tuni = _("# in code: $1")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
191 _uni = mapping(_tuni, (code, ))
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
192 print(_uni)
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
193 _is_valid = False
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
194 while code[-1] == "#":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
195 code = code[:-1]
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
196 if code == "":
26
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
197 _tuni = _("Empty code")
16f91684686b Upgrade to python 3. Keep python 2/3 compatibility
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 23
diff changeset
198 print(_tuni)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
199 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
200 return _is_valid, code
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
201
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
202 def getFiletype(filename, h=None):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
203 """getFiletype(filename, h=None):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
204
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
205 filename: the filename to test
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
206 h: raw string, if h is not None the filename is ignored and h is assumed
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
207 to contain the byte stream to test
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
208
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
209 valid types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
210 "image", "wmf", "dxf", "pdf" , "video",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
211 "office-document", "office-presentation", "office-spreadsheet",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
212 "html", "rtf", "txt"
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
213 """
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
214 _ext = os.path.splitext(filename)[1][1:].lower()
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
215
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
216 _video_types = ["avi", "mp4", "m4p", "m4v2", "m4v","amv", "mpg", "m2v",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
217 "mp2", "mpe", "mpv", "mpeg", "ogg", "ogv", "webm", "mkv",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
218 "ogm", "flv", "f4v", "f4p", "f4a", "f4b", "vob", "drc",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
219 "mts", "m2ts", "mov", "qt", "wmv", "yuv", "rm", "rmvb",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
220 "asf", "svi", "3gp", "3g2", "mxf", "roq", "nsv"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
221 _document_types = ["doc", "docx", "odt"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
222 _spreadsheet_types = ["xls", "xlsx", "ods"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
223 _presentation_types = ["pps", "ppsx", "ppt", "pptx", "odp"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
224 _html_types = ["html", "xhtml"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
225 if _ext in _video_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
226 return "video"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
227 elif _ext in _document_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
228 return "office-document"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
229 elif _ext in _spreadsheet_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
230 return "office-spreadsheet"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
231 elif _ext in _presentation_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
232 return "office-presentation"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
233 elif _ext in _html_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
234 return "html"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
235 elif _ext == "rtf":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
236 return "rtf"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
237 elif _ext == "txt":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
238 return "txt"
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
239 _type = imghdr.what(filename, h)
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
240 _image_types = ["rgb", "gif", "pbm", "pgm", "ppm" ,"tiff", "tif", "rast",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
241 "xbm", "jpeg", "jpg", "bmp", "png", "webp", "exr",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
242 "ico", "svg"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
243 if _type in _image_types and _ext in _image_types:
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
244 return "image"
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
245 elif _type == "wmf" and _ext == "wmf":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
246 return "wmf"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
247 elif _type == "dxf" and _ext == "dxf":
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
248 return "dxf"
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
249
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
250 elif _type == "pdf" and _ext == "pdf":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
251 return "pdf"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
252 return None