annotate configmix/ini.py @ 151:c46b0f82922a

FIX: INIConfigParser.read_file(): correctly document the requirements for the file argument for different Python versions
author Franz Glasner <hg@dom66.de>
date Sat, 14 Apr 2018 17:11:41 +0200
parents 0ac6ffae969f
children 139fb1d1ef54
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 #-
a43749f751e0 Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents: 56
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: 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
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 20
diff changeset
35 from .compat import PY2, u
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]
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
53 if PY2:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
54 if isinstance(filename, str):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
55 filename = filename.decode(locale.getpreferredencoding())
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
56 if isinstance(executable, str):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
57 executable = executable.decode(locale.getpreferredencoding())
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
58 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
59 if encoding is None:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
60 encoding = locale.getpreferredencoding()
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
61 self.encoding = encoding
14
36fa039f7e11 Formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 7
diff changeset
62 with io.open(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
63 self.read_file(cf, filename)
2
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 optionxform(self, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
66 return option
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
67
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
68 def get_path_list(self, section, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
69 v = self.get(section, option)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
70 return v.split(os.pathsep)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
71
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
72 def read(self, filenames):
148
be352645871c Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 120
diff changeset
73 """Not implemented. Use :meth:`read_file` instead"""
be352645871c Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 120
diff changeset
74 raise NotImplementedError("use `read_file()' instead")
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
75
148
be352645871c Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 120
diff changeset
76 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
77 """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
78
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
79 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
80 `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
81
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
82 """
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
83 if hasattr(self, "filename"):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
84 raise RuntimeError("already initialized")
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
85 filename = os.path.normpath(os.path.abspath(filename))
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
86 if PY2:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
87 if isinstance(filename, str):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
88 filename = filename.decode(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
89 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
90 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
91 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
92 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
93 _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
94 else:
149
614a0a648f48 Work around the deprecation of SafeConfigParser in Python 3.2+
Franz Glasner <hg@dom66.de>
parents: 148
diff changeset
95 _ConfigParserBase.readfp(self, fp, filename=filename)
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
96 self.filename = filename
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
97 self.root = os.path.dirname(self.executable)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
98
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
99 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
100 """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
101
56
1f11672c4615 Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents: 54
diff changeset
102 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
103
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
104 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
105 ``: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
106 float.
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 """
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
109 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
110 if v.startswith(u(":bool:")):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
111 v = v[6:].lower()
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
112 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
113 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
114 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
115 elif v.startswith(u(":int:")):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
116 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
117 elif v.startswith(u(":float:")):
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
118 return float(v[7:])
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
119 else:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
120 return v
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
121
21
ce290b10dac5 Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 20
diff changeset
122 _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
123 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
124 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
125 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
126 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
127 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
128 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
129 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
130
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
131 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
132 """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
133 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
134
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 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
136 `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
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 """
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 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
140 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
141 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
142 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
143 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
144 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
145 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
146 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
147 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
148
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 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
150 """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
151 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
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 """
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 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
155
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
156
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
157 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
158 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
159 """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
160 `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
161
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 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
163
97
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
164 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
165 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
166
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
167 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
168
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
169 """
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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 # try to read "<extract>.xxx" sections as tree
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
182 for treemarker in [ e + '.' for e in extract ]:
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
183 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
184 sections.sort()
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
185 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
186 cur_cfg = conf
1b4d95f60650 Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents: 82
diff changeset
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 return conf