Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/config.py @ 500:34cd4f111134
"quote()" and "unquote()" are now static methods
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 18 Dec 2021 01:03:49 +0100 |
| parents | f46932e8a84c |
| children | 4f90e1eb7af8 |
| 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 |
|
482
8b8ffb74d452
Remove unneeded __future__ import because we are on Python 2.6+
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
481
diff
changeset
|
11 from __future__ import division, absolute_import, print_function |
|
16
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 |
|
487
d7f6f2afcee2
Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
485
diff
changeset
|
31 from .compat import u, uchr, n, str_and_u, PY2 |
|
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() |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
36 _MISSING = object() |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
37 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
38 |
|
17
94b5e94fae44
Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
16
diff
changeset
|
39 class _AttributeDict(ConfigurationBase): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
40 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
41 def __getattr__(self, name): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
42 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
43 v = self[name] |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
44 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
45 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
|
46 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
47 # 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
|
48 if isinstance(v, dict): |
|
29
17af7a78710c
FIX: Renaming a class was not really complete
Franz Glasner <hg@dom66.de>
parents:
25
diff
changeset
|
49 return _AttributeDict(v) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
50 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
51 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
52 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
53 |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
54 class CoercingMethodsMixin(object): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
55 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
56 """Mixin to provide some common implementations for retrieval |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
57 methods that convert return values to a fixed type (int, bool, |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
58 float). |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
59 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
60 Both :class:`~.Configuration` and :class:`~._JailedConfiguration` use |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
61 this mixin. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
62 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
63 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
64 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
65 def getintvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
66 """Get a (possibly substituted) variable and coerce text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
67 number. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
68 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
69 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
70 s = self.getvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
71 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
72 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
73 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
74 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
75 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
76 def getfirstintvarl_s(self, *paths, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
77 """Get a (possibly substituted) variable and coerce text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
78 number. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
79 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
80 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
81 s = self.getfirstvarl_s(*paths, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
82 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
83 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
84 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
85 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
86 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
87 def getintvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
88 """Get a (possibly substituted) variable and coerce text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
89 number. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
90 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
91 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
92 s = self.getvar_s(varname, default=default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
93 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
94 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
95 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
96 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
97 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
98 def getfirstintvar_s(self, *varnames, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
99 """A variant of :meth:`~.getintvar_s` that returns the first found |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
100 variable in the list of given variables in `varnames`. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
101 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
102 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
103 s = self.getfirstvar_s(*varnames, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
104 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
105 return int(s, 0) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
106 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
107 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
108 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
109 def getboolvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
110 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
111 boolean |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
112 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
113 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
114 s = self.getvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
115 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
116 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
117 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
118 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
119 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
120 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
121 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
122 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
123 def getfirstboolvarl_s(self, *paths, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
124 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
125 boolean |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
126 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
127 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
128 s = self.getfirstvarl_s(*paths, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
129 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
130 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
131 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
132 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
133 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
134 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
135 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
136 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
137 def getboolvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
138 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
139 boolean |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
140 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
141 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
142 s = self.getvar_s(varname, default=default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
143 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
144 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
145 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
146 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
147 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
148 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
149 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
150 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
151 def getfirstboolvar_s(self, *varnames, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
152 """A variant of :meth:`~.getboolvar_s` that returns the first found |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
153 variable in the list of given variables in `varnames`. |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
154 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
155 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
156 s = self.getfirstvar_s(*varnames, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
157 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
158 sl = s.strip().lower() |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
159 if sl not in self._BOOL_CVT: |
|
450
12f25ac6a13b
Make exception formatting more robuts: pack all %-style formatting args explicitely into tuples
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
449
diff
changeset
|
160 raise ValueError("Not a boolean: %r" % (s, )) |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
161 return self._BOOL_CVT[sl] |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
162 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
163 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
164 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
165 # Conversion of booleans |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
166 _BOOL_CVT = { |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
167 u('1'): True, u('yes'): True, u('true'): True, u('on'): True, |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
168 u('0'): False, u('no'): False, u('false'): False, u('off'): False |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
169 } |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
170 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
171 def getfloatvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
172 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
173 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
174 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
175 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
176 s = self.getvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
177 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
178 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
179 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
180 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
181 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
182 def getfirstfloatvarl_s(self, *path, **kwds): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
183 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
184 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
185 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
186 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
187 s = self.getfirstvarl_s(*path, **kwds) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
188 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
189 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
190 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
191 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
192 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
193 def getfloatvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
194 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
195 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
196 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
197 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
198 s = self.getvar_s(varname, default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
199 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
200 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
201 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
202 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
203 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
204 def getfirstfloatvar_s(self, varname, default=_MARKER): |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
205 """Get a (possibly substituted) variable and convert text to a |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
206 float |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
207 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
208 """ |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
209 s = self.getfirstvar_s(varname, default) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
210 if isinstance(s, _TEXTTYPE): |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
211 return float(s) |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
212 else: |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
213 return s |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
214 |
|
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
215 |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
216 # Speed |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
217 _EMPTY_STR = u("") |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
218 _TEXTTYPE = type(_EMPTY_STR) |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
219 _STARTTOK = u(b"{{") |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
220 _ENDTOK = u(b"}}") |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
221 _HIER_SEPARATOR = u(b'.') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
222 _NS_SEPARATOR = u(b':') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
223 _FILTER_SEPARATOR = u(b'|') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
224 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
225 _ENDTOK_REF = _ENDTOK |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
226 _DOT = u(b'.') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
227 _QUOTE = u(b'%') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
228 _QUOTE_x = u(b'x') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
229 _QUOTE_u = u(b'u') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
230 _QUOTE_U = u(b'U') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
231 _COMMENT = u(b'#') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
232 _QUOTE_MAP = { |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
233 0x25: u(b'%x25'), # _QUOTE |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
234 0x2e: u(b'%x2e'), # _DOT |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
235 0x3a: u(b'%x3a'), # _NS_SEPARATOR |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
236 0x23: u(b'%x23'), # _COMMENT / anchor |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
237 0x7c: u(b'%x7c'), # _FILTER_SEPARATOR |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
238 0x22: u(b'%x22'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
239 0x27: u(b'%x27'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
240 0x7b: u(b'%x7b'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
241 0x7d: u(b'%x7d'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
242 0x5b: u(b'%x5b'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
243 0x5d: u(b'%x5d'), |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
244 } |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
245 _QUOTE_SAFE = u(b'abcdefghijklmnopqrstuvwxyz' |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
246 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
247 b'0123456789' |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
248 b'-_@!$&/\\()=?*+~;,<>^') |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
249 """Mostly used configuration key characters that do not need any quoting |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
250 |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
251 """ |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
252 |
|
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
253 |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
254 class Configuration(CoercingMethodsMixin, _AttributeDict): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
255 |
| 18 | 256 """The configuration dictionary with attribute support or |
| 257 variable substitution. | |
| 258 | |
| 322 | 259 .. note:: When retrieving by attribute names variables will *not* |
| 18 | 260 substituted. |
| 261 | |
| 262 """ | |
| 263 | |
|
404
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
264 is_jail = False |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
265 """Flag to show that this is not a jail for another configuration""" |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
266 |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
267 def __init__(self, *args, **kwds): |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
268 # |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
269 # PY2.7 compat: must be set before calling the superclass' __init__ |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
270 # |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
271 self.__cache = {} |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
272 super(Configuration, self).__init__(*args, **kwds) |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
273 |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
274 def __getitem__(self, key): |
| 469 | 275 """Mapping and list interface that forwards to :meth:`~.getvarl_s` |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
276 |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
277 """ |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
278 if isinstance(key, (tuple, list)): |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
279 return self.getvarl_s(*key) |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
280 else: |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
281 return self.getvarl_s(key) |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
282 |
|
442
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
283 def get(self, key, default=None): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
284 """Mapping interface that forwards to :meth:`~.getvarl_s` |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
285 |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
286 """ |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
287 if isinstance(key, (tuple, list)): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
288 return self.getvarl_s(*key, default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
289 else: |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
290 return self.getvarl_s(key, default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
291 |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
292 def __contains__(self, key): |
| 469 | 293 """Containment test""" |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
294 if isinstance(key, (tuple, list)): |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
295 # No namespace and quoting support here |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
296 try: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
297 self._lookupvar(*key) |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
298 except KeyError: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
299 return False |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
300 else: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
301 return True |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
302 else: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
303 return super(Configuration, self).__contains__(key) |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
304 |
|
443
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
305 def getitem_ns(self, key): |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
306 """Just forward to the *original* :meth:`dict.__getitem__`. |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
307 |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
308 No variable interpolation and key path access. |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
309 |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
310 """ |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
311 return super(Configuration, self).__getitem__(key) |
|
23941c014130
FIX: Merge properly when the configuration's __getitem__ do now interpolate: prohibit duplicate interpolation and interpolation while merging
Franz Glasner <fzglas.hg@dom66.de>
parents:
442
diff
changeset
|
312 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
313 def getvarl(self, *path, **kwds): |
|
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
314 """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
|
315 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
|
316 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
317 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
|
318 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
319 Quoting of `path` and `namespace` is *not* needed and wrong. |
| 333 | 320 |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
321 """ |
|
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
|
322 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
|
323 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
|
324 try: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
325 if not namespace: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
326 lookupfn = self._lookupvar |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
327 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
328 if namespace == REF_NAMESPACE: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
329 lookupfn = self._lookupref |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
330 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
331 lookupfn = lookup_varns(namespace) |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
332 varvalue = lookupfn(*path) |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
333 except KeyError: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
334 if default is _MARKER: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
335 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
|
336 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
337 return default |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
338 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
339 return varvalue |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
340 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
341 def getkeysl(self, *path, **kwds): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
342 """Yield the keys of a variable value. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
343 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
344 :rtype: A generator |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
345 :raise KeyError: |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
346 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
347 .. note:: Dictionary keys are not subject to interpolation. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
348 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
349 """ |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
350 if "default" in kwds: |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
351 raise TypeError("got unexpected keyword argument: default") |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
352 for k in self.getvarl(*path, **kwds).keys(): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
353 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
354 |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
355 def getfirstvarl(self, *paths, **kwds): |
|
385
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
356 """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
|
357 variable in the `paths` list. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
358 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
359 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
|
360 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
|
361 and "path". |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
362 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
|
363 to be `None`. |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
364 |
| 389 | 365 Note that a caller that wants to use variables from a non-default |
| 366 namespace must use a sequence of dicts. | |
| 367 | |
|
385
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
368 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
|
369 |
|
4beeb291926d
First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents:
384
diff
changeset
|
370 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
|
371 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
372 """ |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
373 default = kwds.pop("default", _MARKER) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
374 for path in paths: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
375 if isinstance(path, (list, tuple)): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
376 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
377 varvalue = self.getvarl(*path) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
378 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
379 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
380 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
381 return varvalue |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
382 elif isinstance(path, dict): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
383 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
384 namespace = path["namespace"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
385 p = path["path"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
386 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
387 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
|
388 " and a `namespace' key") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
389 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
390 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
391 varvalue = self.getvarl(*p, namespace=namespace) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
392 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
393 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
394 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
395 return varvalue |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
396 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
397 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
|
398 if default is _MARKER: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
399 raise KeyError( |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
400 "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
|
401 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
402 return default |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
403 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
404 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
|
405 """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
|
406 variables from other namespaces. |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
407 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
408 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
|
409 |
| 333 | 410 Special characters (e.g. ``:`` and ``.``) must be quoted when using |
| 411 the default namespace. | |
| 412 | |
| 413 See also :meth:`~.quote`. | |
| 414 | |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
415 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
416 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
|
417 if not varns: |
|
419
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
418 if varname: |
|
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
419 varnameparts = [ |
|
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
420 self.unquote(vp) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
421 for vp in varname.split(_HIER_SEPARATOR) |
|
419
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
422 ] |
|
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
423 else: |
|
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
424 varnameparts = tuple() |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
425 else: |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
426 varnameparts = (varname,) |
|
364
1941f0188e81
FIX: Handle a "default" keyword parameter in ".getvar()" properly.
Franz Glasner <fzglas.hg@dom66.de>
parents:
357
diff
changeset
|
427 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
|
428 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
429 def getkeys(self, varname): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
430 """Yield all the keys of a variable value. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
431 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
432 :rtype: A generator |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
433 :raise KeyError: |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
434 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
435 .. note:: Dictionary keys are not subject to interpolation. |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
436 |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
437 """ |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
438 for k in self.getvar(varname).keys(): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
439 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
440 |
|
368
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
441 def getfirstvar(self, *varnames, **kwds): |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
442 """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
|
443 in the list of given variables in `varnames`. |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
444 |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
445 """ |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
446 default = kwds.pop("default", _MARKER) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
447 for varname in varnames: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
448 try: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
449 varvalue = self.getvar(varname) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
450 except KeyError: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
451 pass |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
452 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
453 return varvalue |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
454 if default is _MARKER: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
455 raise KeyError( |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
456 "none of the given variables found: %r" % (varnames,)) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
457 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
458 return default |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
459 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
460 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
|
461 """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
|
462 |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
463 `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
|
464 :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
|
465 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
|
466 applied. |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
467 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
468 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
|
469 |
|
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
470 """ |
|
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
|
471 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
|
472 namespace = kwds.pop("namespace", None) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
473 try: |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
474 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
|
475 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
|
476 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
477 if default is _MARKER: |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
478 raise |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
479 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
480 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
481 |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
482 def getfirstvarl_s(self, *paths, **kwds): |
|
388
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
483 """A variant of :meth:`~.getfirstvarl` that does variable |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
484 interpolation. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
485 |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
486 `paths` and `kwds` are interpreted as in :meth:`.getfirstvarl`. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
487 But variables will be interpolated |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
488 recursively within the variable values and filters are |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
489 applied. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
490 |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
491 For more details see chapter :ref:`variable-interpolation`. |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
492 |
|
13427f37abab
Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
385
diff
changeset
|
493 """ |
|
381
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
494 default = kwds.pop("default", _MARKER) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
495 for path in paths: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
496 if isinstance(path, (list, tuple)): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
497 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
498 obj = self.getvarl(*path) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
499 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
500 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
501 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
502 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
|
503 |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
504 elif isinstance(path, dict): |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
505 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
506 namespace = path["namespace"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
507 p = path["path"] |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
508 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
509 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
|
510 " and a `namespace' key") |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
511 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
512 try: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
513 obj = self.getvarl(*p, namespace=namespace) |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
514 except KeyError: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
515 pass |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
516 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
517 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
|
518 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
519 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
|
520 if default is _MARKER: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
521 raise KeyError( |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
522 "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
|
523 else: |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
524 return default |
|
fe3dfd687621
Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents:
380
diff
changeset
|
525 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
526 def getvar_s(self, varname, default=_MARKER): |
|
24
fa65adab0b71
FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
23
diff
changeset
|
527 """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
|
528 |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
529 `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
|
530 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
|
531 and filters are applied. |
|
115
a5339d39af5c
Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents:
104
diff
changeset
|
532 |
|
202
2e66178a09d8
Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
533 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
|
534 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
535 """ |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
536 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
537 obj = self.getvar(varname) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
538 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
|
539 except KeyError: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
540 if default is _MARKER: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
541 raise |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
542 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
543 return default |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
544 |
|
368
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
545 def getfirstvar_s(self, *varnames, **kwds): |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
546 """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
|
547 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
|
548 |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
549 """ |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
550 default = kwds.pop("default", _MARKER) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
551 for varname in varnames: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
552 try: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
553 obj = self.getvar(varname) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
554 except KeyError: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
555 pass |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
556 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
557 return self.substitute_variables_in_obj(obj) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
558 if default is _MARKER: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
559 raise KeyError( |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
560 "none of the given variables found: %r" % (varnames,)) |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
561 else: |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
562 return default |
|
4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents:
364
diff
changeset
|
563 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
564 def _split_ns(self, s): |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
565 ns, sep, rest = s.partition(_NS_SEPARATOR) |
|
480
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
566 if sep: |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
567 return (self.unquote(ns), rest) |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
568 else: |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
569 return (None, ns) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
570 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
571 def _split_filters(self, s): |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
572 name, sep, filters = s.partition(_FILTER_SEPARATOR) |
|
480
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
573 if sep: |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
574 filters = filters.strip() |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
575 if filters: |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
576 return (name.rstrip(), |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
577 filters.split(_FILTER_SEPARATOR)) |
|
476
eddc0f7c6271
Optimize ._split_filters() for the most common case of no-filters: use s.partition() first
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
475
diff
changeset
|
578 else: |
|
480
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
579 return (name.rstrip(), []) |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
580 else: |
|
3cfc808e613b
Because we are Pytho 2.6+ remove some compatibility code for pre-Python 2.6
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
479
diff
changeset
|
581 return (name, []) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
582 |
|
489
b49164db1273
Remove unused keyword params for ._lookupvar().
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
488
diff
changeset
|
583 def _lookupvar(self, *path): |
|
314
043a6412be3c
Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
311
diff
changeset
|
584 """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
|
585 |
|
489
b49164db1273
Remove unused keyword params for ._lookupvar().
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
488
diff
changeset
|
586 :raise KeyError: An unexisting `path` raises a `KeyError` |
|
b49164db1273
Remove unused keyword params for ._lookupvar().
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
488
diff
changeset
|
587 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
588 """ |
|
419
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
589 if not path: |
|
079a82129110
Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents:
418
diff
changeset
|
590 return self |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
591 v = self.__cache.get(path, _MARKER) |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
592 if v is not _MARKER: |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
593 if v is _MISSING: |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
594 raise KeyError( |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
595 "Configuration variable %r not found" |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
596 " (negative internal cache value)" % (path,)) |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
597 else: |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
598 return v |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
599 eiref = self.expand_if_reference |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
600 try: |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
601 v = eiref(super(Configuration, self).__getitem__(path[0])) |
|
382
24db29162d09
Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents:
381
diff
changeset
|
602 for p in path[1:]: |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
603 v = eiref(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
|
604 except TypeError: |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
605 self.__cache[path] = _MISSING |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
606 raise KeyError( |
|
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
202
diff
changeset
|
607 "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
|
608 "(missing intermediate keys?)" % (path,)) |
|
499
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
609 except KeyError: |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
610 self.__cache[path] = _MISSING |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
611 raise |
|
f46932e8a84c
Configuration._lookupvar() now uses an internal cache (positive and negative).
Franz Glasner <fzglas.hg@dom66.de>
parents:
497
diff
changeset
|
612 self.__cache[path] = v |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
613 return v |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
614 |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
615 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
|
616 """ |
| 311 | 617 |
| 618 `key` must be a configuration reference URI without any | |
| 619 (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
|
620 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
621 """ |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
622 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
|
623 |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
624 def expand_if_reference(self, v): |
| 311 | 625 """Check whether `v` is a configuration reference and -- if true -- |
| 626 then expand it. | |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
627 |
| 310 | 628 `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
|
629 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
630 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
|
631 unchanged. |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
632 |
|
493
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
633 :raise KeyError: If the reverence cannot found |
|
6a0f761ff35b
Remove default (i.e. all keyword arguments) from .expand_if_reference() because no-one uses it
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
491
diff
changeset
|
634 |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
635 """ |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
636 if not isinstance(v, _TEXTTYPE): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
637 return v |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
638 if v.startswith(_STARTTOK_REF) and v.endswith(_ENDTOK_REF): |
|
488
298510ec8171
Simplify logic by not using many negations
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
487
diff
changeset
|
639 return self.expand_ref_uri( |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
640 v[len(_STARTTOK_REF):-len(_ENDTOK_REF)]) |
|
488
298510ec8171
Simplify logic by not using many negations
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
487
diff
changeset
|
641 else: |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
642 return v |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
643 |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
644 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
|
645 pu = urlsplit(uri) |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
646 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
|
647 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
|
648 if not pu.fragment: |
|
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
649 return self |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
650 if pu.fragment.startswith(_DOT): |
|
305
f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents:
304
diff
changeset
|
651 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
|
652 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
|
653 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
654 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
|
655 """Recursively expand variables in the object tree `obj`.""" |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
656 ty = type(obj) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
657 if issubclass(ty, _TEXTTYPE): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
658 # a string - really replace the value |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
659 return self.expand_variable(obj) |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
660 elif issubclass(ty, dict): |
|
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
661 newdict = ty() |
|
497
8e516f17cf95
Optimize .substitute_variables_in_obj: use dict.items() to avoid one dict lookup
Franz Glasner <fzglas.hg@dom66.de>
parents:
495
diff
changeset
|
662 for k, v in obj.items(): |
|
8e516f17cf95
Optimize .substitute_variables_in_obj: use dict.items() to avoid one dict lookup
Franz Glasner <fzglas.hg@dom66.de>
parents:
495
diff
changeset
|
663 newdict[k] = self.substitute_variables_in_obj(v) |
|
490
ea4b7fac02d6
Reorder type comparisons by usage
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
489
diff
changeset
|
664 return newdict |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
665 elif issubclass(ty, list): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
666 return [self.substitute_variables_in_obj(i) for i in obj] |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
667 elif issubclass(ty, tuple): |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
668 tmp = [self.substitute_variables_in_obj(i) for i in obj] |
|
491
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
669 return ty(tmp) |
|
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
670 elif issubclass(ty, set): |
|
de776953337b
Remove repeated type(obj) calls
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
490
diff
changeset
|
671 newset = ty() |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
672 for i in obj: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
673 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
|
674 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
675 return obj |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
676 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
677 def expand_variable(self, s): |
| 302 | 678 """Expand variables in the single string `s`""" |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
679 start = s.find(_STARTTOK, 0) |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
680 if start < 0: |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
681 return s |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
682 res = [] |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
683 res_append = res.append |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
684 rest = 0 |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
685 while start != -1: |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
686 res_append(s[rest:start]) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
687 end = s.find(_ENDTOK, start) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
688 if end < 0: |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
689 rest = start |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
690 break |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
691 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
|
692 try: |
|
357
dd454e1efea4
Use constants for the names of the "None" and "Empty" filters
Franz Glasner <fzglas.hg@dom66.de>
parents:
352
diff
changeset
|
693 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
|
694 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
|
695 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
|
696 elif EMPTY_FILTER in filters: |
|
352
2b209bdf6995
Implement the "Empty" filter.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
350
diff
changeset
|
697 varvalue = self._apply_filters( |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
698 filters, self.getvar_s(varname, |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
699 default=_EMPTY_STR)) |
|
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
|
700 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
|
701 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
|
702 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
|
703 except KeyError: |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
704 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
|
705 "%r" % (varname, s, ), |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
706 UserWarning, |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
707 stacklevel=1) |
|
8715e5cc59ac
Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents:
36
diff
changeset
|
708 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
|
709 # |
|
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
|
710 # 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
|
711 # 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
|
712 # |
|
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
|
713 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
|
714 return varvalue |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
715 if not varvalue: |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
716 # None and/or empty str are handled equally here |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
717 pass |
|
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
718 else: |
|
487
d7f6f2afcee2
Instead of using u(str(v)) use specialized functions for PY2 and PY3
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
485
diff
changeset
|
719 res_append(str_and_u(varvalue)) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
720 # don't re-evaluate because `self.getvar_s()` expands already |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
721 rest = end + 2 |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
722 start = s.find(_STARTTOK, rest) |
|
484
0259bd521729
Avoid repeaded string concatenations in .expand_variable(): use a list of string parts and proper indexes into the source string
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
483
diff
changeset
|
723 res_append(s[rest:]) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
724 return _EMPTY_STR.join(res) |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
725 |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
726 def _apply_filters(self, filters, value): |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
727 for name in filters: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
728 try: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
729 filterfn = lookup_filter(name) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
730 except KeyError: |
|
40
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
731 # |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
732 # 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
|
733 # a very serious error. |
|
8d66aef2f7fb
Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents:
39
diff
changeset
|
734 # |
|
16
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
735 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
|
736 else: |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
737 value = filterfn(self, value) |
|
f85dc4677c01
Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
738 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
|
739 |
|
500
34cd4f111134
"quote()" and "unquote()" are now static methods
Franz Glasner <fzglas.hg@dom66.de>
parents:
499
diff
changeset
|
740 @staticmethod |
|
34cd4f111134
"quote()" and "unquote()" are now static methods
Franz Glasner <fzglas.hg@dom66.de>
parents:
499
diff
changeset
|
741 def quote(s): |
| 333 | 742 """Replace important special characters in string `s` by replacing |
| 743 them with ``%xNN`` where `NN` are the two hexadecimal digits of the | |
| 744 characters unicode codepoint value. | |
| 745 | |
| 746 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
|
747 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``. |
| 333 | 748 |
| 749 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
|
750 |
|
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
|
751 """ |
|
483
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
752 # Quick check whether all of the chars are in _QUOTE_SAFE |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
753 if not s.rstrip(_QUOTE_SAFE): |
|
481
e3990ec55f2a
Use a quick-check for .quote() if no quoting is needed
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
480
diff
changeset
|
754 return s |
|
483
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
755 |
|
481
e3990ec55f2a
Use a quick-check for .quote() if no quoting is needed
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
480
diff
changeset
|
756 # Slow path |
|
483
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
757 re_encode = False |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
758 if PY2: |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
759 # Use the Unicode translation variant in PY2 |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
760 if isinstance(s, str): |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
761 s = s.decode("latin1") |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
762 re_encode = True |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
763 s = s.translate(_QUOTE_MAP) |
|
483
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
764 if re_encode: |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
765 return s.encode("latin1") |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
766 else: |
|
38e4be1882e3
Optimize .quote() by using str.translate() instead of repeatedly calling str.replace()
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
482
diff
changeset
|
767 return s |
|
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
|
768 |
|
500
34cd4f111134
"quote()" and "unquote()" are now static methods
Franz Glasner <fzglas.hg@dom66.de>
parents:
499
diff
changeset
|
769 @staticmethod |
|
34cd4f111134
"quote()" and "unquote()" are now static methods
Franz Glasner <fzglas.hg@dom66.de>
parents:
499
diff
changeset
|
770 def unquote(s): |
| 333 | 771 """Unquote the content of `s`: handle all patterns ``%xNN``, |
| 772 ``%uNNNN`` or ``%UNNNNNNNN``. | |
| 773 | |
| 774 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
|
775 |
|
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
|
776 """ |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
777 if _QUOTE not in s: |
|
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
|
778 return s |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
779 parts = s.split(_QUOTE) |
|
477
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
780 res = [parts[0]] |
|
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
781 res_append = res.append |
|
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
|
782 for p in parts[1:]: |
|
479
490f0c4665bb
Optimize .unquote(): instead of doing string comparisone do this with characters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
478
diff
changeset
|
783 try: |
|
490f0c4665bb
Optimize .unquote(): instead of doing string comparisone do this with characters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
478
diff
changeset
|
784 qc = p[0] |
|
490f0c4665bb
Optimize .unquote(): instead of doing string comparisone do this with characters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
478
diff
changeset
|
785 except IndexError: |
|
490f0c4665bb
Optimize .unquote(): instead of doing string comparisone do this with characters
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
478
diff
changeset
|
786 raise ValueError("unknown quote syntax string: {}".format(s)) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
787 if qc == _QUOTE_x: |
|
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
|
788 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
|
789 raise ValueError("quote syntax: length too small") |
|
477
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
790 res_append(uchr(int(p[1:3], 16))) |
|
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
791 res_append(p[3:]) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
792 elif qc == _QUOTE_u: |
|
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
|
793 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
|
794 raise ValueError("quote syntax: length too small") |
|
477
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
795 res_append(uchr(int(p[1:5], 16))) |
|
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
796 res_append(p[5:]) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
797 elif qc == _QUOTE_U: |
|
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
|
798 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
|
799 raise ValueError("quote syntax: length too small") |
|
477
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
800 res_append(uchr(int(p[1:9], 16))) |
|
1de6cc49776e
Optimize .unquote() by aliasing some methods to locals and avoiding the first append call
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
476
diff
changeset
|
801 res_append(p[9:]) |
|
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
|
802 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
|
803 raise ValueError("unknown quote syntax string: {}".format(s)) |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
804 return _EMPTY_STR.join(res) |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
805 |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
806 def jailed(self, rootpath=None, root=None, bind_root=True): |
| 399 | 807 """Return a "jailed" configuration of the current configuration. |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
808 |
| 399 | 809 :param rootpath: a sequence of strings that shall emcompass |
| 810 the chroot-like jail of the returned | |
| 811 configuration | |
| 812 :type rootpath: list or tuple | |
| 813 :param str root: a string path expression that shall encompass | |
| 814 the chroot-like jail of the returned configuration | |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
815 :param bool bind_root: if you do a :meth:`~.rebind` just after |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
816 creation of a jailed config you can set |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
817 `bind_root` to `False`; otherwise use |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
818 the default |
| 397 | 819 :return: a jailed (aka restricted) configuration |
| 820 :rtype: _JailedConfiguration | |
| 821 | |
| 399 | 822 Exactly one of `rootpath` or `root` must be given. |
| 823 | |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
824 """ |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
825 if rootpath is not None and root is not None: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
826 raise ValueError("only one of `rootpath' or `root' can be given") |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
827 if rootpath is None and root is None: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
828 raise ValueError("one of `rootpath' or `root' must be given") |
|
416
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
829 if rootpath is not None and not isinstance(rootpath, (list, tuple)): |
|
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
830 raise TypeError("`rootpath' must be a list or a tuple") |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
831 if root is not None: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
832 # convert to path |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
833 varns, varname = self._split_ns(root) |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
834 if varns: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
835 raise ValueError( |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
836 "jailed configurations do not support namespaces") |
|
416
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
837 if varname: |
|
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
838 rootpath = [ |
|
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
839 self.unquote(p) for p in root.split( |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
840 _HIER_SEPARATOR) |
|
416
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
841 ] |
|
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
842 else: |
|
2abde0d3c735
FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents:
412
diff
changeset
|
843 rootpath = tuple() |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
844 jc = _JailedConfiguration(*rootpath) |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
845 if bind_root: |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
846 jc.rebind(self) |
|
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
847 return jc |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
848 |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
849 |
|
412
816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents:
411
diff
changeset
|
850 class _JailedConfiguration(CoercingMethodsMixin): |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
851 |
| 397 | 852 """A jailed and restricted variant of :class:`Configuration`. |
| 853 | |
| 399 | 854 Restriction is two-fold: |
| 855 | |
| 856 - The access to configuration variables in `config` is restricted | |
| 857 to the configuration sub-tree that is configured in `path`. | |
| 858 | |
| 859 - Not all access-methods of :class:`Configuration` are implemented | |
| 860 yet. | |
| 861 | |
| 400 | 862 .. seealso:: :ref:`jailed-configuration` |
| 863 | |
| 399 | 864 .. note:: There is no namespace support. |
| 865 | |
| 866 .. note:: Do not call the constructor directly. Instantiate a jailed | |
| 867 configuration from the parent configuration's | |
| 868 :meth:`~.Configuration.jailed` factory method. | |
| 397 | 869 |
| 870 """ | |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
871 |
| 396 | 872 __slots__ = ("_base", "_path", "_pathstr") |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
873 |
|
404
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
874 is_jail = True |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
875 """Flag to show that this is a jail for another configuration""" |
|
6a5aea02f3d0
Implement a ".is_jail" flag for configuration objects: to test/assert whether we expect a jail configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
400
diff
changeset
|
876 |
|
411
3c95faa91dad
Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
406
diff
changeset
|
877 def __init__(self, *path): |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
878 super(_JailedConfiguration, self).__init__() |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
879 self._path = path |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
880 if path: |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
881 self._pathstr = \ |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
882 _HIER_SEPARATOR.join( |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
883 [Configuration.quote(p) for p in path]) \ |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
884 + _HIER_SEPARATOR |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
885 else: |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
886 self._pathstr = _EMPTY_STR |
|
405
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
887 |
|
406
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
888 @property |
|
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
889 def base(self): |
|
426
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
890 """Ask for the base (aka parent) configuration". |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
891 |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
892 This configuration is always unjailed. |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
893 |
|
84d4f82ffe59
Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents:
419
diff
changeset
|
894 """ |
|
406
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
895 return self._base |
|
5286df5aeefe
Allow a jailed configuration to return its base configuration via a property
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
405
diff
changeset
|
896 |
|
405
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
897 def rebind(self, new_base): |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
898 """Bind the jail to a new unjailed configuration `new_base`. |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
899 |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
900 The new configuration base also must have an existing path to |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
901 the root. |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
902 |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
903 :param Configuration new_base: the new base |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
904 |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
905 """ |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
906 if new_base.is_jail: |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
907 raise TypeError("can only bind to an unjailed configuration") |
|
af367e1d0950
Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
404
diff
changeset
|
908 self._base = new_base |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
909 # |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
910 # Early error out if the chroot does not exist but allow |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
911 # degenerated case if `self._path` is empty. |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
912 # |
|
449
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
913 if self._path and self._path not in new_base: |
|
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
914 raise KeyError( |
|
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
915 "base key path %r not available in the new base" |
|
5864977bb44f
FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
448
diff
changeset
|
916 % (self._path, )) |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
917 |
|
448
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
918 def __getattr__(self, name): |
| 469 | 919 """Attribute-style access. |
| 920 | |
| 921 Result values are interpolated (i.e. forwarded to | |
| 922 :meth:`~.getvarl_s`) | |
| 923 | |
| 924 """ | |
|
448
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
925 try: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
926 v = self._base.getvarl_s(*(self._path + (name, ))) |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
927 except KeyError: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
928 raise AttributeError("%s has no attribute %r" % (type(self), name)) |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
929 else: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
930 # Wrap a dict into another dict with attribute access support |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
931 if isinstance(v, dict): |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
932 return _AttributeDict(v) |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
933 else: |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
934 return v |
|
b95c12781497
Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
443
diff
changeset
|
935 |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
936 def __getitem__(self, key): |
| 469 | 937 """Mapping and list interface that forwards to :meth:`~.getvarl_s` |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
938 |
|
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
939 """ |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
940 if isinstance(key, tuple): |
|
440
f297c23f78f0
Optimize __getitem__() in jailed configurations: call base directly
Franz Glasner <fzglas.hg@dom66.de>
parents:
439
diff
changeset
|
941 return self._base.getvarl_s(*(self._path + key)) |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
942 elif isinstance(key, list): |
|
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
943 return self._base.getvarl_s(*(self._path + tuple(key))) |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
944 else: |
|
468
95df1a10259a
Index list-access for jailed configurations: be more generic by calling also __getitem__ in the base
Franz Glasner <fzglas.hg@dom66.de>
parents:
467
diff
changeset
|
945 return self._base.getvarl_s(*self._path)[key] |
|
437
bbc5b64e137a
- Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
432
diff
changeset
|
946 |
|
442
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
947 def get(self, key, default=None): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
948 if isinstance(key, tuple): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
949 return self._base.get(self._path + key, default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
950 elif isinstance(key, list): |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
951 return self._base.get(self._path + tuple(key), default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
952 else: |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
953 return self._base.get(self._path + (key, ), default=default) |
|
94cf5a8722d6
Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
441
diff
changeset
|
954 |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
955 def __contains__(self, key): |
| 469 | 956 """Containment support for containers""" |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
957 if isinstance(key, tuple): |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
958 return (self._path + key) in self._base |
|
441
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
959 elif isinstance(key, list): |
|
9d20fab53a19
FIX: Handle "list"-type keys in __contains__ and __getitem__ properly: convert to tuples for contatenation with tuple
Franz Glasner <fzglas.hg@dom66.de>
parents:
440
diff
changeset
|
960 return (self._path + tuple(key)) in self._base |
|
439
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
961 else: |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
962 return (self._path + (key, )) in self._base |
|
bd27da55483a
Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
438
diff
changeset
|
963 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
964 def getvarl(self, *path, **kwds): |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
965 return self._base.getvarl(*(self._path + path), **kwds) |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
966 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
967 def getkeysl(self, *path, **kwds): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
968 for k in self._base.getkeysl(*(self._path + path), **kwds): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
969 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
970 |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
971 def getfirstvarl(self, *paths, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
972 real_paths = [] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
973 for path in paths: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
974 if isinstance(path, (list, tuple)): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
975 real_paths.append(self._path + path) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
976 elif isinstance(path, dict): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
977 raise TypeError( |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
978 "a `dict' is not supported in a jailed configuration") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
979 else: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
980 raise TypeError("a paths item must be a list or tuple") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
981 return self._base.getfirstvarl(*real_paths, **kwds) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
982 |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
983 def getvarl_s(self, *path, **kwds): |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
984 return self._base.getvarl_s(*(self._path + path), **kwds) |
|
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
985 |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
986 def getfirstvarl_s(self, *paths, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
987 real_paths = [] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
988 for path in paths: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
989 if isinstance(path, (list, tuple)): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
990 real_paths.append(self._path + path) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
991 elif isinstance(path, dict): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
992 raise TypeError( |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
993 "a `dict' is not supported in a jailed configuration") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
994 else: |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
995 raise TypeError("a paths item must be a list or tuple") |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
996 return self._base.getfirstvarl_s(*real_paths, **kwds) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
997 |
|
428
090a25f36a3d
FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
426
diff
changeset
|
998 def getvar(self, varname, **kwds): |
|
090a25f36a3d
FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
426
diff
changeset
|
999 return self._base.getvar(self._pathstr + varname, **kwds) |
|
395
0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
389
diff
changeset
|
1000 |
|
418
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1001 def getkeys(self, varname): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1002 for k in self._base.getkeys(self._pathstr + varname): |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1003 yield k |
|
bb5f11abd12a
Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
Franz Glasner <fzglas.hg@dom66.de>
parents:
417
diff
changeset
|
1004 |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1005 def getfirstvar(self, *varnames, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1006 real_varnames = [self._pathstr + vn for vn in varnames] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1007 return self._base.getfirstvar(*real_varnames, **kwds) |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1008 |
|
428
090a25f36a3d
FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
426
diff
changeset
|
1009 def getvar_s(self, varname, **kwds): |
|
090a25f36a3d
FIX: Allow jailed configurations to use correctly use base configurations that use a different "default" marker object.
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
426
diff
changeset
|
1010 return self._base.getvar_s(self._pathstr + varname, **kwds) |
|
398
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1011 |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1012 def getfirstvar_s(self, *varnames, **kwds): |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1013 real_varnames = [self._pathstr + vn for vn in varnames] |
|
b1f82b853290
Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents:
397
diff
changeset
|
1014 return self._base.getfirstvar_s(*real_varnames, **kwds) |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1015 |
|
459
9dc9cef1b9cd
Implement proper iteration support for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
455
diff
changeset
|
1016 def __iter__(self): |
| 469 | 1017 """Iteration support for containers""" |
|
459
9dc9cef1b9cd
Implement proper iteration support for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
455
diff
changeset
|
1018 return iter(self._base.getvarl_s(*self._path)) |
|
9dc9cef1b9cd
Implement proper iteration support for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents:
455
diff
changeset
|
1019 |
|
467
9fcdc42a0457
len-support for jailed configurations: implement a proper __len__() method
Franz Glasner <fzglas.hg@dom66.de>
parents:
460
diff
changeset
|
1020 def __len__(self): |
| 469 | 1021 """Length support for containers""" |
|
467
9fcdc42a0457
len-support for jailed configurations: implement a proper __len__() method
Franz Glasner <fzglas.hg@dom66.de>
parents:
460
diff
changeset
|
1022 return len(self._base.getvarl(*self._path)) |
|
9fcdc42a0457
len-support for jailed configurations: implement a proper __len__() method
Franz Glasner <fzglas.hg@dom66.de>
parents:
460
diff
changeset
|
1023 |
|
460
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1024 if PY2: |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1025 |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1026 def __nonzero__(self): |
| 469 | 1027 """Map- and list-style evaluation in boolean context""" |
|
460
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1028 return bool(self._base.getvarl_s(*self._path)) |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1029 |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1030 else: |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1031 |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1032 def __bool__(self): |
| 469 | 1033 """Map- and list-style evaluation in boolean context""" |
|
460
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1034 return bool(self._base.getvarl_s(*self._path)) |
|
d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
Franz Glasner <fzglas.hg@dom66.de>
parents:
459
diff
changeset
|
1035 |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1036 def jailed(self, rootpath=None, root=None, bind_root=True): |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1037 """Return a "jailed" configuration that effectively is a |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1038 subjail of the current jail |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1039 |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1040 For a more complete description see :meth:`.Configuration.jailed`. |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1041 |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1042 """ |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1043 if rootpath is not None and root is not None: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1044 raise ValueError("only one of `rootpath' or `root' can be given") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1045 if rootpath is None and root is None: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1046 raise ValueError("one of `rootpath' or `root' must be given") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1047 if rootpath is not None and not isinstance(rootpath, (list, tuple)): |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1048 raise TypeError("`rootpath' must be a list or a tuple") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1049 if root is not None: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1050 # convert to path |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1051 varns, varname = self._base._split_ns(root) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1052 if varns: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1053 raise ValueError( |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1054 "sub-jails do not support namespaces") |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1055 if varname: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1056 rootpath = [ |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1057 self._base.unquote(p) for p in varname.split( |
|
495
3f0c932588fc
Performance: module-level variable lookup is much faster (similar to local) than class-level (either via CLASS.VARIABLE or self.VARIABLE).
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
493
diff
changeset
|
1058 _HIER_SEPARATOR) |
|
417
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1059 ] |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1060 else: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1061 rootpath = tuple() |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1062 if self._path: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1063 new_rootpath = self._path + tuple(rootpath) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1064 else: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1065 new_rootpath = rootpath |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1066 sjc = _JailedConfiguration(*new_rootpath) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1067 if bind_root: |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1068 sjc.rebind(self._base) |
|
83d537f1dfbb
Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents:
416
diff
changeset
|
1069 return sjc |
|
432
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1070 |
|
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1071 def __repr__(self): |
|
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1072 r = "_JailedConfiguration(rootpath=%s)" % n(repr(self._path)) |
|
b96f49c9c76b
Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents:
428
diff
changeset
|
1073 return r |
