Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/compat.py @ 284:4aaf74858d07
Some links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws moduleSome links to AWS docu into the aws module
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 07 Dec 2020 01:59:11 +0100 |
| parents | ff964825a75a |
| children | eed16a1ec8f3 |
| 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 # :- |
|
237
13711ba8e81e
Adjust copyright year to 2020
Franz Glasner <fzglas.hg@dom66.de>
parents:
209
diff
changeset
|
3 # :Copyright: (c) 2015-2020, Franz Glasner. All rights reserved. |
|
79
a43749f751e0
Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
4 # :License: 3-clause BSD. 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", |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
17 "u2fs"] |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
18 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
19 |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
20 import sys |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
21 import os |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
22 import locale |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
23 |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
24 |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
25 PY2 = sys.version_info[0] <= 2 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
26 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
27 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
28 if PY2: |
|
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 _OS_ENCODING = locale.getpreferredencoding() |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
31 |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
32 _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
|
33 |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
34 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
35 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
|
36 if isinstance(s, unicode): # noqa: F821 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
37 return s.encode(encoding or _OS_ENCODING) |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
38 else: |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
39 return s |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
40 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
41 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
42 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
|
43 return s.decode(encoding or _OS_ENCODING) |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
44 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
45 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
46 def u(s, encoding="utf-8"): |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
47 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
|
48 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
49 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
50 return s.decode(encoding) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
51 |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
52 |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
53 def u2fs(s, force=False): |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
54 """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
|
55 |
|
209
c8d071581a4c
Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents:
208
diff
changeset
|
56 .. 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
|
57 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
|
58 |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
59 .. 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
|
60 return `s` unchanged. |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
61 |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
62 """ |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
63 if isinstance(s, str): |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
64 return s |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
65 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
|
66 # 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
|
67 return s |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
68 return s.encode(_FS_ENCODING) |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
69 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
70 else: |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
71 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
72 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
|
73 return s |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
74 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
75 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
76 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
|
77 return s |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
78 |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
79 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
80 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
|
81 if isinstance(s, str): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
82 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
83 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
84 return s.decode(encoding) |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
85 |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
86 |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
87 def u2fs(s, force=False): |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
88 """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
|
89 |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
90 .. 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
|
91 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
|
92 |
|
209
c8d071581a4c
Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents:
208
diff
changeset
|
93 .. 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
|
94 return `s` unchanged. |
|
c8d071581a4c
Doc: adjust documentation of configmix.compat slightly
Franz Glasner <fzglas.hg@dom66.de>
parents:
208
diff
changeset
|
95 |
|
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 assert isinstance(s, str) |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
98 return s |
