annotate configmix/config.py @ 449:5864977bb44f

FIX: KeyError formatting. For jailed configs: self._path is a tuple and must be wrapped into a tuple when used for "%"-style error formatting.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Tue, 14 Dec 2021 14:28:10 +0100
parents b95c12781497
children 12f25ac6a13b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
3 # :Copyright: (c) 2015-2021, Franz Glasner. All rights reserved.
296
eed16a1ec8f3 Use SPDX license identifiers (either full or short) all over the package
Franz Glasner <fzglas.hg@dom66.de>
parents: 251
diff changeset
4 # :License: BSD-3-Clause. See LICENSE.txt for details.
208
bbe8513ea649 Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents: 207
diff changeset
5 # :-
82
218807d7d883 Remove header markup from the Python files and put them into the doc .rst files
Franz Glasner <hg@dom66.de>
parents: 79
diff changeset
6 """The unified configuration dictionary with attribute support or
54
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 40
diff changeset
7 variable substitution.
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
8
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
9 """
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
10
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
11 from __future__ import division, absolute_import, print_function
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
12
250
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
13
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
14 __all__ = ["Configuration"]
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
15
ff964825a75a Style: placement of "__all__"
Franz Glasner <fzglas.hg@dom66.de>
parents: 249
diff changeset
16
39
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
17 import warnings
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
18 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
19 from collections import OrderedDict as ConfigurationBase
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
20 except ImportError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
21 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
22 from ordereddict import OrderedDict as ConfigurationBase
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23 except ImportError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24 ConfigurationBase = dict
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
25 try:
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
26 from urllib.parse import urlsplit
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
27 except ImportError:
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
28 from urlparse import urlsplit
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
29
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
30 from .variables import lookup_varns, lookup_filter
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
31 from .compat import u, uchr, n
357
dd454e1efea4 Use constants for the names of the "None" and "Empty" filters
Franz Glasner <fzglas.hg@dom66.de>
parents: 352
diff changeset
32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
33
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
34
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
35 _MARKER = object()
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
36
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
37
17
94b5e94fae44 Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 16
diff changeset
38 class _AttributeDict(ConfigurationBase):
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
39
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
40 def __getattr__(self, name):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
41 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
42 v = self[name]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
43 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
44 raise AttributeError("%s has no attribute %r" % (type(self), name))
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
45 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
46 # Wrap a dict into another dict with attribute access support
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
47 if isinstance(v, dict):
29
17af7a78710c FIX: Renaming a class was not really complete
Franz Glasner <hg@dom66.de>
parents: 25
diff changeset
48 return _AttributeDict(v)
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
49 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
50 return v
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
51
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
52
412
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
53 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
54
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
55 """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
56 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
57 float).
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
58
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
59 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
60 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
61
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 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
65 """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
66 number.
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
67
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 s = self.getvarl_s(*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
70 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
71 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
72 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
73 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
74
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
75 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
76 """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
77 number.
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
78
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 s = self.getfirstvarl_s(*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
81 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
82 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
83 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
84 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
85
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
86 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
87 """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
88 number.
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
89
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 s = self.getvar_s(varname, default=default)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
92 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
93 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
94 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
95 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
96
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
97 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
98 """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
99 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
100
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 s = self.getfirstvar_s(*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
103 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
104 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
105 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
106 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
107
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
108 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
109 """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
110 boolean
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
111
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 s = self.getvarl_s(*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
114 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
115 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
116 if sl not in self._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
117 raise ValueError("Not a boolean: %r" % s)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
118 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
119 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
120 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
121
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
122 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
123 """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
124 boolean
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
125
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 s = self.getfirstvarl_s(*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
128 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
129 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
130 if sl not in self._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
131 raise ValueError("Not a boolean: %r" % s)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
132 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
133 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
134 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
135
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
136 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
137 """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
138 boolean
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
139
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 s = self.getvar_s(varname, default=default)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
142 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
143 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
144 if sl not in self._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
145 raise ValueError("Not a boolean: %r" % s)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
146 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
147 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
148 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
149
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
150 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
151 """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
152 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
153
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 s = self.getfirstvar_s(*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
156 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
157 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
158 if sl not in self._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
159 raise ValueError("Not a boolean: %r" % s)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
160 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
161 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
162 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
163
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
164 # 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
165 _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
166 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
167 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
168 }
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 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
171 """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
172 float
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
173
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 s = self.getvarl_s(*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
176 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
177 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
178 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
179 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
180
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
181 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
182 """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
183 float
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
184
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 s = self.getfirstvarl_s(*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
187 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
188 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
189 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
190 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
191
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
192 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
193 """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
194 float
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
195
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 s = self.getvar_s(varname, default)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
198 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
199 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
200 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
201 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
202
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
203 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
204 """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
205 float
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
206
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 s = self.getfirstvar_s(varname, default)
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
209 if isinstance(s, Configuration._TEXTTYPE):
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
210 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
211 else:
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
212 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
213
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 class Configuration(CoercingMethodsMixin, _AttributeDict):
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
216
18
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
217 """The configuration dictionary with attribute support or
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
218 variable substitution.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
219
322
4aa19e04ff65 Comment: typo
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 321
diff changeset
220 .. note:: When retrieving by attribute names variables will *not*
18
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
221 substituted.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
222
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
223 """
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
224
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
225 # Speed
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
226 _TEXTTYPE = type(u(""))
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
227 _STARTTOK = u(b"{{")
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
228 _ENDTOK = u(b"}}")
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
229 _HIER_SEPARATOR = u(b'.')
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
230 _NS_SEPARATOR = u(b':')
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
231 _FILTER_SEPARATOR = u(b'|')
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
232 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
233 _ENDTOK_REF = _ENDTOK
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
234 _DOT = u(b'.')
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
235 _QUOTE = u(b'%')
325
ab33d51f3412 By default quote the "#" character also because it is typically a comment in all the configuration file formats.
Franz Glasner <fzglas.hg@dom66.de>
parents: 322
diff changeset
236 _COMMENT = u(b'#')
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
237
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
238 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
239 """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
240
437
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
241 def __getitem__(self, key):
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
242 """Mapping interface that forwards to :meth:`~.getvarl_s`
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
243
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
244 """
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
245 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
246 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
247 else:
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
248 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
249
442
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
250 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
251 """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
252
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
253 """
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
254 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
255 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
256 else:
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
257 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
258
439
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
259 def __contains__(self, key):
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
260 if isinstance(key, (tuple, list)):
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
261 # 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
262 try:
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
263 self._lookupvar(*key)
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
264 except KeyError:
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
265 return False
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
266 else:
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
267 return True
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
268 else:
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
269 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
270
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
271 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
272 """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
273
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
274 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
275
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
276 """
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
277 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
278
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
279 def getvarl(self, *path, **kwds):
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
280 """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
281 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
282
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
283 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
284
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
285 Quoting of `path` and `namespace` is *not* needed and wrong.
333
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
286
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
287 """
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
288 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
289 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
290 try:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
291 if not namespace:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
292 lookupfn = self._lookupvar
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
293 else:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
294 if namespace == REF_NAMESPACE:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
295 lookupfn = self._lookupref
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
296 else:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
297 lookupfn = lookup_varns(namespace)
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
298 varvalue = lookupfn(*path)
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
299 except KeyError:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
300 if default is _MARKER:
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
301 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
302 else:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
303 return default
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
304 else:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
305 return varvalue
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
306
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
307 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
308 """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
309
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
310 :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
311 :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
312
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
313 .. 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
314
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
315 """
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
316 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
317 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
318 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
319 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
320
381
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
321 def getfirstvarl(self, *paths, **kwds):
385
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
322 """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
323 variable in the `paths` list.
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
324
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
325 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
326 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
327 and "path".
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
328 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
329 to be `None`.
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
330
389
Franz Glasner <fzglas.hg@dom66.de>
parents: 388
diff changeset
331 Note that a caller that wants to use variables from a non-default
Franz Glasner <fzglas.hg@dom66.de>
parents: 388
diff changeset
332 namespace must use a sequence of dicts.
Franz Glasner <fzglas.hg@dom66.de>
parents: 388
diff changeset
333
385
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
334 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
335
4beeb291926d First documentation of getfirstvarl()
Franz Glasner <fzglas.hg@dom66.de>
parents: 384
diff changeset
336 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
337
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
338 """
381
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
339 default = kwds.pop("default", _MARKER)
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
340 for path in paths:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
341 if isinstance(path, (list, tuple)):
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
342 try:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
343 varvalue = self.getvarl(*path)
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
344 except KeyError:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
345 pass
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
346 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
347 return varvalue
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
348 elif isinstance(path, dict):
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
349 try:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
350 namespace = path["namespace"]
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
351 p = path["path"]
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
352 except KeyError:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
353 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
354 " and a `namespace' key")
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
355 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
356 try:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
357 varvalue = self.getvarl(*p, namespace=namespace)
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
358 except KeyError:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
359 pass
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
360 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
361 return varvalue
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
362 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
363 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
364 if default is _MARKER:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
365 raise KeyError(
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
366 "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
367 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
368 return default
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
369
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
370 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
371 """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
372 variables from other namespaces.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
373
202
2e66178a09d8 Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
374 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
375
333
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
376 Special characters (e.g. ``:`` and ``.``) must be quoted when using
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
377 the default namespace.
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
378
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
379 See also :meth:`~.quote`.
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
380
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
381 """
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
382 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
383 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
384 if varname:
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
385 varnameparts = [
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
386 self.unquote(vp)
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
387 for vp in varname.split(self._HIER_SEPARATOR)
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
388 ]
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
389 else:
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
390 varnameparts = tuple()
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
391 else:
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
392 varnameparts = (varname,)
364
1941f0188e81 FIX: Handle a "default" keyword parameter in ".getvar()" properly.
Franz Glasner <fzglas.hg@dom66.de>
parents: 357
diff changeset
393 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
394
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
395 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
396 """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
397
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
398 :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
399 :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
400
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
401 .. 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
402
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
403 """
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
404 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
405 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
406
368
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
407 def getfirstvar(self, *varnames, **kwds):
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
408 """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
409 in the list of given variables in `varnames`.
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
410
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
411 """
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
412 default = kwds.pop("default", _MARKER)
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
413 for varname in varnames:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
414 try:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
415 varvalue = self.getvar(varname)
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
416 except KeyError:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
417 pass
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
418 else:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
419 return varvalue
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
420 if default is _MARKER:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
421 raise KeyError(
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
422 "none of the given variables found: %r" % (varnames,))
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
423 else:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
424 return default
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
425
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
426 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
427 """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
428
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
429 `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
430 :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
431 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
432 applied.
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
433
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
434 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
435
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
436 """
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
437 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
438 namespace = kwds.pop("namespace", None)
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
439 try:
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
440 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
441 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
442 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
443 if default is _MARKER:
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
444 raise
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
445 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
446 return default
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
447
381
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
448 def getfirstvarl_s(self, *paths, **kwds):
388
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
449 """A variant of :meth:`~.getfirstvarl` that does variable
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
450 interpolation.
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
451
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
452 `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
453 But variables will be interpolated
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
454 recursively within the variable values and filters are
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
455 applied.
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
456
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
457 For more details see chapter :ref:`variable-interpolation`.
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
458
13427f37abab Docs: document Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 385
diff changeset
459 """
381
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
460 default = kwds.pop("default", _MARKER)
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
461 for path in paths:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
462 if isinstance(path, (list, tuple)):
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
463 try:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
464 obj = self.getvarl(*path)
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
465 except KeyError:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
466 pass
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
467 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
468 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
469
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
470 elif isinstance(path, dict):
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
471 try:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
472 namespace = path["namespace"]
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
473 p = path["path"]
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
474 except KeyError:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
475 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
476 " and a `namespace' key")
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
477 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
478 try:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
479 obj = self.getvarl(*p, namespace=namespace)
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
480 except KeyError:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
481 pass
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
482 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
483 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
484 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
485 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
486 if default is _MARKER:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
487 raise KeyError(
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
488 "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
489 else:
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
490 return default
fe3dfd687621 Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
Franz Glasner <fzglas.hg@dom66.de>
parents: 380
diff changeset
491
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
492 def getvar_s(self, varname, default=_MARKER):
24
fa65adab0b71 FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 23
diff changeset
493 """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
494
115
a5339d39af5c Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents: 104
diff changeset
495 `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
496 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
497 and filters are applied.
115
a5339d39af5c Begin the documentation of variables and its expansion
Franz Glasner <hg@dom66.de>
parents: 104
diff changeset
498
202
2e66178a09d8 Docu: Ban "keyword expansion" -- use "variable interpolation" instead
Franz Glasner <fzglas.hg@dom66.de>
parents: 156
diff changeset
499 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
500
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
501 """
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
502 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
503 obj = self.getvar(varname)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
504 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
505 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
506 if default is _MARKER:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
507 raise
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
508 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
509 return default
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
510
368
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
511 def getfirstvar_s(self, *varnames, **kwds):
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
512 """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
513 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
514
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
515 """
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
516 default = kwds.pop("default", _MARKER)
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
517 for varname in varnames:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
518 try:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
519 obj = self.getvar(varname)
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
520 except KeyError:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
521 pass
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
522 else:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
523 return self.substitute_variables_in_obj(obj)
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
524 if default is _MARKER:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
525 raise KeyError(
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
526 "none of the given variables found: %r" % (varnames,))
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
527 else:
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
528 return default
4ee53f6fcac1 Implement ".getfirstvar()" and ".getfirstvar_s()".
Franz Glasner <fzglas.hg@dom66.de>
parents: 364
diff changeset
529
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
530 def _split_ns(self, s):
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
531 nameparts = s.split(self._NS_SEPARATOR, 1)
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
532 if len(nameparts) == 1:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
533 return (None, s, )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
534 else:
329
d81d2cdf4925 FIX: Handle the unquoting of namespaces within Configuration.getvar() properly.
Franz Glasner <fzglas.hg@dom66.de>
parents: 326
diff changeset
535 return (self.unquote(nameparts[0]), nameparts[1], )
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
536
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
537 def _split_filters(self, s):
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
538 nameparts = s.split(self._FILTER_SEPARATOR)
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
539 if len(nameparts) == 1:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
540 return (s, [], )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
541 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
542 return (nameparts[0].rstrip(), nameparts[1:], )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
543
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
544 def _lookupvar(self, *path, **kwds):
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
545 """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
546
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
547 If no default is given an unexisting `path` raises a `KeyError`
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
548 else `default` is returned.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
549 """
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
550 default = kwds.pop("default", _MARKER)
419
079a82129110 Allow the empty variable name to retrieve the root configuration object
Franz Glasner <fzglas.hg@dom66.de>
parents: 418
diff changeset
551 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
552 return self
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
553 try:
438
Franz Glasner <fzglas.hg@dom66.de>
parents: 437
diff changeset
554 v = self.expand_if_reference(
Franz Glasner <fzglas.hg@dom66.de>
parents: 437
diff changeset
555 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
556 for p in path[1:]:
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
557 v = self.expand_if_reference(v[p])
104
d9f9a06e0c49 Make a better error message for "TypeError" exceptions when looking up variables.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 82
diff changeset
558 except TypeError:
207
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 202
diff changeset
559 raise KeyError(
b3b5ed34d180 Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents: 202
diff changeset
560 "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
561 "(missing intermediate keys?)" % (path,))
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
562 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
563 if default is _MARKER:
314
043a6412be3c Implemented new access methods .getvarl() and .getvarl_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 311
diff changeset
564 raise KeyError(
382
24db29162d09 Renamed "names" arguments into the more proper "path"
Franz Glasner <fzglas.hg@dom66.de>
parents: 381
diff changeset
565 "Configuration variable %r not found" % (path,))
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
566 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
567 return default
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
568 return v
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
569
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
570 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
571 """
311
Franz Glasner <fzglas.hg@dom66.de>
parents: 310
diff changeset
572
Franz Glasner <fzglas.hg@dom66.de>
parents: 310
diff changeset
573 `key` must be a configuration reference URI without any
Franz Glasner <fzglas.hg@dom66.de>
parents: 310
diff changeset
574 (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
575
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
576 """
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
577 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
578
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
579 def expand_if_reference(self, v, default=_MARKER):
311
Franz Glasner <fzglas.hg@dom66.de>
parents: 310
diff changeset
580 """Check whether `v` is a configuration reference and -- if true --
Franz Glasner <fzglas.hg@dom66.de>
parents: 310
diff changeset
581 then expand it.
305
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
582
310
801cd16223e4 FIX: Docu
Franz Glasner <fzglas.hg@dom66.de>
parents: 305
diff changeset
583 `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
584
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
585 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
586 unchanged.
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
587
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
588 """
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
589 if not isinstance(v, self._TEXTTYPE):
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
590 return v
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
591 if not v.startswith(self._STARTTOK_REF) \
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
592 or not v.endswith(self._ENDTOK_REF):
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
593 return v
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
594 return self.expand_ref_uri(
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
595 v[len(self._STARTTOK_REF):-len(self._ENDTOK_REF)],
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
596 default=default)
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
597
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
598 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
599 pu = urlsplit(uri)
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
600 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
601 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
602 if not pu.fragment:
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
603 return self
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
604 if pu.fragment.startswith(self._DOT):
f529ca46dd50 Implemented the "ref" namespace to get configuration tree references.
Franz Glasner <fzglas.hg@dom66.de>
parents: 304
diff changeset
605 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
606 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
607
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
608 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
609 """Recursively expand variables in the object tree `obj`."""
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
610 if isinstance(obj, self._TEXTTYPE):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
611 # a string - really replace the value
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
612 return self.expand_variable(obj)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
613 elif isinstance(obj, list):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
614 return [self.substitute_variables_in_obj(i) for i in obj]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
615 elif isinstance(obj, tuple):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
616 tmp = [self.substitute_variables_in_obj(i) for i in obj]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
617 return type(obj)(tmp)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
618 elif isinstance(obj, dict):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
619 newdict = type(obj)()
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
620 for k in obj:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
621 newdict[k] = self.substitute_variables_in_obj(obj[k])
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
622 return newdict
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
623 elif isinstance(obj, set):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
624 newset = type(obj)()
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
625 for i in obj:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
626 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
627 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
628 return obj
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
629
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
630 def expand_variable(self, s):
302
Franz Glasner <fzglas.hg@dom66.de>
parents: 296
diff changeset
631 """Expand variables in the single string `s`"""
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
632 start = s.find(self._STARTTOK, 0)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
633 while start != -1:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
634 end = s.find(self._ENDTOK, start)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
635 if end < 0:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
636 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
637 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
638 try:
357
dd454e1efea4 Use constants for the names of the "None" and "Empty" filters
Franz Glasner <fzglas.hg@dom66.de>
parents: 352
diff changeset
639 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
640 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
641 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
642 elif EMPTY_FILTER in filters:
352
2b209bdf6995 Implement the "Empty" filter.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 350
diff changeset
643 varvalue = self._apply_filters(
2b209bdf6995 Implement the "Empty" filter.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 350
diff changeset
644 filters, self.getvar_s(varname, default=u("")))
349
83f76a41cf7c Implement a special filter named "None" that suppresses "KeyErrors" from interpolation lookups and returns a Python "None" instead
Franz Glasner <fzglas.hg@dom66.de>
parents: 334
diff changeset
645 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
646 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
647 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
648 except KeyError:
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
649 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
650 "%r" % (varname, s, ),
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
651 UserWarning,
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
652 stacklevel=1)
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
653 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
654 #
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
655 # 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
656 # 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
657 #
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
658 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
659 return varvalue
350
9d729c479dc2 FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 349
diff changeset
660 if varvalue is None:
9d729c479dc2 FIX: Do not apply the type convertion None -> "" if the expansions comprises the whole expression
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 349
diff changeset
661 varvalue = u("")
249
1e38ccfba3de Use explicit type conversion instead of an implicit one.
Franz Glasner <fzglas.hg@dom66.de>
parents: 248
diff changeset
662 replaced = s[:start] + u(str(varvalue))
248
13283057a21e Do not use ".format()" but string concatenation or (when conversion to Unicode is needed) the faster %s method
Franz Glasner <fzglas.hg@dom66.de>
parents: 237
diff changeset
663 s = replaced + s[end+2:]
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
664 # don't re-evaluate because `self.getvar_s()` expands already
23
c77cb6bc8eeb FIX: Handle non-str types in variable substitutions properly
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 18
diff changeset
665 start = s.find(self._STARTTOK, len(replaced))
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
666 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
667
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
668 def _apply_filters(self, filters, value):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
669 for name in filters:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
670 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
671 filterfn = lookup_filter(name)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
672 except KeyError:
40
8d66aef2f7fb Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents: 39
diff changeset
673 #
8d66aef2f7fb Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents: 39
diff changeset
674 # 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
675 # a very serious error.
8d66aef2f7fb Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents: 39
diff changeset
676 #
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
677 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
678 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
679 value = filterfn(self, value)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
680 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
681
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
682 @classmethod
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
683 def quote(klass, s):
333
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
684 """Replace important special characters in string `s` by replacing
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
685 them with ``%xNN`` where `NN` are the two hexadecimal digits of the
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
686 characters unicode codepoint value.
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
687
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
688 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
689 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``.
333
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
690
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
691 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
692
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
693 """
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
694 qc = klass._QUOTE
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
695 s = s.replace(qc, qc + "x25")
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
696 s = s.replace(klass._DOT, qc + "x2e")
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
697 s = s.replace(klass._NS_SEPARATOR, qc + "x3a")
325
ab33d51f3412 By default quote the "#" character also because it is typically a comment in all the configuration file formats.
Franz Glasner <fzglas.hg@dom66.de>
parents: 322
diff changeset
698 s = s.replace(klass._COMMENT, qc + "x23")
326
b7abfbfe806d By default quote '"' and "'" also
Franz Glasner <fzglas.hg@dom66.de>
parents: 325
diff changeset
699 s = s.replace(klass._FILTER_SEPARATOR, qc + "x7c")
b7abfbfe806d By default quote '"' and "'" also
Franz Glasner <fzglas.hg@dom66.de>
parents: 325
diff changeset
700 s = s.replace('"', qc + "x22")
334
a04cd5dbcd2c By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 333
diff changeset
701 s = s.replace("'", qc + "x27")
a04cd5dbcd2c By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 333
diff changeset
702 s = s.replace('{', qc + "x7b")
a04cd5dbcd2c By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 333
diff changeset
703 s = s.replace('}', qc + "x7d")
a04cd5dbcd2c By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 333
diff changeset
704 s = s.replace('[', qc + "x5b")
a04cd5dbcd2c By default quote also the characters "{", "}", "[" and "]" because they are special in YAML
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 333
diff changeset
705 return s.replace(']', qc + "x5d")
320
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
706
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
707 @classmethod
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
708 def unquote(klass, s):
333
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
709 """Unquote the content of `s`: handle all patterns ``%xNN``,
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
710 ``%uNNNN`` or ``%UNNNNNNNN``.
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
711
5ec0ae3bb8db Docs: quoting
Franz Glasner <fzglas.hg@dom66.de>
parents: 329
diff changeset
712 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
713
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
714 """
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
715 if klass._QUOTE not in s:
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
716 return s
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
717 res = []
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
718 parts = s.split(klass._QUOTE)
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
719 res.append(parts[0])
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
720 for p in parts[1:]:
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
721 if p.startswith(u(b'x')):
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
722 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
723 raise ValueError("quote syntax: length too small")
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
724 res.append(uchr(int(p[1:3], 16)))
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
725 res.append(p[3:])
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
726 elif p.startswith(u(b'u')):
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
727 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
728 raise ValueError("quote syntax: length too small")
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
729 res.append(uchr(int(p[1:5], 16)))
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
730 res.append(p[5:])
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
731 elif p.startswith(u(b'U')):
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
732 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
733 raise ValueError("quote syntax: length too small")
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
734 res.append(uchr(int(p[1:9], 16)))
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
735 res.append(p[9:])
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
736 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
737 raise ValueError("unknown quote syntax string: {}".format(s))
98490375d90c Allow variable name quoting to be used in .getvar() and .getvar_s() and references
Franz Glasner <fzglas.hg@dom66.de>
parents: 314
diff changeset
738 return ''.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
739
411
3c95faa91dad Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 406
diff changeset
740 def jailed(self, rootpath=None, root=None, bind_root=True):
399
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
741 """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
742
399
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
743 :param rootpath: a sequence of strings that shall emcompass
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
744 the chroot-like jail of the returned
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
745 configuration
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
746 :type rootpath: list or tuple
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
747 :param str root: a string path expression that shall encompass
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
748 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
749 :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
750 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
751 `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
752 the default
397
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
753 :return: a jailed (aka restricted) configuration
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
754 :rtype: _JailedConfiguration
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
755
399
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
756 Exactly one of `rootpath` or `root` must be given.
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
757
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
758 """
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
759 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
760 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
761 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
762 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
763 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
764 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
765 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
766 # 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
767 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
768 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
769 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
770 "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
771 if varname:
2abde0d3c735 FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 412
diff changeset
772 rootpath = [
2abde0d3c735 FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 412
diff changeset
773 self.unquote(p) for p in root.split(
2abde0d3c735 FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 412
diff changeset
774 self._HIER_SEPARATOR)
2abde0d3c735 FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 412
diff changeset
775 ]
2abde0d3c735 FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 412
diff changeset
776 else:
2abde0d3c735 FIX: Handle .jailed() with an empty "root" properly
Franz Glasner <fzglas.hg@dom66.de>
parents: 412
diff changeset
777 rootpath = tuple()
411
3c95faa91dad Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 406
diff changeset
778 jc = _JailedConfiguration(*rootpath)
3c95faa91dad Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 406
diff changeset
779 if bind_root:
3c95faa91dad Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 406
diff changeset
780 jc.rebind(self)
3c95faa91dad Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 406
diff changeset
781 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
782
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
783
412
816327e178b0 Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
Franz Glasner <fzglas.hg@dom66.de>
parents: 411
diff changeset
784 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
785
397
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
786 """A jailed and restricted variant of :class:`Configuration`.
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
787
399
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
788 Restriction is two-fold:
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
789
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
790 - The access to configuration variables in `config` is restricted
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
791 to the configuration sub-tree that is configured in `path`.
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
792
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
793 - Not all access-methods of :class:`Configuration` are implemented
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
794 yet.
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
795
400
Franz Glasner <fzglas.hg@dom66.de>
parents: 399
diff changeset
796 .. seealso:: :ref:`jailed-configuration`
Franz Glasner <fzglas.hg@dom66.de>
parents: 399
diff changeset
797
399
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
798 .. note:: There is no namespace support.
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
799
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
800 .. note:: Do not call the constructor directly. Instantiate a jailed
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
801 configuration from the parent configuration's
Franz Glasner <fzglas.hg@dom66.de>
parents: 398
diff changeset
802 :meth:`~.Configuration.jailed` factory method.
397
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
803
Franz Glasner <fzglas.hg@dom66.de>
parents: 396
diff changeset
804 """
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
396
Franz Glasner <fzglas.hg@dom66.de>
parents: 395
diff changeset
806 __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
807
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
808 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
809 """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
810
411
3c95faa91dad Optimize the creation of a jailed config.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 406
diff changeset
811 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
812 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
813 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
814 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
815 self._pathstr = \
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
816 Configuration._HIER_SEPARATOR.join(
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
817 [Configuration.quote(p) for p in 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
818 + Configuration._HIER_SEPARATOR
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
819 else:
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
820 self._pathstr = ''
405
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
821
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
822 @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
823 def base(self):
426
84d4f82ffe59 Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents: 419
diff changeset
824 """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
825
84d4f82ffe59 Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents: 419
diff changeset
826 This configuration is always unjailed.
84d4f82ffe59 Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents: 419
diff changeset
827
84d4f82ffe59 Docs: more on jails and sub-jails
Franz Glasner <fzglas.hg@dom66.de>
parents: 419
diff changeset
828 """
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
829 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
830
405
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
831 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
832 """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
833
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
834 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
835 the root.
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
836
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
837 :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
838
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
839 """
af367e1d0950 Allow to rebind a jailed configuration to another "similar" base
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 404
diff changeset
840 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
841 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
842 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
843 #
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
844 # 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
845 # 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
846 #
449
5864977bb44f FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 448
diff changeset
847 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
848 raise KeyError(
5864977bb44f FIX: KeyError formatting.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 448
diff changeset
849 "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
850 % (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
851
448
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
852 def __getattr__(self, name):
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
853 try:
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
854 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
855 except KeyError:
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
856 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
857 else:
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
858 # 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
859 if isinstance(v, dict):
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
860 return _AttributeDict(v)
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
861 else:
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
862 return v
b95c12781497 Attribute-style access for jailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 443
diff changeset
863
437
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
864 def __getitem__(self, key):
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
865 """Mapping interface that forwards to :meth:`~.getvarl_s`
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
866
bbc5b64e137a - Dict-level access to a configuration key now does variable interpolation.
Franz Glasner <fzglas.hg@dom66.de>
parents: 432
diff changeset
867 """
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
868 if isinstance(key, tuple):
440
f297c23f78f0 Optimize __getitem__() in jailed configurations: call base directly
Franz Glasner <fzglas.hg@dom66.de>
parents: 439
diff changeset
869 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
870 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
871 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
872 else:
440
f297c23f78f0 Optimize __getitem__() in jailed configurations: call base directly
Franz Glasner <fzglas.hg@dom66.de>
parents: 439
diff changeset
873 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
874
442
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
875 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
876 if isinstance(key, tuple):
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
877 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
878 elif isinstance(key, list):
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
879 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
880 else:
94cf5a8722d6 Add proper .get() support for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 441
diff changeset
881 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
882
439
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
883 def __contains__(self, 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
884 if isinstance(key, tuple):
439
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
885 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
886 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
887 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
888 else:
bd27da55483a Optimized __contains__() implementation for jailed and unjailed configurations
Franz Glasner <fzglas.hg@dom66.de>
parents: 438
diff changeset
889 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
890
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
891 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
892 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
893
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
894 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
895 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
896 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
897
398
b1f82b853290 Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 397
diff changeset
898 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
899 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
900 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
901 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
902 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
903 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
904 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
905 "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
906 else:
b1f82b853290 Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 397
diff changeset
907 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
908 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
909
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
910 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
911 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
912
398
b1f82b853290 Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 397
diff changeset
913 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
914 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
915 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
916 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
917 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
918 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
919 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
920 "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
921 else:
b1f82b853290 Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 397
diff changeset
922 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
923 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
924
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
925 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
926 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
927
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
928 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
929 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
930 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
931
398
b1f82b853290 Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 397
diff changeset
932 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
933 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
934 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
935
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
936 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
937 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
938
b1f82b853290 Jailed configuration: implement more methods: getfirstvarl, getfirstvarl_s, getfirstvar and getfirstvar_s
Franz Glasner <fzglas.hg@dom66.de>
parents: 397
diff changeset
939 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
940 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
941 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
942
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
943 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
944 """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
945 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
946
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
947 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
948
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
949 """
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
950 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
951 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
952 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
953 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
954 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
955 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
956 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
957 # 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
958 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
959 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
960 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
961 "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
962 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
963 rootpath = [
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
964 self._base.unquote(p) for p in varname.split(
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
965 self._base._HIER_SEPARATOR)
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
966 ]
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
967 else:
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
968 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
969 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
970 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
971 else:
83d537f1dfbb Implement sub-jails: allow to get a jailed configuration from a jail
Franz Glasner <fzglas.hg@dom66.de>
parents: 416
diff changeset
972 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
973 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
974 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
975 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
976 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
977
b96f49c9c76b Proper "repr()" for a jailed configuration: put the root path into the output
Franz Glasner <fzglas.hg@dom66.de>
parents: 428
diff changeset
978 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
979 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
980 return r