annotate configmix/ini.py @ 207:b3b5ed34d180

Handle most flake8 errors and warnings. NOTE: E265 "block comment should start with '# ' ist not yet handled. We would need to adjust our Python style.
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 05 May 2019 18:29:47 +0200
parents b5ce9a8461bf
children bbe8513ea649
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: 56
diff changeset
2 #-
156
e2e8d21b4122 Adjust copyright to year 2019
Franz Glasner <fzglas.hg@dom66.de>
parents: 153
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: 56
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: 56
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
7c095c6223b8 Module comment for ini.py
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
7
7c095c6223b8 Module comment for ini.py
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
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
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
12 import sys
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
13 import os
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
14 import io
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
15 import locale
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
16 try:
149
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
17 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
18 # 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
19 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
20 _ConfigParserBase = ConfigParser
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
21 else:
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
22 from configparser import SafeConfigParser
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
23 _ConfigParserBase = SafeConfigParser
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
24 except ImportError:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
25 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
26 _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
27 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
28 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
29 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
30 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
31 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
32 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
33 DictImpl = dict
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
34
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 166
diff changeset
35 from .compat import u, u2fs
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
36
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
37
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
38 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError",
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
39 "load"]
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
40
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
41
149
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
42 class INIConfigParser(_ConfigParserBase):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
43
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
44 """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
45 values.
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
46
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 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
50 _ConfigParserBase.__init__(self)
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
51 if executable is None:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
52 executable = sys.argv[0]
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
53 filename = u(filename, locale.getpreferredencoding())
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
54 executable = u(executable, locale.getpreferredencoding())
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
55 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
56 if encoding is None:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
57 encoding = locale.getpreferredencoding()
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
58 self.encoding = encoding
166
b5ce9a8461bf Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
59 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
60 self.read_file(cf, filename)
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
61
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
62 def optionxform(self, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
63 return option
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
64
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
65 def get_path_list(self, section, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
66 v = self.get(section, option)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
67 return v.split(os.pathsep)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
68
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
69 def read(self, filenames):
153
4b10bd85610b FIX: Typo
Franz Glasner <hg@dom66.de>
parents: 152
diff changeset
70 """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
71 raise NotImplementedError("use `read_file()' instead")
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
72
152
139fb1d1ef54 For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents: 151
diff changeset
73 def readfp(self, fp, filename):
139fb1d1ef54 For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents: 151
diff changeset
74 """Compatibility for older Python versions.
139fb1d1ef54 For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents: 151
diff changeset
75
139fb1d1ef54 For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents: 151
diff changeset
76 Use :meth:`.read_file` instead.
139fb1d1ef54 For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents: 151
diff changeset
77
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 return self.read_file(fp, filename)
139fb1d1ef54 For compatibility reasons: provide a INIConfigParser.readfp() also
Franz Glasner <hg@dom66.de>
parents: 151
diff changeset
80
148
be352645871c Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 120
diff changeset
81 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
82 """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
83
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
84 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
85 `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
86
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
87 """
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
88 if hasattr(self, "filename"):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
89 raise RuntimeError("already initialized")
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
90 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
91 filename = u(filename, locale.getpreferredencoding())
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 20
diff changeset
92 self.set(None, u("self"), filename)
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 20
diff changeset
93 self.set(None, u("here"), os.path.dirname(filename))
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 20
diff changeset
94 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
95 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
96 _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
97 else:
149
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
98 _ConfigParserBase.readfp(self, fp, filename=filename)
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
99 self.filename = filename
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
100 self.root = os.path.dirname(self.executable)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
101
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
102 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
103 """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
104
56
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
105 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
106
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
107 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
108 ``: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
109 float.
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
110
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 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
113 if v.startswith(u(":bool:")):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
114 v = v[6:].lower()
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
115 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
116 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
117 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
118 elif v.startswith(u(":int:")):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
119 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
120 elif v.startswith(u(":float:")):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
121 return float(v[7:])
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
122 else:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
123 return v
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
124
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 20
diff changeset
125 _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
126 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
127 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
128 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
129 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
130 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
131 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
132 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
133
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
134 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
135 """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
136 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
137
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 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
139 `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
140
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 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 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
151
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 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
153 """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
154 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
155
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 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
158
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
159
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
160 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
161 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
162 """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
163 `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
164
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 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
166
97
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
167 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
168 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
169
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
170 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
171
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
172 """
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 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 # 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
185 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
186 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
187 sections.sort()
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
188 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
189 cur_cfg = conf
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 return conf