annotate configmix/compat.py @ 472:89bbb66aff02 v0.19.2

+++++ v0.19.2
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 16 Dec 2021 09:30:43 +0100
parents b96f49c9c76b
children d7f6f2afcee2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
diff changeset
2 # :-
359
ce2a8f5a2fb2 Extend copyright year in compat.py to 2021
Franz Glasner <fzglas.hg@dom66.de>
parents: 348
diff changeset
3 # :Copyright: (c) 2015-2021, Franz Glasner. All rights reserved.
296
eed16a1ec8f3 Use SPDX license identifiers (either full or short) all over the package
Franz Glasner <fzglas.hg@dom66.de>
parents: 250
diff changeset
4 # :License: BSD-3-Clause. See LICENSE.txt for details.
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
diff changeset
5 # :-
82
218807d7d883 Remove header markup from the Python files and put them into the doc .rst files
Franz Glasner <hg@dom66.de>
parents: 79
diff changeset
6 """Some minimal compatibility shim between Python2 and Python3
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
7
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
8 """
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
9
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
10 from __future__ import division, absolute_import, print_function
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
11
250
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
12
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
13 __all__ = ["PY2",
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
14 "text_to_native_os_str",
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
15 "native_os_str_to_text",
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
16 "u",
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
17 "u2fs",
432
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
18 "uchr",
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
19 "n"]
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
20
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
21
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
22 import sys
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
23 import os
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
24 import locale
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
25
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
26
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
27 PY2 = sys.version_info[0] <= 2
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
28
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
29
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
30 if PY2:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
31
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
32 _OS_ENCODING = locale.getpreferredencoding()
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
33
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
34 _FS_ENCODING = sys.getfilesystemencoding() or _OS_ENCODING
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
35
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
36
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
37 def text_to_native_os_str(s, encoding=None):
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
38 if isinstance(s, unicode): # noqa: F821
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
39 return s.encode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
40 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
41 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
42
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
43
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
44 def native_os_str_to_text(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
45 return s.decode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
46
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
47
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
48 def u(s, encoding="utf-8"):
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
49 if isinstance(s, unicode): # noqa: F821
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
50 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
51 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
52 return s.decode(encoding)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
53
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
54
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
55 def u2fs(s, force=False):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
56 """Convert a text (Unicode) string to the filesystem encoding.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
57
209
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
58 .. note:: The filesystem encoding on Python 3 is a Unicode text
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
59 string. The function is a noop when called on Python 3.
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
60
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
61 .. note:: If `s` is already a byte string be permissive and
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
62 return `s` unchanged.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
63
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
64 """
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
65 if isinstance(s, str):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
66 return s
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
67 if not force and os.name in ("nt", "ce"):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
68 # WinNT and CE have native Unicode support: nothing to convert
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
69 return s
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
70 return s.encode(_FS_ENCODING)
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
71
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
72 def uchr(n):
348
396d8d9aaead Mark the use of "unichr()" with "noqa: F821" because of flake8 complaints when running it with Python3
Franz Glasner <fzglas.hg@dom66.de>
parents: 320
diff changeset
73 return unichr(n) # noqa: F821
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
74
432
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
75 def n(s, encoding="utf-8"):
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
76 if isinstance(s, str):
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
77 return s
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
78 else:
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
79 return s.encode(encoding)
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
80
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
81 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
82
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
83 def text_to_native_os_str(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
84 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
85
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
86
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
87 def native_os_str_to_text(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
88 return s
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
89
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
90
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
91 def u(s, encoding="utf-8"):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
92 if isinstance(s, str):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
93 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
94 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
95 return s.decode(encoding)
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
96
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
97
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
98 def u2fs(s, force=False):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
99 """Convert a text (Unicode) string to the filesystem encoding.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
100
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
101 .. note:: The filesystem encoding on Python 3 is a Unicode text
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
102 string. The function is a noop when called on Python 3.
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
103
209
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
104 .. note:: If `s` is already a byte string be permissive and
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
105 return `s` unchanged.
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
106
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
107 """
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
108 assert isinstance(s, str)
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
109 return s
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
110
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
111 def uchr(n):
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
112 return chr(n)
432
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
113
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
114 def n(s, encoding="utf-8"):
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
115 if isinstance(s, str):
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
116 return s
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
117 else:
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
118 return s.decode(encoding)