Sat, 26 Apr 2025 12:34:32 +0200
MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.
5888
f23f3d2b7516
Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
f23f3d2b7516
Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10928
diff
changeset
|
3 | # Copyright (c) 2017 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> |
5888
f23f3d2b7516
Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
f23f3d2b7516
Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
f23f3d2b7516
Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
7 | Package containing utility modules and functions. |
5888
f23f3d2b7516
Added a cache for the already determined completion lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
9 | |
10928
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
10 | import codecs |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
11 | import contextlib |
10926
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
12 | import os |
10928
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
13 | import re |
10926
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
14 | |
10928
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
15 | import chardet |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
16 | import semver |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
17 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
18 | from PyQt6.QtCore import QByteArray, QCoreApplication |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
19 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
20 | ############################################################################### |
10926
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
21 | ## Functions dealing with the configuration directory. |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
22 | ############################################################################### |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
23 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
24 | _configDir = None |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
25 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
26 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
27 | def getConfigDir(): |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
28 | """ |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
29 | Module function to get the name of the directory storing the config data. |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
30 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
31 | @return directory name of the config dir |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
32 | @rtype str |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
33 | """ |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
34 | if _configDir is not None and os.path.exists(_configDir): |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
35 | return _configDir |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
36 | else: |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
37 | confDir = os.path.join(os.path.expanduser("~"), ".eric7") |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
38 | if not os.path.exists(confDir): |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
39 | os.mkdir(confDir) |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
40 | return confDir |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
41 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
42 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
43 | def setConfigDir(d): |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
44 | """ |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
45 | Module function to set the name of the directory storing the config data. |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
46 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
47 | @param d name of an existing directory |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
48 | @type str |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
49 | """ |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
50 | global _configDir |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
51 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
52 | _configDir = os.path.expanduser(d) |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
53 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
54 | |
9ef616cd220d
Moved some functions from 'Globals' to 'EricUtilities'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10921
diff
changeset
|
55 | ############################################################################### |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
56 | ## Functions for converting QSetting return types to valid types. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
57 | ############################################################################### |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
58 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
59 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
60 | def toBool(value): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
61 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
62 | Function to convert a value to bool. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
63 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
64 | @param value value to be converted |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
65 | @type str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
66 | @return converted data |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
67 | @rtype bool |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
68 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
69 | if (isinstance(value, str) and value.lower() in ["true", "1", "yes"]) or ( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
70 | isinstance(value, bytes) and value.lower() in [b"true", b"1", b"yes"] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
71 | ): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
72 | return True |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
73 | elif (isinstance(value, str) and value.lower() in ["false", "0", "no"]) or ( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
74 | isinstance(value, bytes) and value.lower() in [b"false", b"0", b"no"] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
75 | ): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
76 | return False |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
77 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
78 | return bool(value) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
79 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
80 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
81 | def toList(value): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
82 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
83 | Function to convert a value to a list. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
84 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
85 | @param value value to be converted |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
86 | @type None, list or Any |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
87 | @return converted data |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
88 | @rtype list |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
89 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
90 | if value is None: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
91 | return [] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
92 | elif not isinstance(value, list): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
93 | return [value] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
94 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
95 | return value |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
96 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
97 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
98 | def toByteArray(value): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
99 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
100 | Function to convert a value to a byte array. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
101 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
102 | @param value value to be converted |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
103 | @type QByteArray or None |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
104 | @return converted data |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
105 | @rtype QByteArray |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
106 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
107 | if value is None: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
108 | return QByteArray() |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
109 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
110 | return value |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
111 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
112 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
113 | def toDict(value): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
114 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
115 | Function to convert a value to a dictionary. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
116 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
117 | @param value value to be converted |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
118 | @type dict or None |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
119 | @return converted data |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
120 | @rtype dict |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
121 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
122 | if value is None: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
123 | return {} |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
124 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
125 | return value |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
126 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
127 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
128 | ############################################################################### |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
129 | ## Functions for version handling. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
130 | ############################################################################### |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
131 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
132 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
133 | def versionIsValid(version): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
134 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
135 | Function to check, if the given version string is valid. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
136 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
137 | @param version version string |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
138 | @type str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
139 | @return flag indicating validity |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
140 | @rtype bool |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
141 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
142 | try: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
143 | return semver.VersionInfo.is_valid(version) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
144 | except AttributeError: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
145 | return semver.VersionInfo.isvalid(version) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
146 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
147 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
148 | def versionToTuple(version): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
149 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
150 | Function to convert a version string into a tuple. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
151 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
152 | Note: A version string consists of non-negative decimals separated by "." |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
153 | optionally followed by a suffix. Suffix is everything after the last |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
154 | decimal. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
155 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
156 | @param version version string |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
157 | @type str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
158 | @return version named tuple containing the version parts |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
159 | @rtype semver.VersionInfo |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
160 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
161 | while version and not version[0].isdecimal(): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
162 | # sanitize version string (get rid of leading non-decimal characters) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
163 | version = version[1:] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
164 | |
10921
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
165 | while version.count(".") < 2: |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
166 | # ensure the version string contains at least three parts |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
167 | version += ".0" |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
168 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
169 | if versionIsValid(version): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
170 | return semver.VersionInfo.parse(version) |
10921
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
171 | elif version.count(".") > 2: |
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
172 | v = ".".join(version.split(".")[:3]) |
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
173 | if versionIsValid(v): |
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
174 | return semver.VersionInfo.parse(v) |
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
175 | |
495f084a737e
Fixed an issue converting a version string with more than 3 parts into a semver.VersionInfo object.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
176 | return semver.VersionInfo(0, 0, 0) |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
177 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
178 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
179 | ############################################################################### |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
180 | ## Functions for extended string handling. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
181 | ############################################################################### |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
182 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
183 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
184 | def strGroup(txt, sep, groupLen=4): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
185 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
186 | Function to group a string into sub-strings separated by a |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
187 | separator. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
188 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
189 | @param txt text to be grouped |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
190 | @type str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
191 | @param sep separator string |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
192 | @type str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
193 | @param groupLen length of each group |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
194 | @type int |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
195 | @return result string |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
196 | @rtype str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
197 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
198 | groups = [] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
199 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
200 | while len(txt) // groupLen != 0: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
201 | groups.insert(0, txt[-groupLen:]) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
202 | txt = txt[:-groupLen] |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
203 | if len(txt) > 0: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
204 | groups.insert(0, txt) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
205 | return sep.join(groups) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
206 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
207 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
208 | def strToQByteArray(txt): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
209 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
210 | Function to convert a Python string into a QByteArray. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
211 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
212 | @param txt Python string to be converted |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
213 | @type str, bytes, bytearray |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
214 | @return converted QByteArray |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
215 | @rtype QByteArray |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
216 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
217 | if isinstance(txt, str): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
218 | txt = txt.encode("utf-8") |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
219 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
220 | return QByteArray(txt) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
221 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
222 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
223 | def dataString(size, loc=None): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
224 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
225 | Function to generate a formatted size string. |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
226 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
227 | @param size size to be formatted |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
228 | @type int |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
229 | @param loc locale to be used for localized size strings (defaults to None) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
230 | @type QLocale (optional) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
231 | @return formatted data string |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
232 | @rtype str |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
233 | """ |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
234 | if loc is None: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
235 | if size < 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
236 | return QCoreApplication.translate("EricUtilities", "{0:4d} Bytes").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
237 | size |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
238 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
239 | elif size < 1024 * 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
240 | size /= 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
241 | return QCoreApplication.translate("EricUtilities", "{0:4.2f} KiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
242 | size |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
243 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
244 | elif size < 1024 * 1024 * 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
245 | size /= 1024 * 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
246 | return QCoreApplication.translate("EricUtilities", "{0:4.2f} MiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
247 | size |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
248 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
249 | elif size < 1024 * 1024 * 1024 * 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
250 | size /= 1024 * 1024 * 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
251 | return QCoreApplication.translate("EricUtilities", "{0:4.2f} GiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
252 | size |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
253 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
254 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
255 | size /= 1024 * 1024 * 1024 * 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
256 | return QCoreApplication.translate("EricUtilities", "{0:4.2f} TiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
257 | size |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
258 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
259 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
260 | if not isinstance(size, float): |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
261 | size = float(size) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
262 | |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
263 | if size < 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
264 | return QCoreApplication.translate("EricUtilities", "{0} Bytes").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
265 | loc.toString(size) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
266 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
267 | elif size < 1024 * 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
268 | size /= 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
269 | return QCoreApplication.translate("EricUtilities", "{0} KiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
270 | loc.toString(size, "f", 2) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
271 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
272 | elif size < 1024 * 1024 * 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
273 | size /= 1024 * 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
274 | return QCoreApplication.translate("EricUtilities", "{0} MiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
275 | loc.toString(size, "f", 2) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
276 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
277 | elif size < 1024 * 1024 * 1024 * 1024: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
278 | size /= 1024 * 1024 * 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
279 | return QCoreApplication.translate("EricUtilities", "{0} GiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
280 | loc.toString(size, "f", 2) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
281 | ) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
282 | else: |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
283 | size /= 1024 * 1024 * 1024 * 1024 |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
284 | return QCoreApplication.translate("EricUtilities", "{0} TiB").format( |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
285 | loc.toString(size, "f", 2) |
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
286 | ) |
10928
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
287 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
288 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
289 | def decodeString(text): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
290 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
291 | Function to decode a string containing Unicode encoded characters. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
292 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
293 | @param text text containing encoded chars |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
294 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
295 | @return decoded text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
296 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
297 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
298 | buf = b"" |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
299 | index = 0 |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
300 | while index < len(text): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
301 | if text[index] == "\\": |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
302 | qb = QByteArray.fromHex(text[index : index + 4].encode()) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
303 | buf += bytes(qb) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
304 | index += 4 |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
305 | else: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
306 | buf += codecs.encode(text[index], "utf-8") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
307 | index += 1 |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
308 | buf = buf.replace(b"\x00", b"") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
309 | return decodeBytes(buf) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
310 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
311 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
312 | def decodeBytes(buffer): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
313 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
314 | Function to decode some byte text into a string. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
315 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
316 | @param buffer byte buffer to decode |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
317 | @type bytes |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
318 | @return decoded text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
319 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
320 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
321 | # try UTF with BOM |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
322 | with contextlib.suppress(UnicodeError, LookupError): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
323 | if buffer.startswith(codecs.BOM_UTF8): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
324 | # UTF-8 with BOM |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
325 | return str(buffer[len(codecs.BOM_UTF8) :], encoding="utf-8") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
326 | elif buffer.startswith(codecs.BOM_UTF16): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
327 | # UTF-16 with BOM |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
328 | return str(buffer[len(codecs.BOM_UTF16) :], encoding="utf-16") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
329 | elif buffer.startswith(codecs.BOM_UTF32): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
330 | # UTF-32 with BOM |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
331 | return str(buffer[len(codecs.BOM_UTF32) :], encoding="utf-32") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
332 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
333 | # try UTF-8 |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
334 | with contextlib.suppress(UnicodeError): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
335 | return str(buffer, encoding="utf-8") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
336 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
337 | # try codec detection |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
338 | try: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
339 | guess = chardet.detect(buffer) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
340 | if guess and guess["encoding"] is not None: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
341 | codec = guess["encoding"].lower() |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
342 | return str(buffer, encoding=codec) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
343 | except (LookupError, UnicodeError): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
344 | pass |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
345 | except ImportError: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
346 | pass |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
347 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
348 | return str(buffer, encoding="utf-8", errors="ignore") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
349 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
350 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
351 | def readStringFromStream(stream): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
352 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
353 | Module function to read a string from the given stream. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
354 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
355 | @param stream data stream opened for reading |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
356 | @type QDataStream |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
357 | @return string read from the stream |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
358 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
359 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
360 | data = stream.readString() |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
361 | if data is None: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
362 | data = b"" |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
363 | return data.decode("utf-8") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
364 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
365 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
366 | ############################################################################### |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
367 | ## Functions for HTML string handling. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
368 | ############################################################################### |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
369 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
370 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
371 | _escape = re.compile("[&<>\"'\u0080-\uffff]") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
372 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
373 | _escape_map = { |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
374 | "&": "&", |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
375 | "<": "<", |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
376 | ">": ">", |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
377 | '"': """, |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
378 | "'": "'", |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
379 | } |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
380 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
381 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
382 | def escape_entities(m, escmap=_escape_map): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
383 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
384 | Function to encode html entities. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
385 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
386 | @param m the match object |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
387 | @type re.Match |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
388 | @param escmap the map of entities to encode |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
389 | @type dict |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
390 | @return the converted text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
391 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
392 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
393 | char = m.group() |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
394 | text = escmap.get(char) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
395 | if text is None: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
396 | text = "&#{0:d};".format(ord(char)) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
397 | return text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
398 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
399 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
400 | def html_encode(text, pattern=_escape): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
401 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
402 | Function to correctly encode a text for html. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
403 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
404 | @param text text to be encoded |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
405 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
406 | @param pattern search pattern for text to be encoded |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
407 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
408 | @return the encoded text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
409 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
410 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
411 | if not text: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
412 | return "" |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
413 | text = pattern.sub(escape_entities, text) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
414 | return text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
415 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
416 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
417 | _uescape = re.compile("[\u0080-\uffff]") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
418 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
419 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
420 | def escape_uentities(m): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
421 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
422 | Function to encode html entities. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
423 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
424 | @param m the match object |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
425 | @type re.Match |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
426 | @return the converted text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
427 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
428 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
429 | char = m.group() |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
430 | text = "&#{0:d};".format(ord(char)) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
431 | return text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
432 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
433 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
434 | def html_uencode(text, pattern=_uescape): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
435 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
436 | Function to correctly encode a unicode text for html. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
437 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
438 | @param text text to be encoded |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
439 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
440 | @param pattern search pattern for text to be encoded |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
441 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
442 | @return the encoded text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
443 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
444 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
445 | if not text: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
446 | return "" |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
447 | text = pattern.sub(escape_uentities, text) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
448 | return text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
449 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
450 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
451 | _uunescape = re.compile(r"&#\d+;") |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
452 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
453 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
454 | def unescape_uentities(m): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
455 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
456 | Function to decode html entities. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
457 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
458 | @param m the match object |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
459 | @type re.Match |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
460 | @return the converted text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
461 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
462 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
463 | char = m.group() |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
464 | ordinal = int(char[2:-1]) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
465 | return chr(ordinal) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
466 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
467 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
468 | def html_udecode(text, pattern=_uunescape): |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
469 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
470 | Function to correctly decode a html text to a unicode text. |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
471 | |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
472 | @param text text to be decoded |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
473 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
474 | @param pattern search pattern for text to be decoded |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
475 | @type str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
476 | @return the decoded text |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
477 | @rtype str |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
478 | """ |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
479 | if not text: |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
480 | return "" |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
481 | text = pattern.sub(unescape_uentities, text) |
46651e194fbe
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10926
diff
changeset
|
482 | return text |