annotate configmix/compat.py @ 79:a43749f751e0

Put a copyright and license note into every source file of the configmix package
author Franz Glasner <hg@dom66.de>
date Wed, 14 Mar 2018 23:58:47 +0100
parents aa8345dae995
children 218807d7d883
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 -*-
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 #-
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
3 # :Copyright: (c) 2015-2018, Franz Glasner. All rights reserved.
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 #-
54
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 16
diff changeset
6 r"""
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 16
diff changeset
7 configmix.compat
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 16
diff changeset
8 ^^^^^^^^^^^^^^^^
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 16
diff changeset
9
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 16
diff changeset
10 Some minimal compatibility between Python2 and Python3
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
11
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
12 """
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
13
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
14 from __future__ import division, absolute_import, print_function
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
15
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
16 import sys
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
17 import locale
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
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
20 __all__ = ["PY2",
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
21 "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
22 "native_os_str_to_text",
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
23 "u"]
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
24
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
25
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
26 PY2 = sys.version_info[0] <= 2
15
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
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
29 if PY2:
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 _OS_ENCODING = locale.getpreferredencoding()
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 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
34 if isinstance(s, unicode):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
35 return s.encode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
36 else:
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
37 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
38
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
39
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
40 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
41 return s.decode(encoding or _OS_ENCODING)
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
42
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
43
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
44 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
45 if isinstance(s, unicode):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
46 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
47 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
48 return s.decode(encoding)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
49
15
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
50 else:
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 text_to_native_os_str(s, encoding=None):
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
53 return s
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
54
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
55
0b1292e920af Variables: namespaces and filters
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 5
diff changeset
56 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
57 return s
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
58
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
59 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
60 if isinstance(s, str):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
61 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
62 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents: 15
diff changeset
63 return s.decode(encoding)