Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 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 | 1941f0188e81 |
| children | 0c65aac81807 |
comparison
equal
deleted
inserted
replaced
| 367:a72e2f36983a | 368:4ee53f6fcac1 |
|---|---|
| 118 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)] | 118 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)] |
| 119 else: | 119 else: |
| 120 varnameparts = (varname,) | 120 varnameparts = (varname,) |
| 121 return self.getvarl(*varnameparts, namespace=varns, default=default) | 121 return self.getvarl(*varnameparts, namespace=varns, default=default) |
| 122 | 122 |
| 123 def getfirstvar(self, *varnames, **kwds): | |
| 124 """A variant of :meth:`~.getvar` that returns the first found variable | |
| 125 in the list of given variables in `varnames`. | |
| 126 | |
| 127 """ | |
| 128 default = kwds.pop("default", _MARKER) | |
| 129 for varname in varnames: | |
| 130 try: | |
| 131 varvalue = self.getvar(varname) | |
| 132 except KeyError: | |
| 133 pass | |
| 134 else: | |
| 135 return varvalue | |
| 136 if default is _MARKER: | |
| 137 raise KeyError( | |
| 138 "none of the given variables found: %r" % (varnames,)) | |
| 139 else: | |
| 140 return default | |
| 141 | |
| 123 def getvarl_s(self, *names, **kwds): | 142 def getvarl_s(self, *names, **kwds): |
| 124 """Get a variable - including variables from other namespaces. | 143 """Get a variable - including variables from other namespaces. |
| 125 | 144 |
| 126 `names` and `namespace` are interpreted as in | 145 `names` and `namespace` are interpreted as in |
| 127 :meth:`.getvarl`. But variables will be interpolated | 146 :meth:`.getvarl`. But variables will be interpolated |
| 158 except KeyError: | 177 except KeyError: |
| 159 if default is _MARKER: | 178 if default is _MARKER: |
| 160 raise | 179 raise |
| 161 else: | 180 else: |
| 162 return default | 181 return default |
| 182 | |
| 183 def getfirstvar_s(self, *varnames, **kwds): | |
| 184 """A variant of :meth:`~.getvar_s` that returns the first found | |
| 185 variable in the list of given variables in `varnames`. | |
| 186 | |
| 187 """ | |
| 188 default = kwds.pop("default", _MARKER) | |
| 189 for varname in varnames: | |
| 190 try: | |
| 191 obj = self.getvar(varname) | |
| 192 except KeyError: | |
| 193 pass | |
| 194 else: | |
| 195 return self.substitute_variables_in_obj(obj) | |
| 196 if default is _MARKER: | |
| 197 raise KeyError( | |
| 198 "none of the given variables found: %r" % (varnames,)) | |
| 199 else: | |
| 200 return default | |
| 163 | 201 |
| 164 def getintvarl_s(self, *names, **kwds): | 202 def getintvarl_s(self, *names, **kwds): |
| 165 """Get a (possibly substituted) variable and coerce text to a | 203 """Get a (possibly substituted) variable and coerce text to a |
| 166 number. | 204 number. |
| 167 | 205 |
