annotate Generic/utils.py @ 25:189f8274aecd

gui. switch navigation menu on switch page
author Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
date Mon, 20 May 2019 13:18:33 +0200
parents 65e7ae0d0e63
children 16f91684686b
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
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
23 import re
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
24 import imghdr
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
25 import os.path
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
26
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
27 # add svg to imghdr
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
28 def test_svg(h, f):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
29 """SVG """
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
30 if isinstance(f,file):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
31 _pos = f.tell()
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
32 f.seek(0)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
33 _h = f.read(32)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
34 f.seek(-32, 2)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
35 _l = f.read(32)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
36 f.seek(_pos)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
37 else:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
38 _h = h
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
39 _l = h[-32:]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
40 if "<?xml" in _h and "</svg>" in _l:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
41 return 'svg'
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
42 imghdr.tests.append(test_svg)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
43
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
44 # add ico to imghdr
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
45 def test_ico(h, f):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
46 """ico image file"""
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
47 if h[:4] == "\x00\x00\x01\x00":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
48 return 'ico'
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
49 imghdr.tests.append(test_ico)
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
50
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
51 # add wmf to imghdr
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
52 def test_pdf(h, f):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
53 """pdf file"""
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
54 if h[:4] == "%PDF":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
55 return 'pdf'
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
56 imghdr.tests.append(test_pdf)
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
57
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
58 # add wmf to imghdr
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
59 def test_wmf(h, f):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
60 """wmf image library"""
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
61 if h[:6] == "\xd7\xcd\xc6\x9a\x00\x00":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
62 return 'wmf'
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
63 imghdr.tests.append(test_wmf)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
64
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
65 # add dxf to imghdr
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
66 def test_dxf(h, f):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
67 """AutoCAD DXF: Drawing Interchange Format"""
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
68 if isinstance(f,file):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
69 _pos = f.tell()
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
70 f.seek(0)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
71 _h = f.read(128)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
72 f.seek(-32, 2)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
73 _l = f.read(32)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
74 f.seek(_pos)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
75 else:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
76 _h = h
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
77 _l = h[-32:]
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
78 _h = _h.replace("\r","")
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
79 _l = _l.replace("\r","")
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
80 if (" 0\nSECTION\n 2\nHEADER\n" in _h or\
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
81 " 0\nSECTION\n 2\nCLASSES\n" in _h or\
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
82 " 0\nSECTION\n 2\nTABLES\n" in _h or\
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
83 " 0\nSECTION\n 2\nBLOCKS\n" in _h or\
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
84 " 0\nSECTION\n 2\nENTITIES\n" in _h or\
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
85 " 0\nSECTION\n 2\nOBJECTS\n" in _h or\
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
86 " 0\nSECTION\n 2\nTHUMBNAILIMAGE\n" in _h) and \
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
87 _l[-19:] == " 0\nENDSEC\n 0\nEOF\n":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
88 return 'dxf'
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
89 imghdr.tests.append(test_dxf)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
90
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
91
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
92 def mapping(string, tuple_strings):
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
93 """mapping(string, tuple)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
94
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
95 string: a message string
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
96 tuple_strings: a truple with string items
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
97 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
98 from the tuple.
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
99 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
100 "Invalid type (%s) in record: %s" %(type, record)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
101 """
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
102 for _index in range(len(tuple_strings)):
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
103 string = string.replace("$" + str(_index+1), tuple_strings[_index])
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
104 #string = string.replace("$" + str(_index+1), str(tuple[_index]))
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
105 return string
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
106
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
107 def eliminate_duplicates(list):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
108 """eliminate_duplicates(list)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
109
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
110 Return a copy of the list without duplicate values
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
111 """
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
112 _result = [ ]
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
113 for item in list:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
114 if item not in _result:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
115 _result.append(item)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
116 return _result
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
117
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
118 def is_valid_code(code):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
119 """is_valid_code(code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
120
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
121 code: a string code
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
122 Funtion to test if a record code is valid
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
123 A valid code must fulfill:
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
124 - Be a not empty string
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
125 - 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
126 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
127 are in cp850
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
128 - Cannot contain the following characters
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
129 <~> separator of records if FIEBDC-3
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
130 <|> separator of fields if FIEBDC-3
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
131 <\> separator of subfield in FIEBDC-3
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
132 <\t> tab -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
133 < > space -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
134 <\n> end of line -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
135 <\r> end of line -> control character
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
136 - Cannot end with <#> or <##>, root and chapter code record
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
137 It return a tuple (is_valid, code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
138 is_valid (True/False)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
139 True: the code is valid
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
140 False: the code is not valid
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
141 code(False/code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
142 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
143 code: the code or the corrected code
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
144 """
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
145 _is_valid = True
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
146 if not isinstance(code, str):
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
147 print("Not a string, code: " + code + type(code) )
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
148 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
149 if code == "":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
150 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
151 try:
17
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
152 _unicode_code = unicode(code, "utf8",'replace')
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
153 _code_utf8 = _unicode_code.encode("utf8",'replace')
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
154 _code_cp850 = _unicode_code.encode("cp850",'replace')
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
155 _unicode_code = unicode(_code_cp850, "cp850",'replace')
a7b9f7e7dfa4 Improvements importing FIEBDC files
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 1
diff changeset
156
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
157 except UnicodeError:
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
158 print ("Unicode Error, code: " + code )
1
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 if _code_utf8 != code:
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
161 print ("Not in cp950, code: " + code )
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
162 _is_valid = False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
163 if _code_utf8 == "":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
164 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
165 code = _code_utf8
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
166 _code2 = re.sub("[\t \n\r~|\\\]","",code)
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
167 if _code2 != code:
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
168 print("Control characters in code: " + code )
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
169 if _code2 == "":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
170 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
171 _is_valid = False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
172 code = _code2
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
173 if code[-1] == "#":
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
174 print("# in code: " + code )
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
175 _is_valid = False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
176 while code[-1] == "#":
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
177 code = code[:-1]
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
178 if code == "":
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
179 print("Empty code")
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
180 return False, False
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
181 return _is_valid, code
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
182
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
183 def getFiletype(filename, h=None):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
184 """getFiletype(filename, h=None):
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
185
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
186 filename: the filename to test
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
187 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
188 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
189
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
190 valid types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
191 "image", "wmf", "dxf", "pdf" , "video",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
192 "office-document", "office-presentation", "office-spreadsheet",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
193 "html", "rtf", "txt"
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
194 """
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
195 _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
196
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
197 _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
198 "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
199 "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
200 "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
201 "asf", "svi", "3gp", "3g2", "mxf", "roq", "nsv"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
202 _document_types = ["doc", "docx", "odt"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
203 _spreadsheet_types = ["xls", "xlsx", "ods"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
204 _presentation_types = ["pps", "ppsx", "ppt", "pptx", "odp"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
205 _html_types = ["html", "xhtml"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
206 if _ext in _video_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
207 return "video"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
208 elif _ext in _document_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
209 return "office-document"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
210 elif _ext in _spreadsheet_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
211 return "office-spreadsheet"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
212 elif _ext in _presentation_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
213 return "office-presentation"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
214 elif _ext in _html_types:
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
215 return "html"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
216 elif _ext == "rtf":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
217 return "rtf"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
218 elif _ext == "txt":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
219 return "txt"
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
220 _type = imghdr.what(filename, h)
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
221 _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
222 "xbm", "jpeg", "jpg", "bmp", "png", "webp", "exr",
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
223 "ico", "svg"]
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
224 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
225 return "image"
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
226 elif _type == "wmf" and _ext == "wmf":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
227 return "wmf"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
228 elif _type == "dxf" and _ext == "dxf":
1
2ac1551ad2ab add code
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents:
diff changeset
229 return "dxf"
23
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
230
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
231 elif _type == "pdf" and _ext == "pdf":
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
232 return "pdf"
65e7ae0d0e63 GTK2 to GTK3
Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es>
parents: 17
diff changeset
233 return None