Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/ini.py @ 79:a43749f751e0
Put a copyright and license note into every source file of the configmix package
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Wed, 14 Mar 2018 23:58:47 +0100 |
| parents | 1f11672c4615 |
| children | 218807d7d883 |
| 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 #- |
|
54
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
28
diff
changeset
|
6 r""" |
|
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
28
diff
changeset
|
7 configmix.ini |
|
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
28
diff
changeset
|
8 ^^^^^^^^^^^^^ |
|
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
28
diff
changeset
|
9 |
|
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
28
diff
changeset
|
10 Read INI-style configuration files. |
| 7 | 11 |
| 12 """ | |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
13 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
14 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
|
15 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
16 import sys |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
17 import os |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
18 import io |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
19 import locale |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
20 try: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
21 from configparser import SafeConfigParser, NoSectionError, NoOptionError |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
22 except ImportError: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
23 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError |
|
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
|
24 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
|
25 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
|
26 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
|
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 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
|
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 DictImpl = dict |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
31 |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
32 from .compat import PY2, u |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
33 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
34 |
|
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
|
35 __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
|
36 "load"] |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
37 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
38 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
39 class INIConfigParser(SafeConfigParser): |
|
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 """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
|
42 values. |
|
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 """ |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
45 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
46 def __init__(self, filename, executable=None, encoding=None): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
47 SafeConfigParser.__init__(self) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
48 if executable is None: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
49 executable = sys.argv[0] |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
50 if PY2: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
51 if isinstance(filename, str): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
52 filename = filename.decode(locale.getpreferredencoding()) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
53 if isinstance(executable, str): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
54 executable = executable.decode(locale.getpreferredencoding()) |
|
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 |
| 14 | 59 with io.open(filename, mode="rt", encoding=self.encoding) as cf: |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
60 self.readfp(cf, filename) |
|
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): |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
70 """Not implemented. Use :meth:`readfp` instead""" |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
71 raise NotImplementedError("use `readfp()' instead") |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
72 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
73 def readfp(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
|
74 """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
|
75 |
|
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
76 The `fp` argument must have a `readline()` method. |
|
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
77 |
|
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
78 """ |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
79 if hasattr(self, "filename"): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
80 raise RuntimeError("already initialized") |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
81 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
|
82 if PY2: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
83 if isinstance(filename, str): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
84 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
|
85 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
|
86 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
|
87 self.set(None, u("root"), os.path.dirname(self.executable)) |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
88 SafeConfigParser.readfp(self, fp, filename=filename) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
89 self.filename = filename |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
90 self.root = os.path.dirname(self.executable) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
91 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
92 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
|
93 """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
|
94 |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
95 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
|
96 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
97 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
|
98 ``: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
|
99 float. |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
100 |
|
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 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
|
103 if v.startswith(u(":bool:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
104 v = v[6:].lower() |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 elif v.startswith(u(":int:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
109 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
|
110 elif v.startswith(u(":float:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
111 return float(v[7:]) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
112 else: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
113 return v |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
114 |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
115 _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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 |
|
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
|
124 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
|
125 """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
|
126 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
|
127 |
|
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
|
128 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
|
129 `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
|
130 |
|
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 """ |
|
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 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
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 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
|
143 """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
|
144 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
|
145 |
|
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 """ |
|
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 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
|
148 |
|
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
|
149 |
|
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
|
150 def load(filename, extract=["config"]): |
|
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
|
151 """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
|
152 `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
|
153 |
|
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
|
154 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
|
155 |
|
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 """ |
|
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
|
157 conf = 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
|
158 ini = INIConfigParser(filename) |
|
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 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 conf[option] = value |
|
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
|
168 return conf |
