Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/config.py @ 311:035b42a224ed
Docu
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 28 Apr 2021 09:23:14 +0200 |
| parents | 801cd16223e4 |
| children | 043a6412be3c |
| 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 # :- |
| 302 | 3 # :Copyright: (c) 2015-2021, Franz Glasner. All rights reserved. |
|
296
eed16a1ec8f3
Use SPDX license identifiers (either full or short) all over the package
Franz Glasner <fzglas.hg@dom66.de>
parents:
251
diff
changeset
|
4 # :License: BSD-3-Clause. 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 |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
25 try: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
26 from urllib.parse import urlsplit |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
27 except ImportError: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
28 from urlparse import urlsplit |
|
16
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 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
|
31 from .compat import u |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
32 from .constants import REF_NAMESPACE |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
33 |
|
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 _MARKER = object() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
36 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
37 |
|
17
94b5e94fae44
Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
16
diff
changeset
|
38 class _AttributeDict(ConfigurationBase): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
39 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
40 def __getattr__(self, name): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
41 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
42 v = self[name] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
43 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
44 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
|
45 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
46 # 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
|
47 if isinstance(v, dict): |
|
29
17af7a78710c
FIX: Renaming a class was not really complete
Franz Glasner <hg@dom66.de>
parents:
25
diff
changeset
|
48 return _AttributeDict(v) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
49 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
50 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
51 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
52 |
|
17
94b5e94fae44
Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
16
diff
changeset
|
53 class Configuration(_AttributeDict): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
54 |
| 18 | 55 """The configuration dictionary with attribute support or |
| 56 variable substitution. | |
| 57 | |
| 58 .. note:: When retriving by attribute names variables will *not* | |
| 59 substituted. | |
| 60 | |
| 61 """ | |
| 62 | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
63 # Speed |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
64 _TEXTTYPE = type(u("")) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
65 _STARTTOK = u(b"{{") |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
66 _ENDTOK = u(b"}}") |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
67 _NS_SEPARATOR = u(b':') |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
68 _FILTER_SEPARATOR = u(b'|') |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
69 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
70 _ENDTOK_REF = _ENDTOK |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
71 _DOT = u(b'.') |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
72 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
73 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
|
74 """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
|
75 variables from other namespaces. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
76 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
77 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
|
78 |
|
16
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 varns, varname = self._split_ns(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
81 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
82 if not varns: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
83 lookupfn = self._lookupvar |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
84 else: |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
85 if varns == REF_NAMESPACE: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
86 lookupfn = self._lookupref |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
87 else: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
88 lookupfn = lookup_varns(varns) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
89 varvalue = lookupfn(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
90 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
91 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
92 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
|
93 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
94 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
95 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
96 return varvalue |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
97 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
98 def getvar_s(self, varname, default=_MARKER): |
|
24
fa65adab0b71
FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
23
diff
changeset
|
99 """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
|
100 |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
101 `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
|
102 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
|
103 and filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
104 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
105 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
|
106 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
107 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
108 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
109 obj = self.getvar(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
110 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
|
111 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
112 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
113 raise |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
114 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
115 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
116 |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
117 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
|
118 """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
|
119 number. |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
120 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
121 """ |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
122 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
|
123 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
|
124 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
|
125 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
126 return s |
|
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 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
|
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 boolean |
|
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 sl = s.strip().lower() |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
140 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
141 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
142 # Conversion of booleans |
|
304
d8361dd70d2d
FIX: Map unicode strings to boolean values because this is the canonical texttype in confixmmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
302
diff
changeset
|
143 _BOOL_CVT = { |
|
d8361dd70d2d
FIX: Map unicode strings to boolean values because this is the canonical texttype in confixmmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
302
diff
changeset
|
144 u('1'): True, u('yes'): True, u('true'): True, u('on'): True, |
|
d8361dd70d2d
FIX: Map unicode strings to boolean values because this is the canonical texttype in confixmmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
302
diff
changeset
|
145 u('0'): False, u('no'): False, u('false'): False, u('off'): False |
|
d8361dd70d2d
FIX: Map unicode strings to boolean values because this is the canonical texttype in confixmmix
Franz Glasner <fzglas.hg@dom66.de>
parents:
302
diff
changeset
|
146 } |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
147 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
148 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
|
149 """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
|
150 float |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
151 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
152 """ |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
157 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
158 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
159 def _split_ns(self, s): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
160 nameparts = s.split(self._NS_SEPARATOR, 1) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
161 if len(nameparts) == 1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
162 return (None, s, ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
163 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
164 return (nameparts[0], nameparts[1], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
165 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
166 def _split_filters(self, s): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
167 nameparts = s.split(self._FILTER_SEPARATOR) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
168 if len(nameparts) == 1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
169 return (s, [], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
170 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
171 return (nameparts[0].rstrip(), nameparts[1:], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
172 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
173 def _lookupvar(self, key, default=_MARKER): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
174 """Lookup a variable. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
175 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
176 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
|
177 else `default` is returned. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
178 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
179 parts = key.split('.') |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
180 try: |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
181 v = self.expand_if_reference(self[parts[0]]) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
182 for p in parts[1:]: |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
183 v = self.expand_if_reference(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
|
184 except TypeError: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
185 raise KeyError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
186 "Configuration variable %r not found" |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
187 "(missing intermediate keys?)" % key) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
188 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
189 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
190 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
|
191 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
192 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
193 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
194 |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
195 def _lookupref(self, key, default=_MARKER): |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
196 """ |
| 311 | 197 |
| 198 `key` must be a configuration reference URI without any | |
| 199 (namespace) prefixes and suffixes | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
200 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
201 """ |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
202 return self.expand_ref_uri(key, default=default) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
203 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
204 def expand_if_reference(self, v, default=_MARKER): |
| 311 | 205 """Check whether `v` is a configuration reference and -- if true -- |
| 206 then expand it. | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
207 |
| 310 | 208 `v` must match the pattern ``{{ref:<REFERENCE>}}`` |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
209 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
210 All non-matching texttypes and all non-texttypes are returned |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
211 unchanged. |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
212 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
213 """ |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
214 if not isinstance(v, self._TEXTTYPE): |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
215 return v |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
216 if not v.startswith(self._STARTTOK_REF) \ |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
217 or not v.endswith(self._ENDTOK_REF): |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
218 return v |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
219 return self.expand_ref_uri( |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
220 v[len(self._STARTTOK_REF):-len(self._ENDTOK_REF)], |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
221 default=default) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
222 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
223 def expand_ref_uri(self, uri, default=_MARKER): |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
224 pu = urlsplit(uri) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
225 if pu.scheme or pu.netloc or pu.path or pu.query: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
226 raise ValueError("only fragment-only URIs are supported") |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
227 if not pu.fragment: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
228 return self |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
229 if pu.fragment.startswith(self._DOT): |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
230 raise ValueError("relative refs not supported") |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
231 return self.getvar(pu.fragment, default=default) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
232 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
233 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
|
234 """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
|
235 if isinstance(obj, self._TEXTTYPE): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
236 # a string - really replace the value |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
237 return self.expand_variable(obj) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
238 elif isinstance(obj, list): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
239 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
|
240 elif isinstance(obj, tuple): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
241 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
|
242 return type(obj)(tmp) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
243 elif isinstance(obj, dict): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
244 newdict = type(obj)() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
245 for k in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
246 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
|
247 return newdict |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
248 elif isinstance(obj, set): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
249 newset = type(obj)() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
250 for i in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
251 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
|
252 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
253 return obj |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
254 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
255 def expand_variable(self, s): |
| 302 | 256 """Expand variables in the single string `s`""" |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
257 start = s.find(self._STARTTOK, 0) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
258 while start != -1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
259 end = s.find(self._ENDTOK, start) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
260 if end < 0: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
261 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
262 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
|
263 try: |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
264 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
|
265 except KeyError: |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
266 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
|
267 "%r" % (varname, s, ), |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
268 UserWarning, |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
269 stacklevel=1) |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
270 raise |
|
25
baf862cb4860
Handle "None" variable values when substituting
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
24
diff
changeset
|
271 if varvalue is None: |
|
baf862cb4860
Handle "None" variable values when substituting
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
24
diff
changeset
|
272 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
|
273 # |
|
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
|
274 # 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
|
275 # 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
|
276 # |
|
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
|
277 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
|
278 return varvalue |
|
249
1e38ccfba3de
Use explicit type conversion instead of an implicit one.
Franz Glasner <fzglas.hg@dom66.de>
parents:
248
diff
changeset
|
279 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
|
280 s = replaced + s[end+2:] |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
281 # 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
|
282 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
|
283 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
284 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
285 def _apply_filters(self, filters, value): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
286 for name in filters: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
287 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
288 filterfn = lookup_filter(name) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
289 except KeyError: |
|
40
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
290 # |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
291 # 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
|
292 # a very serious error. |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
293 # |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
294 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
|
295 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
296 value = filterfn(self, value) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
297 return value |
