Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/ini.py @ 296:eed16a1ec8f3
Use SPDX license identifiers (either full or short) all over the package
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 10 Feb 2021 21:38:03 +0100 |
| parents | 2cfd670281ae |
| children | f454889e41fa |
| 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:
208
diff
changeset
|
3 # :Copyright: (c) 2015-2020, 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:
260
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 """Read INI-style configuration files. |
| 7 | 7 |
| 8 """ | |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
9 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
10 from __future__ import division, absolute_import, print_function |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
11 |
|
250
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
12 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
13 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError", |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
14 "load"] |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
15 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
16 |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
17 import sys |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
18 import os |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
19 import io |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
20 import locale |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
21 try: |
|
149
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
22 from configparser import ConfigParser, NoSectionError, NoOptionError |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
23 # SafeConfigParser is deprecated in Python 3.2+ (together with "readfp()") |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
24 if hasattr(ConfigParser, "read_file"): |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
25 _ConfigParserBase = ConfigParser |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
26 else: |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
27 from configparser import SafeConfigParser |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
28 _ConfigParserBase = SafeConfigParser |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
29 except ImportError: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
30 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError |
|
149
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
31 _ConfigParserBase = SafeConfigParser |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
32 try: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
33 from collections import OrderedDict as DictImpl |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
34 except ImportError: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
35 try: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
36 from ordereddict import OrderedDict as DictImpl |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
37 except ImportError: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
38 DictImpl = dict |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
39 |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
40 from .compat import u, u2fs |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
41 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
42 |
|
149
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
43 class INIConfigParser(_ConfigParserBase): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
44 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
45 """A case sensitive config parser that returns all-unicode string |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
46 values. |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
47 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
48 """ |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
49 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
50 def __init__(self, filename, executable=None, encoding=None): |
|
149
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
51 _ConfigParserBase.__init__(self) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
52 if executable is None: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
53 executable = sys.argv[0] |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
54 filename = u(filename, locale.getpreferredencoding()) |
|
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
55 executable = u(executable, locale.getpreferredencoding()) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
56 self.executable = os.path.normpath(os.path.abspath(executable)) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
57 if encoding is None: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
58 encoding = locale.getpreferredencoding() |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
59 self.encoding = encoding |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
60 with io.open(u2fs(filename), mode="rt", encoding=self.encoding) as cf: |
|
148
be352645871c
Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
120
diff
changeset
|
61 self.read_file(cf, filename) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
62 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
63 def optionxform(self, option): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
64 return option |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
65 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
66 def get_path_list(self, section, option): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
67 v = self.get(section, option) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
68 return v.split(os.pathsep) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
69 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
70 def read(self, filenames): |
| 153 | 71 """Not implemented. Use :meth:`read_file` instead.""" |
|
148
be352645871c
Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
120
diff
changeset
|
72 raise NotImplementedError("use `read_file()' instead") |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
73 |
|
152
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
74 def readfp(self, fp, filename): |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
75 """Compatibility for older Python versions. |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
76 |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
77 Use :meth:`.read_file` instead. |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
78 |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
79 """ |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
80 return self.read_file(fp, filename) |
|
139fb1d1ef54
For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents:
151
diff
changeset
|
81 |
|
148
be352645871c
Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
120
diff
changeset
|
82 def read_file(self, fp, filename): |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
83 """Read from a file-like object `fp`. |
|
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
84 |
|
151
c46b0f82922a
FIX: INIConfigParser.read_file(): correctly document the requirements for the file argument for different Python versions
Franz Glasner <hg@dom66.de>
parents:
150
diff
changeset
|
85 The `fp` argument must be iterable (Python 3.2+) or have a |
|
c46b0f82922a
FIX: INIConfigParser.read_file(): correctly document the requirements for the file argument for different Python versions
Franz Glasner <hg@dom66.de>
parents:
150
diff
changeset
|
86 `readline()` method (Python 2, <3.2). |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
87 |
|
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
88 """ |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
89 if hasattr(self, "filename"): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
90 raise RuntimeError("already initialized") |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
91 filename = os.path.normpath(os.path.abspath(filename)) |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
92 filename = u(filename, locale.getpreferredencoding()) |
|
260
2cfd670281ae
Do not set "root", "self" and "here" variables any more.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
93 # self.set(None, u("self"), filename) |
|
2cfd670281ae
Do not set "root", "self" and "here" variables any more.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
94 # self.set(None, u("here"), os.path.dirname(filename)) |
|
2cfd670281ae
Do not set "root", "self" and "here" variables any more.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
95 # self.set(None, u("root"), os.path.dirname(self.executable)) |
|
149
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
96 if hasattr(_ConfigParserBase, "read_file"): |
|
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
97 _ConfigParserBase.read_file(self, fp, source=filename) |
|
148
be352645871c
Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
120
diff
changeset
|
98 else: |
|
149
614a0a648f48
Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents:
148
diff
changeset
|
99 _ConfigParserBase.readfp(self, fp, filename=filename) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
100 self.filename = filename |
|
260
2cfd670281ae
Do not set "root", "self" and "here" variables any more.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
101 # self.root = os.path.dirname(self.executable) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
102 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
103 def getx(self, section, option): |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
104 """Extended `get()` with some automatic type conversion support. |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
105 |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
106 Default: Fetch as string (like :meth:`get`). |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
107 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
108 If annotated with ``:bool:`` fetch as bool, if annotated with |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
109 ``:int:`` fetch as int, if annotated with ``:float:`` fetch as |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
110 float. |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
111 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
112 """ |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
113 v = self.get(section, option) |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
114 if v.startswith(u(":bool:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
115 v = v[6:].lower() |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
116 if v not in self._BOOL_CVT: |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
117 raise ValueError("Not a boolean: %r" % v) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
118 return self._BOOL_CVT[v] |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
119 elif v.startswith(u(":int:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
120 return int(v[5:], 0) |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
121 elif v.startswith(u(":float:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
122 return float(v[7:]) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
123 else: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
124 return v |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
125 |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
126 _BOOL_CVT = {u('1'): True, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
127 u('yes'): True, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
128 u('true'): True, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
129 u('on'): True, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
130 u('0'): False, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
131 u('no'): False, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
132 u('false'): False, |
|
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
133 u('off'): False} |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
134 |
|
28
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
135 def itemsx(self, section, options): |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
136 """Get all the options given in `options` of section `section`. |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
137 Fetch them with :meth:`getx` in the order given. |
|
28
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
138 |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
139 Return a list of ``(name, value)`` pairs for each option in |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
140 `options` in the given `section`. |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
141 |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
142 """ |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
143 d = [] |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
144 for option in options: |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
145 try: |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
146 val = self.getx(section, option) |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
147 except (NoSectionError, NoOptionError): |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
148 pass |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
149 else: |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
150 d.append((option, val, )) |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
151 return d |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
152 |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
153 def items_as_dictx(self, section, options): |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
154 """Similar to :meth:`itemsx` but return a (possibly ordered) |
|
28
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
155 dict instead of a list of key-value pairs. |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
156 |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
157 """ |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
158 return DictImpl(self.itemsx(section, options)) |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
159 |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
160 |
|
120
ba5970a2dcef
The default file encoding when reading INI style files with configmix.ini.load() is now "UTF-8".
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
97
diff
changeset
|
161 def load(filename, extract=["config"], |
|
ba5970a2dcef
The default file encoding when reading INI style files with configmix.ini.load() is now "UTF-8".
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
97
diff
changeset
|
162 encoding="utf-8"): |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
163 """Load a single INI file and read/interpolate the sections given in |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
164 `extract`. |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
165 |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
166 Flattens the given sections into the resulting dictionary. |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
167 |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
168 Then build a tree out of sections which start with any of the `extract` |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
169 content value and a point ``.``. |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
170 |
|
120
ba5970a2dcef
The default file encoding when reading INI style files with configmix.ini.load() is now "UTF-8".
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
97
diff
changeset
|
171 The encoding of the file is given in `encoding`. |
|
ba5970a2dcef
The default file encoding when reading INI style files with configmix.ini.load() is now "UTF-8".
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
97
diff
changeset
|
172 |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
173 """ |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
174 conf = DictImpl() |
|
120
ba5970a2dcef
The default file encoding when reading INI style files with configmix.ini.load() is now "UTF-8".
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
97
diff
changeset
|
175 ini = INIConfigParser(filename, encoding=encoding) |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
176 for sect in extract: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
177 try: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
178 cfg = ini.options(sect) |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
179 except NoSectionError: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
180 pass |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
181 else: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
182 for option in cfg: |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
183 value = ini.getx(sect, option) |
|
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
184 conf[option] = value |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
185 # try to read "<extract>.xxx" sections as tree |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
186 for treemarker in [e + '.' for e in extract]: |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
187 sections = list(ini.sections()) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
188 sections.sort() |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
189 for section in sections: |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
190 cur_cfg = conf |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
191 if section.startswith(treemarker): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
192 treestr = section[len(treemarker):] |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
193 for treepart in treestr.split('.'): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
194 cur_cfg = cur_cfg.setdefault(treepart, DictImpl()) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
195 for option in ini.options(section): |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
196 value = ini.getx(section, option) |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
197 cur_cfg[option] = value |
|
20
9bdc4e421415
A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
14
diff
changeset
|
198 return conf |
