Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/ini.py @ 97:1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Sun, 18 Mar 2018 19:15:01 +0100 |
| parents | 218807d7d883 |
| children | ba5970a2dcef |
| 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 | 7 |
| 8 """ | |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
9 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
10 from __future__ import division, absolute_import, print_function |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
11 |
|
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: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
17 from configparser import SafeConfigParser, NoSectionError, NoOptionError |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
18 except ImportError: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 DictImpl = dict |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
27 |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
28 from .compat import PY2, u |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
29 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
30 |
|
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
|
31 __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
|
32 "load"] |
|
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 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
35 class INIConfigParser(SafeConfigParser): |
|
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 """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
|
38 values. |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
39 |
|
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 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
42 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
|
43 SafeConfigParser.__init__(self) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
44 if executable is None: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
45 executable = sys.argv[0] |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
46 if PY2: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
47 if isinstance(filename, str): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
48 filename = filename.decode(locale.getpreferredencoding()) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
49 if isinstance(executable, str): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
50 executable = executable.decode(locale.getpreferredencoding()) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
51 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
|
52 if encoding is None: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
53 encoding = locale.getpreferredencoding() |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
54 self.encoding = encoding |
| 14 | 55 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
|
56 self.readfp(cf, filename) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
57 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
58 def optionxform(self, option): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
59 return option |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
60 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
61 def get_path_list(self, section, option): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
62 v = self.get(section, option) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
63 return v.split(os.pathsep) |
|
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 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
|
66 """Not implemented. Use :meth:`readfp` instead""" |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
67 raise NotImplementedError("use `readfp()' instead") |
|
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 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
|
70 """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
|
71 |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
72 The `fp` argument must have a `readline()` method. |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
73 |
|
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
74 """ |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
75 if hasattr(self, "filename"): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
76 raise RuntimeError("already initialized") |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
77 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
|
78 if PY2: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
79 if isinstance(filename, str): |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
80 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
|
81 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
|
82 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
|
83 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
|
84 SafeConfigParser.readfp(self, fp, filename=filename) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
85 self.filename = filename |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
86 self.root = os.path.dirname(self.executable) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
87 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
88 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
|
89 """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
|
90 |
|
56
1f11672c4615
Optimize the documentation: make references working with Sphinx using :role:`target`
Franz Glasner <hg@dom66.de>
parents:
54
diff
changeset
|
91 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
|
92 |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
93 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
|
94 ``: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
|
95 float. |
|
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 """ |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
98 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
|
99 if v.startswith(u(":bool:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
100 v = v[6:].lower() |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 elif v.startswith(u(":int:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
105 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
|
106 elif v.startswith(u(":float:")): |
|
2
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
107 return float(v[7:]) |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
108 else: |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
109 return v |
|
9981a68040b6
An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff
changeset
|
110 |
|
21
ce290b10dac5
Better Py2/Py3 compatibility: mark some strings explicitly as Unicode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
20
diff
changeset
|
111 _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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 |
|
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
|
120 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
|
121 """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
|
122 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
|
123 |
|
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 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
|
125 `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
|
126 |
|
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 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
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 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
|
139 """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
|
140 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
|
141 |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
142 """ |
|
7c7955da42ab
An extended `itemsx()` method for INI-style configuration files to get interpreted selected options from a section
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
21
diff
changeset
|
143 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
|
144 |
|
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
|
145 |
|
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
|
146 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
|
147 """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
|
148 `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
|
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 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
|
151 |
|
97
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
152 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
|
153 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
|
154 |
|
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
|
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 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 # 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
|
168 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
|
169 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
|
170 sections.sort() |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
171 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
|
172 cur_cfg = conf |
|
1b4d95f60650
Build a tree-ish configuration from an INI style configuration file
Franz Glasner <hg@dom66.de>
parents:
82
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 return conf |
