Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/compat.py @ 31:50721b43e76c v0.2
+++++ v0.2
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Fri, 18 Mar 2016 09:28:34 +0100 |
| parents | f85dc4677c01 |
| children | aa8345dae995 |
| 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 -*- |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
2 r"""Some minimal compatibility between Python2 and Python3 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
3 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
4 """ |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
5 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
6 from __future__ import division, absolute_import, print_function |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
7 |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
8 import sys |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
9 import locale |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
10 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
11 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
12 __all__ = ["PY2", |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
13 "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
|
14 "native_os_str_to_text", |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
15 "u"] |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
16 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
17 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
18 PY2 = sys.version_info[0] <= 2 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
19 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
20 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
21 if PY2: |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
22 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
23 _OS_ENCODING = locale.getpreferredencoding() |
|
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 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
|
26 if isinstance(s, unicode): |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
27 return s.encode(encoding or _OS_ENCODING) |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
28 else: |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
29 return s |
|
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 |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
32 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
|
33 return s.decode(encoding or _OS_ENCODING) |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
34 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
35 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
36 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
|
37 if isinstance(s, unicode): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
38 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
39 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
40 return s.decode(encoding) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
41 |
|
15
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
42 else: |
|
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 text_to_native_os_str(s, encoding=None): |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
45 return s |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
46 |
|
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 native_os_str_to_text(s, encoding=None): |
|
0b1292e920af
Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
5
diff
changeset
|
49 return s |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
50 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
51 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
|
52 if isinstance(s, str): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
53 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
54 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
15
diff
changeset
|
55 return s.decode(encoding) |
