Mercurial > hgrepos > Python > libs > ConfigMix
changeset 368:4ee53f6fcac1
Implement ".getfirstvar()" and ".getfirstvar_s()".
BUGS: Not tested yet and no documentation yet.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 09 Jul 2021 09:40:49 +0200 |
| parents | a72e2f36983a |
| children | 4ff02a4f401a |
| files | configmix/config.py |
| diffstat | 1 files changed, 38 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/configmix/config.py Fri Jul 09 08:58:57 2021 +0200 +++ b/configmix/config.py Fri Jul 09 09:40:49 2021 +0200 @@ -120,6 +120,25 @@ varnameparts = (varname,) return self.getvarl(*varnameparts, namespace=varns, default=default) + def getfirstvar(self, *varnames, **kwds): + """A variant of :meth:`~.getvar` that returns the first found variable + in the list of given variables in `varnames`. + + """ + default = kwds.pop("default", _MARKER) + for varname in varnames: + try: + varvalue = self.getvar(varname) + except KeyError: + pass + else: + return varvalue + if default is _MARKER: + raise KeyError( + "none of the given variables found: %r" % (varnames,)) + else: + return default + def getvarl_s(self, *names, **kwds): """Get a variable - including variables from other namespaces. @@ -161,6 +180,25 @@ else: return default + def getfirstvar_s(self, *varnames, **kwds): + """A variant of :meth:`~.getvar_s` that returns the first found + variable in the list of given variables in `varnames`. + + """ + default = kwds.pop("default", _MARKER) + for varname in varnames: + try: + obj = self.getvar(varname) + except KeyError: + pass + else: + return self.substitute_variables_in_obj(obj) + if default is _MARKER: + raise KeyError( + "none of the given variables found: %r" % (varnames,)) + else: + return default + def getintvarl_s(self, *names, **kwds): """Get a (possibly substituted) variable and coerce text to a number.
