annotate configmix/config.py @ 54:aa8345dae995

Generate readable HTML documentation and an API documentation
author Franz Glasner <hg@dom66.de>
date Tue, 27 Feb 2018 23:42:11 +0100
parents 8d66aef2f7fb
children 1f11672c4615
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 -*-
54
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 40
diff changeset
2 r"""configmix.config
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 40
diff changeset
3 ^^^^^^^^^^^^^^^^
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 40
diff changeset
4
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 40
diff changeset
5 The unified configuration dictionary with attribute support or
aa8345dae995 Generate readable HTML documentation and an API documentation
Franz Glasner <hg@dom66.de>
parents: 40
diff changeset
6 variable substitution.
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
7
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 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
11
39
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
12 import warnings
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
13 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
14 from collections import OrderedDict as ConfigurationBase
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
15 except ImportError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
16 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
17 from ordereddict import OrderedDict as ConfigurationBase
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
18 except ImportError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
19 ConfigurationBase = dict
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
20
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
21 from .variables import lookup_varns, lookup_filter
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
22 from .compat import u
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
25 __all__ = ["Configuration"]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
26
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
27
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
28 _MARKER = object()
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
17
94b5e94fae44 Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 16
diff changeset
31 class _AttributeDict(ConfigurationBase):
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
32
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
33 def __getattr__(self, name):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
34 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
35 v = self[name]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
36 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
37 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
38 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
39 # 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
40 if isinstance(v, dict):
29
17af7a78710c FIX: Renaming a class was not really complete
Franz Glasner <hg@dom66.de>
parents: 25
diff changeset
41 return _AttributeDict(v)
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
42 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
43 return v
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
44
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
45
17
94b5e94fae44 Make the AttributeDict "private" (-> "_AttributeDict")
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 16
diff changeset
46 class Configuration(_AttributeDict):
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
47
18
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
48 """The configuration dictionary with attribute support or
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
49 variable substitution.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
50
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
51 .. note:: When retriving by attribute names variables will *not*
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
52 substituted.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
53
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
54 """
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 17
diff changeset
55
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
56 def getvar(self, varname, default=_MARKER):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
57 """Get a variable of the form ``[[ns1.]ns2.]name`` - including
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
58 variables from other namespaces.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
59
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
60 No variable expansion is done and no filters are applied.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
61 """
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
62 varns, varname = self._split_ns(varname)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
63 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
64 if not varns:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
65 lookupfn = self._lookupvar
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
66 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
67 lookupfn = lookup_varns(varns)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
68 varvalue = lookupfn(varname)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
69 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
70 if default is _MARKER:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
71 raise KeyError("Variable %r not found" % varname)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
72 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
73 return default
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
74 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
75 return varvalue
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
76
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
77 def getvar_s(self, varname, default=_MARKER):
24
fa65adab0b71 FIX: Typo in comment
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 23
diff changeset
78 """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
79
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
80 Variables will be substituted recursively in the result.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
81 """
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
82 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
83 obj = self.getvar(varname)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
84 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
85 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
86 if default is _MARKER:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
87 raise
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
88 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
89 return default
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
90
36
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
91 def getintvar_s(self, varname, default=_MARKER):
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
92 """Get a (possibly substituted) variable and coerce text to a
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
93 number.
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
94
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
95 """
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
96 s = self.getvar_s(varname, default)
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
97 if isinstance(s, self._TEXTTYPE):
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
98 return int(s, 0)
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
99 else:
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
100 return s
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
101
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
102 def getboolvar_s(self, varname, default=_MARKER):
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
103 """Get a (possibly substituted) variable and convert text to a
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
104 boolean
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
105
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
106 """
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
107 s = self.getvar_s(varname, default)
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
108 if isinstance(s, self._TEXTTYPE):
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
109 sl = s.strip().lower()
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
110 if not sl in self._BOOL_CVT:
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
111 raise ValueError("Not a boolean: %r" % s)
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
112 return self._BOOL_CVT[sl]
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
113 else:
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
114 return s
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
115
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
116 # Conversion of booleans
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
117 _BOOL_CVT = {'1': True, 'yes': True, 'true': True, 'on': True,
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
118 '0': False, 'no': False, 'false': False, 'off': False}
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
119
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
120 def getfloatvar_s(self, varname, default=_MARKER):
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
121 """Get a (possibly substituted) variable and convert text to a
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
122 float
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
123
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
124 """
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
125 s = self.getvar_s(varname, default)
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
126 if isinstance(s, self._TEXTTYPE):
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
127 return float(s)
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
128 else:
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
129 return s
b04a350f894b Implement methods to convert text to other types when getting configuration variables.
Franz Glasner <hg@dom66.de>
parents: 29
diff changeset
130
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
131 def _split_ns(self, s):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
132 nameparts = s.split(':', 1)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
133 if len(nameparts) == 1:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
134 return (None, s, )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
135 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
136 return (nameparts[0], nameparts[1], )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
137
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
138 def _split_filters(self, s):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
139 nameparts = s.split('|')
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
140 if len(nameparts) == 1:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
141 return (s, [], )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
142 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
143 return (nameparts[0].rstrip(), nameparts[1:], )
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
144
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
145 def _lookupvar(self, key, default=_MARKER):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
146 """Lookup a variable.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
147
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
148 If no default is given an unexisting `key` raises a `KeyError`
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
149 else `default` is returned.
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
150 """
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
151 parts = key.split('.')
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
152 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
153 v = self[parts[0]]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
154 for p in parts[1:]:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
155 v = v[p]
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
156 except KeyError:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
157 if default is _MARKER:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
158 raise KeyError("Configuration variable %r not found" % key)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
159 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
160 return default
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
161 return v
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
162
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
163 # Speed
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
164 _TEXTTYPE = type(u(""))
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
165
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
166 def substitute_variables_in_obj(self, obj):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
167 """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
168 if isinstance(obj, self._TEXTTYPE):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
169 # a string - really replace the value
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
170 return self.expand_variable(obj)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
171 elif isinstance(obj, list):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
172 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
173 elif isinstance(obj, tuple):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
174 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
175 return type(obj)(tmp)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
176 elif isinstance(obj, dict):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
177 newdict = type(obj)()
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
178 for k in obj:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
179 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
180 return newdict
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
181 elif isinstance(obj, set):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
182 newset = type(obj)()
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
183 for i in obj:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
184 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
185 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
186 return obj
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
187
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
188 # Speed
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
189 _STARTTOK = u(b"{{")
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
190 _ENDTOK = u(b"}}")
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
191
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
192 def expand_variable(self, s):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
193 """Expand variables in a single string"""
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
194 start = s.find(self._STARTTOK, 0)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
195 while start != -1:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
196 end = s.find(self._ENDTOK, start)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
197 if end < 0:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
198 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
199 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
200 try:
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
201 varvalue = self._apply_filters(filters, self.getvar_s(varname))
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
202 except KeyError:
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
203 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
204 "%r" % (varname, s, ),
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
205 UserWarning,
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
206 stacklevel=1)
8715e5cc59ac Print a warning if a variable cannot be expanded.
Franz Glasner <hg@dom66.de>
parents: 36
diff changeset
207 raise
25
baf862cb4860 Handle "None" variable values when substituting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 24
diff changeset
208 if varvalue is None:
baf862cb4860 Handle "None" variable values when substituting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 24
diff changeset
209 varvalue = u("")
23
c77cb6bc8eeb FIX: Handle non-str types in variable substitutions properly
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 18
diff changeset
210 replaced = u(b"{0}{1}").format(s[:start], varvalue)
c77cb6bc8eeb FIX: Handle non-str types in variable substitutions properly
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 18
diff changeset
211 s = u(b"{0}{1}").format(replaced, s[end+2:])
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
212 # 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
213 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
214 return s
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
215
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
216 def _apply_filters(self, filters, value):
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
217 for name in filters:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
218 try:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
219 filterfn = lookup_filter(name)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
220 except KeyError:
40
8d66aef2f7fb Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents: 39
diff changeset
221 #
8d66aef2f7fb Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents: 39
diff changeset
222 # 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
223 # a very serious error.
8d66aef2f7fb Comment about exceptions when filters are missing.
Franz Glasner <hg@dom66.de>
parents: 39
diff changeset
224 #
16
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
225 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
226 else:
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
227 value = filterfn(self, value)
f85dc4677c01 Implemented the real configuration dictionary with attribute access or
Franz Glasner <hg@dom66.de>
parents:
diff changeset
228 return value