comparison configmix/config.py @ 321:7a0f3c256cf4

FIX: Python2 compatibility: keyword arguments after *args not allowed: use **kwds and manual retrieval with .pop() instead
author Franz Glasner <f.glasner@feldmann-mg.com>
date Thu, 06 May 2021 11:06:50 +0200
parents 98490375d90c
children 4aa19e04ff65
comparison
equal deleted inserted replaced
320:98490375d90c 321:7a0f3c256cf4
70 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR 70 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR
71 _ENDTOK_REF = _ENDTOK 71 _ENDTOK_REF = _ENDTOK
72 _DOT = u(b'.') 72 _DOT = u(b'.')
73 _QUOTE = u(b'%') 73 _QUOTE = u(b'%')
74 74
75 def getvarl(self, *names, default=_MARKER, namespace=None): 75 def getvarl(self, *names, **kwds):
76 """Get a variable where the hierarchy is given in `names` as sequence 76 """Get a variable where the hierarchy is given in `names` as sequence
77 and the namespace is given in `namespace`. 77 and the namespace is given in `namespace`.
78 78
79 No variable interpolation is done and no filters are applied. 79 No variable interpolation is done and no filters are applied.
80 80
81 """ 81 """
82 default = kwds.pop("default", _MARKER)
83 namespace = kwds.pop("namespace", None)
82 try: 84 try:
83 if not namespace: 85 if not namespace:
84 lookupfn = self._lookupvar 86 lookupfn = self._lookupvar
85 else: 87 else:
86 if namespace == REF_NAMESPACE: 88 if namespace == REF_NAMESPACE:
108 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)] 110 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)]
109 else: 111 else:
110 varnameparts = (varname,) 112 varnameparts = (varname,)
111 return self.getvarl(*varnameparts, namespace=varns) 113 return self.getvarl(*varnameparts, namespace=varns)
112 114
113 def getvarl_s(self, *names, default=_MARKER, namespace=None): 115 def getvarl_s(self, *names, **kwds):
114 """Get a variable - including variables from other namespaces. 116 """Get a variable - including variables from other namespaces.
115 117
116 `names` and `namespace` are interpreted as in 118 `names` and `namespace` are interpreted as in
117 :meth:`.getvarl`. But variables will be interpolated 119 :meth:`.getvarl`. But variables will be interpolated
118 recursively within the variable values and filters are 120 recursively within the variable values and filters are
119 applied. 121 applied.
120 122
121 For more details see chapter :ref:`variable-interpolation`. 123 For more details see chapter :ref:`variable-interpolation`.
122 124
123 """ 125 """
126 default = kwds.pop("default", _MARKER)
127 namespace = kwds.pop("namespace", None)
124 try: 128 try:
125 obj = self.getvarl(*names, namespace=namespace) 129 obj = self.getvarl(*names, namespace=namespace)
126 return self.substitute_variables_in_obj(obj) 130 return self.substitute_variables_in_obj(obj)
127 except KeyError: 131 except KeyError:
128 if default is _MARKER: 132 if default is _MARKER:
147 if default is _MARKER: 151 if default is _MARKER:
148 raise 152 raise
149 else: 153 else:
150 return default 154 return default
151 155
152 def getintvarl_s(self, *names, default=_MARKER, namespace=None): 156 def getintvarl_s(self, *names, **kwds):
153 """Get a (possibly substituted) variable and coerce text to a 157 """Get a (possibly substituted) variable and coerce text to a
154 number. 158 number.
155 159
156 """ 160 """
157 s = self.getvarl_s(*names, default=default, namespace=namespace) 161 s = self.getvarl_s(*names, **kwds)
158 if isinstance(s, self._TEXTTYPE): 162 if isinstance(s, self._TEXTTYPE):
159 return int(s, 0) 163 return int(s, 0)
160 else: 164 else:
161 return s 165 return s
162 166
169 if isinstance(s, self._TEXTTYPE): 173 if isinstance(s, self._TEXTTYPE):
170 return int(s, 0) 174 return int(s, 0)
171 else: 175 else:
172 return s 176 return s
173 177
174 def getboolvarl_s(self, *names, default=_MARKER, namespace=None): 178 def getboolvarl_s(self, *names, **kwds):
175 """Get a (possibly substituted) variable and convert text to a 179 """Get a (possibly substituted) variable and convert text to a
176 boolean 180 boolean
177 181
178 """ 182 """
179 s = self.getvarl_s(*names, default=default, namespace=namespace) 183 s = self.getvarl_s(*names, **kwds)
180 if isinstance(s, self._TEXTTYPE): 184 if isinstance(s, self._TEXTTYPE):
181 sl = s.strip().lower() 185 sl = s.strip().lower()
182 if sl not in self._BOOL_CVT: 186 if sl not in self._BOOL_CVT:
183 raise ValueError("Not a boolean: %r" % s) 187 raise ValueError("Not a boolean: %r" % s)
184 return self._BOOL_CVT[sl] 188 return self._BOOL_CVT[sl]
203 _BOOL_CVT = { 207 _BOOL_CVT = {
204 u('1'): True, u('yes'): True, u('true'): True, u('on'): True, 208 u('1'): True, u('yes'): True, u('true'): True, u('on'): True,
205 u('0'): False, u('no'): False, u('false'): False, u('off'): False 209 u('0'): False, u('no'): False, u('false'): False, u('off'): False
206 } 210 }
207 211
208 def getfloatvarl_s(self, *names, default=_MARKER, namespace=None): 212 def getfloatvarl_s(self, *names, **kwds):
209 """Get a (possibly substituted) variable and convert text to a 213 """Get a (possibly substituted) variable and convert text to a
210 float 214 float
211 215
212 """ 216 """
213 s = self.getvarl_s(*names, default=default, namespace=namespace) 217 s = self.getvarl_s(*names, **kwds)
214 if isinstance(s, self._TEXTTYPE): 218 if isinstance(s, self._TEXTTYPE):
215 return float(s) 219 return float(s)
216 else: 220 else:
217 return s 221 return s
218 222
239 if len(nameparts) == 1: 243 if len(nameparts) == 1:
240 return (s, [], ) 244 return (s, [], )
241 else: 245 else:
242 return (nameparts[0].rstrip(), nameparts[1:], ) 246 return (nameparts[0].rstrip(), nameparts[1:], )
243 247
244 def _lookupvar(self, *names, default=_MARKER): 248 def _lookupvar(self, *names, **kwds):
245 """Lookup a variable within a hierarchy. 249 """Lookup a variable within a hierarchy.
246 250
247 If no default is given an unexisting `name` raises a `KeyError` 251 If no default is given an unexisting `name` raises a `KeyError`
248 else `default` is returned. 252 else `default` is returned.
249 """ 253 """
254 default = kwds.pop("default", _MARKER)
250 try: 255 try:
251 v = self.expand_if_reference(self[names[0]]) 256 v = self.expand_if_reference(self[names[0]])
252 for p in names[1:]: 257 for p in names[1:]:
253 v = self.expand_if_reference(v[p]) 258 v = self.expand_if_reference(v[p])
254 except TypeError: 259 except TypeError: