Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/compat.py @ 156:e2e8d21b4122
Adjust copyright to year 2019
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 21 Feb 2019 22:16:05 +0100 |
| parents | 218807d7d883 |
| children | b5ce9a8461bf |
| 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 -*- |
|
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
|
2 #- |
|
156
e2e8d21b4122
Adjust copyright to year 2019
Franz Glasner <fzglas.hg@dom66.de>
parents:
82
diff
changeset
|
3 # :Copyright: (c) 2015-2019, 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. |
|
a43749f751e0
Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents:
54
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 |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
12 import sys |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
13 import locale |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
14 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
15 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
16 __all__ = ["PY2", |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
17 "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
|
18 "native_os_str_to_text", |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
19 "u"] |
|
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 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
22 PY2 = sys.version_info[0] <= 2 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
23 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
24 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
25 if PY2: |
|
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 _OS_ENCODING = locale.getpreferredencoding() |
|
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 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
|
30 if isinstance(s, unicode): |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
31 return s.encode(encoding or _OS_ENCODING) |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
32 else: |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
33 return s |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
34 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
35 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
36 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
|
37 return s.decode(encoding or _OS_ENCODING) |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
38 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
39 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
40 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
|
41 if isinstance(s, unicode): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
42 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
43 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
44 return s.decode(encoding) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
45 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
46 else: |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
47 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
48 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
|
49 return s |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
50 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
51 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
52 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
|
53 return s |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
54 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
55 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
|
56 if isinstance(s, str): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
57 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
58 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
59 return s.decode(encoding) |
