Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/config.py @ 385:4beeb291926d
First documentation of getfirstvarl()
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 10 Nov 2021 02:11:12 +0100 |
| parents | 8c3aaa894089 |
| children | 13427f37abab |
| 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 |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
31 from .compat import u, uchr |
|
357
dd454e1efea4
Use constants for the names of the "None" and "Empty" filters
Franz Glasner <fzglas.hg@dom66.de>
parents:
352
diff
changeset
|
32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER |
|
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 | |
| 322 | 58 .. note:: When retrieving by attribute names variables will *not* |
| 18 | 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"}}") |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
67 _HIER_SEPARATOR = u(b'.') |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
68 _NS_SEPARATOR = u(b':') |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
69 _FILTER_SEPARATOR = u(b'|') |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
70 _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
|
71 _ENDTOK_REF = _ENDTOK |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
72 _DOT = u(b'.') |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
73 _QUOTE = u(b'%') |
|
325
ab33d51f3412
By default quote the "#" character also because it is typically a comment in all the configuration file formats.
Franz Glasner <fzglas.hg@dom66.de>
parents:
322
diff
changeset
|
74 _COMMENT = u(b'#') |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
75 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
76 def getvarl(self, *path, **kwds): |
|
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
77 """Get a variable where the hierarchy is given in `path` as sequence |
|
380
bb4a90fb58e0
Docu: "namespace" in getvarl() is a keyword argument
Franz Glasner <fzglas.hg@dom66.de>
parents:
374
diff
changeset
|
78 and the namespace is given in the `namespace` keyword argument. |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
79 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
80 No variable interpolation is done and no filters are applied. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
81 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
82 Quoting of `path` and `namespace` is *not* needed and wrong. |
| 333 | 83 |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
84 """ |
|
321
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
85 default = kwds.pop("default", _MARKER) |
|
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
86 namespace = kwds.pop("namespace", None) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
87 try: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
88 if not namespace: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
89 lookupfn = self._lookupvar |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
90 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
91 if namespace == REF_NAMESPACE: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
92 lookupfn = self._lookupref |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
93 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
94 lookupfn = lookup_varns(namespace) |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
95 varvalue = lookupfn(*path) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
96 except KeyError: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
97 if default is _MARKER: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
98 raise KeyError("Variable %r not found" % (path,)) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
99 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
100 return default |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
101 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
102 return varvalue |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
103 |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
104 def getfirstvarl(self, *paths, **kwds): |
|
385
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
105 """A variant of :meth:`~.getvarl` that returns the first found |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
106 variable in the `paths` list. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
107 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
108 Every item in `paths` is either a `tuple` or `list` or a `dict`. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
109 If the path item is a `dict` then it must have two keys "namespace" |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
110 and "path". |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
111 If the path item is a `list` or `tuple` then the namespace is assumed |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
112 to be `None`. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
113 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
114 No variable interpolation is done and no filters are applied. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
115 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
116 Quoting of anything in `paths` is *not* needed and wrong. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
117 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
118 """ |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
119 default = kwds.pop("default", _MARKER) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
120 for path in paths: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
121 if isinstance(path, (list, tuple)): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
122 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
123 varvalue = self.getvarl(*path) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
124 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
125 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
126 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
127 return varvalue |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
128 elif isinstance(path, dict): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
129 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
130 namespace = path["namespace"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
131 p = path["path"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
132 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
133 raise TypeError("a paths dict item must have a `path'" |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
134 " and a `namespace' key") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
135 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
136 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
137 varvalue = self.getvarl(*p, namespace=namespace) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
138 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
139 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
140 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
141 return varvalue |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
142 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
143 raise TypeError("a paths item must be a dict, list or tuple") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
144 if default is _MARKER: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
145 raise KeyError( |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
146 "none of the given variables found: %r" % (paths,)) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
147 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
148 return default |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
149 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
150 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
|
151 """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
|
152 variables from other namespaces. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
153 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
154 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
|
155 |
| 333 | 156 Special characters (e.g. ``:`` and ``.``) must be quoted when using |
| 157 the default namespace. | |
| 158 | |
| 159 See also :meth:`~.quote`. | |
| 160 | |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
161 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
162 varns, varname = self._split_ns(varname) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
163 if not varns: |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
164 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)] |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
165 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
166 varnameparts = (varname,) |
|
364
1941f0188e81
FIX: Handle a "default" keyword parameter in ".getvar()" properly.
Franz Glasner <fzglas.hg@dom66.de>
parents:
357
diff
changeset
|
167 return self.getvarl(*varnameparts, namespace=varns, default=default) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
168 |
|
368
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
169 def getfirstvar(self, *varnames, **kwds): |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
170 """A variant of :meth:`~.getvar` that returns the first found variable |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
171 in the list of given variables in `varnames`. |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
172 |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
173 """ |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
174 default = kwds.pop("default", _MARKER) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
175 for varname in varnames: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
176 try: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
177 varvalue = self.getvar(varname) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
178 except KeyError: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
179 pass |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
180 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
181 return varvalue |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
182 if default is _MARKER: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
183 raise KeyError( |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
184 "none of the given variables found: %r" % (varnames,)) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
185 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
186 return default |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
187 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
188 def getvarl_s(self, *path, **kwds): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
189 """Get a variable - including variables from other namespaces. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
190 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
191 `path` and `namespace` are interpreted as in |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
192 :meth:`.getvarl`. But variables will be interpolated |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
193 recursively within the variable values and filters are |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
194 applied. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
195 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
196 For more details see chapter :ref:`variable-interpolation`. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
197 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
198 """ |
|
321
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
199 default = kwds.pop("default", _MARKER) |
|
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
200 namespace = kwds.pop("namespace", None) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
201 try: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
202 obj = self.getvarl(*path, namespace=namespace) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
203 return self.substitute_variables_in_obj(obj) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
204 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
205 if default is _MARKER: |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
206 raise |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
207 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
208 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
209 |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
210 def getfirstvarl_s(self, *paths, **kwds): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
211 default = kwds.pop("default", _MARKER) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
212 for path in paths: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
213 if isinstance(path, (list, tuple)): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
214 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
215 obj = self.getvarl(*path) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
216 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
217 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
218 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
219 return self.substitute_variables_in_obj(obj) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
220 |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
221 elif isinstance(path, dict): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
222 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
223 namespace = path["namespace"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
224 p = path["path"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
225 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
226 raise TypeError("a paths dict item must have a `path'" |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
227 " and a `namespace' key") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
228 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
229 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
230 obj = self.getvarl(*p, namespace=namespace) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
231 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
232 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
233 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
234 return self.substitute_variables_in_obj(obj) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
235 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
236 raise TypeError("a paths item must be a dict, list or tuple") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
237 if default is _MARKER: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
238 raise KeyError( |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
239 "none of the given variables found: %r" % (paths,)) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
240 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
241 return default |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
242 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
243 def getvar_s(self, varname, default=_MARKER): |
|
24
fa65adab0b71
FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
23
diff
changeset
|
244 """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
|
245 |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
246 `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
|
247 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
|
248 and filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
249 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
250 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
|
251 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
252 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
253 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
254 obj = self.getvar(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
255 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
|
256 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
257 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
258 raise |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
259 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
260 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
261 |
|
368
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
262 def getfirstvar_s(self, *varnames, **kwds): |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
263 """A variant of :meth:`~.getvar_s` that returns the first found |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
264 variable in the list of given variables in `varnames`. |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
265 |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
266 """ |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
267 default = kwds.pop("default", _MARKER) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
268 for varname in varnames: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
269 try: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
270 obj = self.getvar(varname) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
271 except KeyError: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
272 pass |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
273 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
274 return self.substitute_variables_in_obj(obj) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
275 if default is _MARKER: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
276 raise KeyError( |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
277 "none of the given variables found: %r" % (varnames,)) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
278 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
279 return default |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
280 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
281 def getintvarl_s(self, *path, **kwds): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
282 """Get a (possibly substituted) variable and coerce text to a |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
283 number. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
284 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
285 """ |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
286 s = self.getvarl_s(*path, **kwds) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
287 if isinstance(s, self._TEXTTYPE): |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
288 return int(s, 0) |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
289 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
290 return s |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
291 |
|
384
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
292 def getfirstintvarl_s(self, *paths, **kwds): |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
293 """Get a (possibly substituted) variable and coerce text to a |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
294 number. |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
295 |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
296 """ |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
297 s = self.getfirstvarl_s(*paths, **kwds) |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
298 if isinstance(s, self._TEXTTYPE): |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
299 return int(s, 0) |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
300 else: |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
301 return s |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
302 |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
303 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
|
304 """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
|
305 number. |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
306 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
307 """ |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
308 s = self.getvar_s(varname, default=default) |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
309 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
|
310 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
|
311 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
312 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
313 |
|
373
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
314 def getfirstintvar_s(self, *varnames, **kwds): |
| 374 | 315 """A variant of :meth:`~.getintvar_s` that returns the first found |
| 316 variable in the list of given variables in `varnames`. | |
|
373
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
317 |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
318 """ |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
319 s = self.getfirstvar_s(*varnames, **kwds) |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
320 if isinstance(s, self._TEXTTYPE): |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
321 return int(s, 0) |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
322 else: |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
323 return s |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
324 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
325 def getboolvarl_s(self, *path, **kwds): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
326 """Get a (possibly substituted) variable and convert text to a |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
327 boolean |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
328 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
329 """ |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
330 s = self.getvarl_s(*path, **kwds) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
331 if isinstance(s, self._TEXTTYPE): |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
332 sl = s.strip().lower() |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
333 if sl not in self._BOOL_CVT: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
334 raise ValueError("Not a boolean: %r" % s) |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
335 return self._BOOL_CVT[sl] |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
336 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
337 return s |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
338 |
|
384
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
339 def getfirstboolvarl_s(self, *paths, **kwds): |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
340 """Get a (possibly substituted) variable and convert text to a |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
341 boolean |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
342 |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
343 """ |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
344 s = self.getfirstvarl_s(*paths, **kwds) |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
345 if isinstance(s, self._TEXTTYPE): |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
346 sl = s.strip().lower() |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
347 if sl not in self._BOOL_CVT: |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
348 raise ValueError("Not a boolean: %r" % s) |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
349 return self._BOOL_CVT[sl] |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
350 else: |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
351 return s |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
352 |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
353 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
|
354 """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
|
355 boolean |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
356 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
357 """ |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
358 s = self.getvar_s(varname, default=default) |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
359 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
|
360 sl = s.strip().lower() |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
361 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
|
362 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
|
363 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
|
364 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
365 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
366 |
|
373
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
367 def getfirstboolvar_s(self, *varnames, **kwds): |
| 374 | 368 """A variant of :meth:`~.getboolvar_s` that returns the first found |
| 369 variable in the list of given variables in `varnames`. | |
|
373
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
370 |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
371 """ |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
372 s = self.getfirstvar_s(*varnames, **kwds) |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
373 if isinstance(s, self._TEXTTYPE): |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
374 sl = s.strip().lower() |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
375 if sl not in self._BOOL_CVT: |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
376 raise ValueError("Not a boolean: %r" % s) |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
377 return self._BOOL_CVT[sl] |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
378 else: |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
379 return s |
|
0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
Franz Glasner <fzglas.hg@dom66.de>
parents:
368
diff
changeset
|
380 |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
381 # 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
|
382 _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
|
383 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
|
384 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
|
385 } |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
386 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
387 def getfloatvarl_s(self, *path, **kwds): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
388 """Get a (possibly substituted) variable and convert text to a |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
389 float |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
390 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
391 """ |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
392 s = self.getvarl_s(*path, **kwds) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
393 if isinstance(s, self._TEXTTYPE): |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
394 return float(s) |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
395 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
396 return s |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
397 |
|
384
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
398 def getfirstfloatvarl_s(self, *path, **kwds): |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
399 """Get a (possibly substituted) variable and convert text to a |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
400 float |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
401 |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
402 """ |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
403 s = self.getfirstvarl_s(*path, **kwds) |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
404 if isinstance(s, self._TEXTTYPE): |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
405 return float(s) |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
406 else: |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
407 return s |
|
8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
383
diff
changeset
|
408 |
|
36
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
409 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
|
410 """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
|
411 float |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
412 |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
413 """ |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 else: |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
418 return s |
|
b04a350f894b
Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents:
29
diff
changeset
|
419 |
|
383
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
420 def getfirstfloatvar_s(self, varname, default=_MARKER): |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
421 """Get a (possibly substituted) variable and convert text to a |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
422 float |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
423 |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
424 """ |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
425 s = self.getfirstvar_s(varname, default) |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
426 if isinstance(s, self._TEXTTYPE): |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
427 return float(s) |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
428 else: |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
429 return s |
|
5c72da46b8ae
Implemented Configuration.getfirstfloatvar_s().
Franz Glasner <fzglas.hg@dom66.de>
parents:
382
diff
changeset
|
430 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
431 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
|
432 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
|
433 if len(nameparts) == 1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
434 return (None, s, ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
435 else: |
|
329
d81d2cdf4925
FIX: Handle the unquoting of namespaces within Configuration.getvar() properly.
Franz Glasner <fzglas.hg@dom66.de>
parents:
326
diff
changeset
|
436 return (self.unquote(nameparts[0]), nameparts[1], ) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
437 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
438 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
|
439 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
|
440 if len(nameparts) == 1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
441 return (s, [], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
442 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
443 return (nameparts[0].rstrip(), nameparts[1:], ) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
444 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
445 def _lookupvar(self, *path, **kwds): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
446 """Lookup a variable within a hierarchy. |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
447 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
448 If no default is given an unexisting `path` raises a `KeyError` |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
449 else `default` is returned. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
450 """ |
|
321
7a0f3c256cf4
FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
320
diff
changeset
|
451 default = kwds.pop("default", _MARKER) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
452 try: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
453 v = self.expand_if_reference(self[path[0]]) |
|
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
454 for p in path[1:]: |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
455 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
|
456 except TypeError: |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
457 raise KeyError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
458 "Configuration variable %r not found" |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
459 "(missing intermediate keys?)" % (path,)) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
460 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
461 if default is _MARKER: |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
462 raise KeyError( |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
463 "Configuration variable %r not found" % (path,)) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
464 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
465 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
466 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
467 |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
468 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
|
469 """ |
| 311 | 470 |
| 471 `key` must be a configuration reference URI without any | |
| 472 (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
|
473 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
474 """ |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
475 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
|
476 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
477 def expand_if_reference(self, v, default=_MARKER): |
| 311 | 478 """Check whether `v` is a configuration reference and -- if true -- |
| 479 then expand it. | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
480 |
| 310 | 481 `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
|
482 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
483 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
|
484 unchanged. |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
485 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
486 """ |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
487 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
|
488 return v |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
489 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
|
490 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
|
491 return v |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
492 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
|
493 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
|
494 default=default) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
495 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
496 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
|
497 pu = urlsplit(uri) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
498 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
|
499 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
|
500 if not pu.fragment: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
501 return self |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
502 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
|
503 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
|
504 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
|
505 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
506 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
|
507 """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
|
508 if isinstance(obj, self._TEXTTYPE): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
509 # a string - really replace the value |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
510 return self.expand_variable(obj) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
511 elif isinstance(obj, list): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
512 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
|
513 elif isinstance(obj, tuple): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
514 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
|
515 return type(obj)(tmp) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
516 elif isinstance(obj, dict): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
517 newdict = type(obj)() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
518 for k in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
519 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
|
520 return newdict |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
521 elif isinstance(obj, set): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
522 newset = type(obj)() |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
523 for i in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
524 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
|
525 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
526 return obj |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
527 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
528 def expand_variable(self, s): |
| 302 | 529 """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
|
530 start = s.find(self._STARTTOK, 0) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
531 while start != -1: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
532 end = s.find(self._ENDTOK, start) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
533 if end < 0: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
534 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
535 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
|
536 try: |
|
357
dd454e1efea4
Use constants for the names of the "None" and "Empty" filters
Franz Glasner <fzglas.hg@dom66.de>
parents:
352
diff
changeset
|
537 if NONE_FILTER in filters: |
|
349
83f76a41cf7c
Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
334
diff
changeset
|
538 varvalue = self._apply_filters( |
|
83f76a41cf7c
Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
334
diff
changeset
|
539 filters, self.getvar_s(varname, default=None)) |
|
357
dd454e1efea4
Use constants for the names of the "None" and "Empty" filters
Franz Glasner <fzglas.hg@dom66.de>
parents:
352
diff
changeset
|
540 elif EMPTY_FILTER in filters: |
|
352
2b209bdf6995
Implement the "Empty" filter.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
350
diff
changeset
|
541 varvalue = self._apply_filters( |
|
2b209bdf6995
Implement the "Empty" filter.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
350
diff
changeset
|
542 filters, self.getvar_s(varname, default=u(""))) |
|
349
83f76a41cf7c
Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
334
diff
changeset
|
543 else: |
|
83f76a41cf7c
Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
334
diff
changeset
|
544 varvalue = self._apply_filters( |
|
83f76a41cf7c
Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
334
diff
changeset
|
545 filters, self.getvar_s(varname)) |
|
39
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
546 except KeyError: |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
547 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
|
548 "%r" % (varname, s, ), |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
549 UserWarning, |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
550 stacklevel=1) |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
551 raise |
|
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
|
552 # |
|
350
9d729c479dc2
FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
349
diff
changeset
|
553 # Dont apply and type conversions to the variable value if |
|
9d729c479dc2
FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
349
diff
changeset
|
554 # the whole `s` is just one expansion |
|
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
|
555 # |
|
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
|
556 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
|
557 return varvalue |
|
350
9d729c479dc2
FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
349
diff
changeset
|
558 if varvalue is None: |
|
9d729c479dc2
FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
349
diff
changeset
|
559 varvalue = u("") |
|
249
1e38ccfba3de
Use explicit type conversion instead of an implicit one.
Franz Glasner <fzglas.hg@dom66.de>
parents:
248
diff
changeset
|
560 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
|
561 s = replaced + s[end+2:] |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
562 # 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
|
563 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
|
564 return s |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
565 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
566 def _apply_filters(self, filters, value): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
567 for name in filters: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
568 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
569 filterfn = lookup_filter(name) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
570 except KeyError: |
|
40
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
571 # |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
572 # 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
|
573 # a very serious error. |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
574 # |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
575 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
|
576 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
577 value = filterfn(self, value) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
578 return value |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
579 |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
580 @classmethod |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
581 def quote(klass, s): |
| 333 | 582 """Replace important special characters in string `s` by replacing |
| 583 them with ``%xNN`` where `NN` are the two hexadecimal digits of the | |
| 584 characters unicode codepoint value. | |
| 585 | |
| 586 Handled are the important special chars: ``%``, ``.``, ``:``, | |
|
334
a04cd5dbcd2c
By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
333
diff
changeset
|
587 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``. |
| 333 | 588 |
| 589 See also the :ref:`quoting` section. | |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
590 |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
591 """ |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
592 qc = klass._QUOTE |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
593 s = s.replace(qc, qc + "x25") |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
594 s = s.replace(klass._DOT, qc + "x2e") |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
595 s = s.replace(klass._NS_SEPARATOR, qc + "x3a") |
|
325
ab33d51f3412
By default quote the "#" character also because it is typically a comment in all the configuration file formats.
Franz Glasner <fzglas.hg@dom66.de>
parents:
322
diff
changeset
|
596 s = s.replace(klass._COMMENT, qc + "x23") |
|
326
b7abfbfe806d
By default quote '"' and "'" also
Franz Glasner <fzglas.hg@dom66.de>
parents:
325
diff
changeset
|
597 s = s.replace(klass._FILTER_SEPARATOR, qc + "x7c") |
|
b7abfbfe806d
By default quote '"' and "'" also
Franz Glasner <fzglas.hg@dom66.de>
parents:
325
diff
changeset
|
598 s = s.replace('"', qc + "x22") |
|
334
a04cd5dbcd2c
By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
333
diff
changeset
|
599 s = s.replace("'", qc + "x27") |
|
a04cd5dbcd2c
By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
333
diff
changeset
|
600 s = s.replace('{', qc + "x7b") |
|
a04cd5dbcd2c
By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
333
diff
changeset
|
601 s = s.replace('}', qc + "x7d") |
|
a04cd5dbcd2c
By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
333
diff
changeset
|
602 s = s.replace('[', qc + "x5b") |
|
a04cd5dbcd2c
By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
333
diff
changeset
|
603 return s.replace(']', qc + "x5d") |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
604 |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
605 @classmethod |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
606 def unquote(klass, s): |
| 333 | 607 """Unquote the content of `s`: handle all patterns ``%xNN``, |
| 608 ``%uNNNN`` or ``%UNNNNNNNN``. | |
| 609 | |
| 610 This is the inverse of :meth:`~.quote`. | |
|
320
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
611 |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
612 """ |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
613 if klass._QUOTE not in s: |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
614 return s |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
615 res = [] |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
616 parts = s.split(klass._QUOTE) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
617 res.append(parts[0]) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
618 for p in parts[1:]: |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
619 if p.startswith(u(b'x')): |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
620 if len(p) < 3: |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
621 raise ValueError("quote syntax: length too small") |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
622 res.append(uchr(int(p[1:3], 16))) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
623 res.append(p[3:]) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
624 elif p.startswith(u(b'u')): |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
625 if len(p) < 5: |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
626 raise ValueError("quote syntax: length too small") |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
627 res.append(uchr(int(p[1:5], 16))) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
628 res.append(p[5:]) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
629 elif p.startswith(u(b'U')): |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
630 if len(p) < 9: |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
631 raise ValueError("quote syntax: length too small") |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
632 res.append(uchr(int(p[1:9], 16))) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
633 res.append(p[9:]) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
634 else: |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
635 raise ValueError("unknown quote syntax string: {}".format(s)) |
|
98490375d90c
Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents:
314
diff
changeset
|
636 return ''.join(res) |
