annotate configmix/compat.py @ 602:a2fff0d93d83

Split up fast_pathstr2path into internal implementation and a simple wrapper
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 11 Jan 2022 00:52:56 +0100
parents f454889e41fa
children
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 # :-
593
f454889e41fa Adjust copyright year (the end) to 2022
Franz Glasner <fzglas.hg@dom66.de>
parents: 487
diff changeset
3 # :Copyright: (c) 2015-2022, 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",
487
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
19 "n",
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
20 "str_and_u"]
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
21
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
22
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
23 import sys
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
24 import os
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
25 import locale
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
26
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
27
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
28 PY2 = sys.version_info[0] <= 2
15
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
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
31 if PY2:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
32
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
33 _OS_ENCODING = locale.getpreferredencoding()
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
34
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
35 _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
36
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
37
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
38 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
39 if isinstance(s, unicode): # noqa: F821
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
40 return s.encode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
41 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
42 return s
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
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
45 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
46 return s.decode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
47
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
48
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
49 def u(s, encoding="utf-8"):
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
50 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
51 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
52 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
53 return s.decode(encoding)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
54
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
55
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
56 def u2fs(s, force=False):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
57 """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
58
209
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
59 .. 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
60 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
61
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
62 .. 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
63 return `s` unchanged.
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 """
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
66 if isinstance(s, str):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
67 return s
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
68 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
69 # 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
70 return s
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
71 return s.encode(_FS_ENCODING)
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
72
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
73 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
74 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
75
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
76 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
77 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
78 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
79 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
80 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
81
487
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
82 def str_and_u(v):
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
83 if isinstance(v, unicode): # noqa: F821
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
84 return v
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
85 else:
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
86 return u(str(v))
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
87
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
88 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
89
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
90 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
91 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
92
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
93
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
94 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
95 return s
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
96
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
97
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
98 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
99 if isinstance(s, str):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
100 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
101 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
102 return s.decode(encoding)
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
103
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
104
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
105 def u2fs(s, force=False):
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
106 """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
107
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
108 .. 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
109 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
110
209
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
111 .. 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
112 return `s` unchanged.
c8d071581a4c Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents: 208
diff changeset
113
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
114 """
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
115 assert isinstance(s, str)
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
116 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
117
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
118 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
119 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
120
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 359
diff changeset
121 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
122 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
123 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
124 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
125 return s.decode(encoding)
487
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
126
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
127 def str_and_u(v):
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
128 """Convert the value in `v` of any type to a native string and then
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
129 to text (Unicode)
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
130
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
131 """
d7f6f2afcee2 Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 432
diff changeset
132 return str(v)