Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 382:24db29162d09
Renamed "names" arguments into the more proper "path"
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 10 Nov 2021 01:42:15 +0100 |
| parents | fe3dfd687621 |
| children | 5c72da46b8ae |
comparison
equal
deleted
inserted
replaced
| 381:fe3dfd687621 | 382:24db29162d09 |
|---|---|
| 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 _COMMENT = u(b'#') | 74 _COMMENT = u(b'#') |
| 75 | 75 |
| 76 def getvarl(self, *names, **kwds): | 76 def getvarl(self, *path, **kwds): |
| 77 """Get a variable where the hierarchy is given in `names` as sequence | 77 """Get a variable where the hierarchy is given in `path` as sequence |
| 78 and the namespace is given in the `namespace` keyword argument. | 78 and the namespace is given in the `namespace` keyword argument. |
| 79 | 79 |
| 80 No variable interpolation is done and no filters are applied. | 80 No variable interpolation is done and no filters are applied. |
| 81 | 81 |
| 82 Quoting of `names` and `namespace` is *not* needed and wrong. | 82 Quoting of `path` and `namespace` is *not* needed and wrong. |
| 83 | 83 |
| 84 """ | 84 """ |
| 85 default = kwds.pop("default", _MARKER) | 85 default = kwds.pop("default", _MARKER) |
| 86 namespace = kwds.pop("namespace", None) | 86 namespace = kwds.pop("namespace", None) |
| 87 try: | 87 try: |
| 90 else: | 90 else: |
| 91 if namespace == REF_NAMESPACE: | 91 if namespace == REF_NAMESPACE: |
| 92 lookupfn = self._lookupref | 92 lookupfn = self._lookupref |
| 93 else: | 93 else: |
| 94 lookupfn = lookup_varns(namespace) | 94 lookupfn = lookup_varns(namespace) |
| 95 varvalue = lookupfn(*names) | 95 varvalue = lookupfn(*path) |
| 96 except KeyError: | 96 except KeyError: |
| 97 if default is _MARKER: | 97 if default is _MARKER: |
| 98 raise KeyError("Variable %r not found" % (names,)) | 98 raise KeyError("Variable %r not found" % (path,)) |
| 99 else: | 99 else: |
| 100 return default | 100 return default |
| 101 else: | 101 else: |
| 102 return varvalue | 102 return varvalue |
| 103 | 103 |
| 169 raise KeyError( | 169 raise KeyError( |
| 170 "none of the given variables found: %r" % (varnames,)) | 170 "none of the given variables found: %r" % (varnames,)) |
| 171 else: | 171 else: |
| 172 return default | 172 return default |
| 173 | 173 |
| 174 def getvarl_s(self, *names, **kwds): | 174 def getvarl_s(self, *path, **kwds): |
| 175 """Get a variable - including variables from other namespaces. | 175 """Get a variable - including variables from other namespaces. |
| 176 | 176 |
| 177 `names` and `namespace` are interpreted as in | 177 `path` and `namespace` are interpreted as in |
| 178 :meth:`.getvarl`. But variables will be interpolated | 178 :meth:`.getvarl`. But variables will be interpolated |
| 179 recursively within the variable values and filters are | 179 recursively within the variable values and filters are |
| 180 applied. | 180 applied. |
| 181 | 181 |
| 182 For more details see chapter :ref:`variable-interpolation`. | 182 For more details see chapter :ref:`variable-interpolation`. |
| 183 | 183 |
| 184 """ | 184 """ |
| 185 default = kwds.pop("default", _MARKER) | 185 default = kwds.pop("default", _MARKER) |
| 186 namespace = kwds.pop("namespace", None) | 186 namespace = kwds.pop("namespace", None) |
| 187 try: | 187 try: |
| 188 obj = self.getvarl(*names, namespace=namespace) | 188 obj = self.getvarl(*path, namespace=namespace) |
| 189 return self.substitute_variables_in_obj(obj) | 189 return self.substitute_variables_in_obj(obj) |
| 190 except KeyError: | 190 except KeyError: |
| 191 if default is _MARKER: | 191 if default is _MARKER: |
| 192 raise | 192 raise |
| 193 else: | 193 else: |
| 262 raise KeyError( | 262 raise KeyError( |
| 263 "none of the given variables found: %r" % (varnames,)) | 263 "none of the given variables found: %r" % (varnames,)) |
| 264 else: | 264 else: |
| 265 return default | 265 return default |
| 266 | 266 |
| 267 def getintvarl_s(self, *names, **kwds): | 267 def getintvarl_s(self, *path, **kwds): |
| 268 """Get a (possibly substituted) variable and coerce text to a | 268 """Get a (possibly substituted) variable and coerce text to a |
| 269 number. | 269 number. |
| 270 | 270 |
| 271 """ | 271 """ |
| 272 s = self.getvarl_s(*names, **kwds) | 272 s = self.getvarl_s(*path, **kwds) |
| 273 if isinstance(s, self._TEXTTYPE): | 273 if isinstance(s, self._TEXTTYPE): |
| 274 return int(s, 0) | 274 return int(s, 0) |
| 275 else: | 275 else: |
| 276 return s | 276 return s |
| 277 | 277 |
| 295 if isinstance(s, self._TEXTTYPE): | 295 if isinstance(s, self._TEXTTYPE): |
| 296 return int(s, 0) | 296 return int(s, 0) |
| 297 else: | 297 else: |
| 298 return s | 298 return s |
| 299 | 299 |
| 300 def getboolvarl_s(self, *names, **kwds): | 300 def getboolvarl_s(self, *path, **kwds): |
| 301 """Get a (possibly substituted) variable and convert text to a | 301 """Get a (possibly substituted) variable and convert text to a |
| 302 boolean | 302 boolean |
| 303 | 303 |
| 304 """ | 304 """ |
| 305 s = self.getvarl_s(*names, **kwds) | 305 s = self.getvarl_s(*path, **kwds) |
| 306 if isinstance(s, self._TEXTTYPE): | 306 if isinstance(s, self._TEXTTYPE): |
| 307 sl = s.strip().lower() | 307 sl = s.strip().lower() |
| 308 if sl not in self._BOOL_CVT: | 308 if sl not in self._BOOL_CVT: |
| 309 raise ValueError("Not a boolean: %r" % s) | 309 raise ValueError("Not a boolean: %r" % s) |
| 310 return self._BOOL_CVT[sl] | 310 return self._BOOL_CVT[sl] |
| 343 _BOOL_CVT = { | 343 _BOOL_CVT = { |
| 344 u('1'): True, u('yes'): True, u('true'): True, u('on'): True, | 344 u('1'): True, u('yes'): True, u('true'): True, u('on'): True, |
| 345 u('0'): False, u('no'): False, u('false'): False, u('off'): False | 345 u('0'): False, u('no'): False, u('false'): False, u('off'): False |
| 346 } | 346 } |
| 347 | 347 |
| 348 def getfloatvarl_s(self, *names, **kwds): | 348 def getfloatvarl_s(self, *path, **kwds): |
| 349 """Get a (possibly substituted) variable and convert text to a | 349 """Get a (possibly substituted) variable and convert text to a |
| 350 float | 350 float |
| 351 | 351 |
| 352 """ | 352 """ |
| 353 s = self.getvarl_s(*names, **kwds) | 353 s = self.getvarl_s(*path, **kwds) |
| 354 if isinstance(s, self._TEXTTYPE): | 354 if isinstance(s, self._TEXTTYPE): |
| 355 return float(s) | 355 return float(s) |
| 356 else: | 356 else: |
| 357 return s | 357 return s |
| 358 | 358 |
| 379 if len(nameparts) == 1: | 379 if len(nameparts) == 1: |
| 380 return (s, [], ) | 380 return (s, [], ) |
| 381 else: | 381 else: |
| 382 return (nameparts[0].rstrip(), nameparts[1:], ) | 382 return (nameparts[0].rstrip(), nameparts[1:], ) |
| 383 | 383 |
| 384 def _lookupvar(self, *names, **kwds): | 384 def _lookupvar(self, *path, **kwds): |
| 385 """Lookup a variable within a hierarchy. | 385 """Lookup a variable within a hierarchy. |
| 386 | 386 |
| 387 If no default is given an unexisting `name` raises a `KeyError` | 387 If no default is given an unexisting `path` raises a `KeyError` |
| 388 else `default` is returned. | 388 else `default` is returned. |
| 389 """ | 389 """ |
| 390 default = kwds.pop("default", _MARKER) | 390 default = kwds.pop("default", _MARKER) |
| 391 try: | 391 try: |
| 392 v = self.expand_if_reference(self[names[0]]) | 392 v = self.expand_if_reference(self[path[0]]) |
| 393 for p in names[1:]: | 393 for p in path[1:]: |
| 394 v = self.expand_if_reference(v[p]) | 394 v = self.expand_if_reference(v[p]) |
| 395 except TypeError: | 395 except TypeError: |
| 396 raise KeyError( | 396 raise KeyError( |
| 397 "Configuration variable %r not found" | 397 "Configuration variable %r not found" |
| 398 "(missing intermediate keys?)" % (names,)) | 398 "(missing intermediate keys?)" % (path,)) |
| 399 except KeyError: | 399 except KeyError: |
| 400 if default is _MARKER: | 400 if default is _MARKER: |
| 401 raise KeyError( | 401 raise KeyError( |
| 402 "Configuration variable %r not found" % (names,)) | 402 "Configuration variable %r not found" % (path,)) |
| 403 else: | 403 else: |
| 404 return default | 404 return default |
| 405 return v | 405 return v |
| 406 | 406 |
| 407 def _lookupref(self, key, default=_MARKER): | 407 def _lookupref(self, key, default=_MARKER): |
