Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/config.py @ 293:6f0bf673d4ff
Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it.
Currently only the YAML loader understands this flag.
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Wed, 10 Feb 2021 15:25:55 +0100 |
| parents | 2a8dcab2de8c |
| children | eed16a1ec8f3 |
| rev | line source |
|---|---|
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
208
bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents:
207
diff
changeset
|
2 # :- |
|
237
13711ba8e81e
Adjust copyright year to 2020
Franz Glasner <fzglas.hg@dom66.de>
parents:
208
diff
changeset
|
3 # :Copyright: (c) 2015-2020, Franz Glasner. All rights reserved. |
|
79
a43749f751e0
Put a copyright and license note into every source file of the configmix package
Franz Glasner <hg@dom66.de>
parents:
56
diff
changeset
|
4 # :License: 3-clause BSD. See LICENSE.txt for details. |
|
208
bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents:
207
diff
changeset
|
5 # :- |
|
82
218807d7d883
Remove header markup from the Python files and put them into the doc .rst files
Franz Glasner <hg@dom66.de>
parents:
79
diff
changeset
|
6 """The unified configuration dictionary with attribute support or |
|
54
aa8345dae995
Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents:
40
diff
changeset
|
7 variable substitution. |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
8 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
9 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
10 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
11 from __future__ import division, absolute_import, print_function |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
12 |
|
250
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
13 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
14 __all__ = ["Configuration"] |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
15 |
|
ff964825a75a
Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents:
249
diff
changeset
|
16 |
|
39
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
17 import warnings |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
18 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
19 from collections import OrderedDict as ConfigurationBase |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
20 except ImportError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
21 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
22 from ordereddict import OrderedDict as ConfigurationBase |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
23 except ImportError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
24 ConfigurationBase = dict |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
25 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
26 from .variables import lookup_varns, lookup_filter |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
27 from .compat import u |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
28 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
29 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
30 _MARKER = object() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
31 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
32 |
|
17
94b5e94fae44
Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
16
diff
changeset
|
33 class _AttributeDict(ConfigurationBase): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
34 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
35 def __getattr__(self, name): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
36 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
37 v = self[name] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
38 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
39 raise AttributeError("%s has no attribute %r" % (type(self), name)) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
40 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
41 # Wrap a dict into another dict with attribute access support |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
42 if isinstance(v, dict): |
|
29
17af7a78710c
FIX: Renaming a class was not really complete
Franz Glasner <hg@dom66.de>
parents:
25
diff
changeset
|
43 return _AttributeDict(v) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
44 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
45 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
46 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
47 |
|
17
94b5e94fae44
Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
16
diff
changeset
|
48 class Configuration(_AttributeDict): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
49 |
| 18 | 50 """The configuration dictionary with attribute support or |
| 51 variable substitution. | |
| 52 | |
| 53 .. note:: When retriving by attribute names variables will *not* | |
| 54 substituted. | |
| 55 | |
| 56 """ | |
| 57 | |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
58 def getvar(self, varname, default=_MARKER): |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
59 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
60 variables from other namespaces. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
61 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
62 No variable interpolation is done and no filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
63 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
64 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
65 varns, varname = self._split_ns(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
66 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
67 if not varns: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
68 lookupfn = self._lookupvar |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
69 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
70 lookupfn = lookup_varns(varns) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
71 varvalue = lookupfn(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
72 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
73 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
74 raise KeyError("Variable %r not found" % varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
75 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
76 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
77 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
78 return varvalue |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
79 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
80 def getvar_s(self, varname, default=_MARKER): |
|
24
fa65adab0b71
FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
23
diff
changeset
|
81 """Get a variable - including variables from other namespaces. |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
82 |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
83 `varname` is interpreted as in :meth:`.getvar`. But variables |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
84 will be interpolated recursively within the variable values |
|
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
85 and filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
86 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
87 For more details see chapter :ref:`variable-interpolation`. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
88 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
89 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
90 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
91 obj = self.getvar(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
92 return self.substitute_variables_in_obj(obj) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
93 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
94 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
95 raise |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
96 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
97 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
98 |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
99 def getintvar_s(self, varname, default=_MARKER): |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
100 """Get a (possibly substituted) variable and coerce text to a |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
101 number. |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
102 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
103 """ |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
104 s = self.getvar_s(varname, default) |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
105 if isinstance(s, self._TEXTTYPE): |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
106 return int(s, 0) |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
107 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
108 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
109 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
110 def getboolvar_s(self, varname, default=_MARKER): |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
111 """Get a (possibly substituted) variable and convert text to a |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
112 boolean |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
113 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
114 """ |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
115 s = self.getvar_s(varname, default) |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
116 if isinstance(s, self._TEXTTYPE): |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
117 sl = s.strip().lower() |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
118 if sl not in self._BOOL_CVT: |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
119 raise ValueError("Not a boolean: %r" % s) |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
120 return self._BOOL_CVT[sl] |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
121 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
122 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
123 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
124 # Conversion of booleans |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
125 _BOOL_CVT = {'1': True, 'yes': True, 'true': True, 'on': True, |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
126 '0': False, 'no': False, 'false': False, 'off': False} |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
127 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
128 def getfloatvar_s(self, varname, default=_MARKER): |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
129 """Get a (possibly substituted) variable and convert text to a |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
130 float |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
131 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
132 """ |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
133 s = self.getvar_s(varname, default) |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
134 if isinstance(s, self._TEXTTYPE): |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
135 return float(s) |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
136 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
137 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
138 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
139 def _split_ns(self, s): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
140 nameparts = s.split(':', 1) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
141 if len(nameparts) == 1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
142 return (None, s, ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
143 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
144 return (nameparts[0], nameparts[1], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
145 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
146 def _split_filters(self, s): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
147 nameparts = s.split('|') |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
148 if len(nameparts) == 1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
149 return (s, [], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
150 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
151 return (nameparts[0].rstrip(), nameparts[1:], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
152 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
153 def _lookupvar(self, key, default=_MARKER): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
154 """Lookup a variable. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
155 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
156 If no default is given an unexisting `key` raises a `KeyError` |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
157 else `default` is returned. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
158 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
159 parts = key.split('.') |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
160 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
161 v = self[parts[0]] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
162 for p in parts[1:]: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
163 v = v[p] |
|
104
d9f9a06e0c49
Make a better error message for "TypeError" exceptions when looking up variables.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
82
diff
changeset
|
164 except TypeError: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
165 raise KeyError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
166 "Configuration variable %r not found" |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
167 "(missing intermediate keys?)" % key) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
168 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
169 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
170 raise KeyError("Configuration variable %r not found" % key) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
171 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
172 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
173 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
174 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
175 # Speed |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
176 _TEXTTYPE = type(u("")) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
177 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
178 def substitute_variables_in_obj(self, obj): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
179 """Recursively expand variables in the object tree `obj`.""" |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
180 if isinstance(obj, self._TEXTTYPE): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
181 # a string - really replace the value |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
182 return self.expand_variable(obj) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
183 elif isinstance(obj, list): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
184 return [self.substitute_variables_in_obj(i) for i in obj] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
185 elif isinstance(obj, tuple): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
186 tmp = [self.substitute_variables_in_obj(i) for i in obj] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
187 return type(obj)(tmp) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
188 elif isinstance(obj, dict): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
189 newdict = type(obj)() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
190 for k in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
191 newdict[k] = self.substitute_variables_in_obj(obj[k]) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
192 return newdict |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
193 elif isinstance(obj, set): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
194 newset = type(obj)() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
195 for i in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
196 newset.add(self.substitute_variables_in_obj(i)) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
197 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
198 return obj |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
199 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
200 # Speed |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
201 _STARTTOK = u(b"{{") |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
202 _ENDTOK = u(b"}}") |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
203 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
204 def expand_variable(self, s): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
205 """Expand variables in a single string""" |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
206 start = s.find(self._STARTTOK, 0) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
207 while start != -1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
208 end = s.find(self._ENDTOK, start) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
209 if end < 0: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
210 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
211 varname, filters = self._split_filters(s[start+2:end]) |
|
39
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
212 try: |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
213 varvalue = self._apply_filters(filters, self.getvar_s(varname)) |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
214 except KeyError: |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
215 warnings.warn("Cannot expand variable %r in string " |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
216 "%r" % (varname, s, ), |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
217 UserWarning, |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
218 stacklevel=1) |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
219 raise |
|
25
baf862cb4860
Handle "None" variable values when substituting
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
24
diff
changeset
|
220 if varvalue is None: |
|
baf862cb4860
Handle "None" variable values when substituting
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
24
diff
changeset
|
221 varvalue = u("") |
|
251
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
222 # |
|
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
223 # Dont apply and type conversions to str if the whole `s` is |
|
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
224 # just one expansion |
|
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
225 # |
|
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
226 if (start == 0) and (end + 2 == len(s)): |
|
2a8dcab2de8c
Do not implicitely convert a configuration value to text if the value is the result of just a variable expansion.
Franz Glasner <fzglas.hg@dom66.de>
parents:
250
diff
changeset
|
227 return varvalue |
|
249
1e38ccfba3de
Use explicit type conversion instead of an implicit one.
Franz Glasner <fzglas.hg@dom66.de>
parents:
248
diff
changeset
|
228 replaced = s[:start] + u(str(varvalue)) |
|
248
13283057a21e
Do not use ".format()" but string concatenation or (when conversion to Unicode is needed) the faster %s method
Franz Glasner <fzglas.hg@dom66.de>
parents:
237
diff
changeset
|
229 s = replaced + s[end+2:] |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
230 # don't re-evaluate because `self.getvar_s()` expands already |
|
23
c77cb6bc8eeb
FIX: Handle non-str types in variable substitutions properly
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
18
diff
changeset
|
231 start = s.find(self._STARTTOK, len(replaced)) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
232 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
233 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
234 def _apply_filters(self, filters, value): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
235 for name in filters: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
236 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
237 filterfn = lookup_filter(name) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
238 except KeyError: |
|
40
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
239 # |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
240 # Convert to NameError because we find a missing filters |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
241 # a very serious error. |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
242 # |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
243 raise NameError("Filter %r not found" % name) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
244 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
245 value = filterfn(self, value) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
246 return value |
