comparison configmix/config.py @ 418:bb5f11abd12a

Implement .getkeysl() and .getkeys() for configuration objects that iterate over all the keys of a configuration value
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 02 Dec 2021 01:24:53 +0100
parents 83d537f1dfbb
children 079a82129110
comparison
equal deleted inserted replaced
417:83d537f1dfbb 418:bb5f11abd12a
263 raise KeyError("Variable %r not found" % (path,)) 263 raise KeyError("Variable %r not found" % (path,))
264 else: 264 else:
265 return default 265 return default
266 else: 266 else:
267 return varvalue 267 return varvalue
268
269 def getkeysl(self, *path, **kwds):
270 """Yield the keys of a variable value.
271
272 :rtype: A generator
273 :raise KeyError:
274
275 .. note:: Dictionary keys are not subject to interpolation.
276
277 """
278 if "default" in kwds:
279 raise TypeError("got unexpected keyword argument: default")
280 for k in self.getvarl(*path, **kwds).keys():
281 yield k
268 282
269 def getfirstvarl(self, *paths, **kwds): 283 def getfirstvarl(self, *paths, **kwds):
270 """A variant of :meth:`~.getvarl` that returns the first found 284 """A variant of :meth:`~.getvarl` that returns the first found
271 variable in the `paths` list. 285 variable in the `paths` list.
272 286
333 self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR) 347 self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)
334 ] 348 ]
335 else: 349 else:
336 varnameparts = (varname,) 350 varnameparts = (varname,)
337 return self.getvarl(*varnameparts, namespace=varns, default=default) 351 return self.getvarl(*varnameparts, namespace=varns, default=default)
352
353 def getkeys(self, varname):
354 """Yield all the keys of a variable value.
355
356 :rtype: A generator
357 :raise KeyError:
358
359 .. note:: Dictionary keys are not subject to interpolation.
360
361 """
362 for k in self.getvar(varname).keys():
363 yield k
338 364
339 def getfirstvar(self, *varnames, **kwds): 365 def getfirstvar(self, *varnames, **kwds):
340 """A variant of :meth:`~.getvar` that returns the first found variable 366 """A variant of :meth:`~.getvar` that returns the first found variable
341 in the list of given variables in `varnames`. 367 in the list of given variables in `varnames`.
342 368
773 new_base.getvarl(*self._path) 799 new_base.getvarl(*self._path)
774 800
775 def getvarl(self, *path, **kwds): 801 def getvarl(self, *path, **kwds):
776 return self._base.getvarl(*(self._path + path), **kwds) 802 return self._base.getvarl(*(self._path + path), **kwds)
777 803
804 def getkeysl(self, *path, **kwds):
805 for k in self._base.getkeysl(*(self._path + path), **kwds):
806 yield k
807
778 def getfirstvarl(self, *paths, **kwds): 808 def getfirstvarl(self, *paths, **kwds):
779 real_paths = [] 809 real_paths = []
780 for path in paths: 810 for path in paths:
781 if isinstance(path, (list, tuple)): 811 if isinstance(path, (list, tuple)):
782 real_paths.append(self._path + path) 812 real_paths.append(self._path + path)
802 raise TypeError("a paths item must be a list or tuple") 832 raise TypeError("a paths item must be a list or tuple")
803 return self._base.getfirstvarl_s(*real_paths, **kwds) 833 return self._base.getfirstvarl_s(*real_paths, **kwds)
804 834
805 def getvar(self, varname, default=_MARKER): 835 def getvar(self, varname, default=_MARKER):
806 return self._base.getvar(self._pathstr + varname, default=default) 836 return self._base.getvar(self._pathstr + varname, default=default)
837
838 def getkeys(self, varname):
839 for k in self._base.getkeys(self._pathstr + varname):
840 yield k
807 841
808 def getfirstvar(self, *varnames, **kwds): 842 def getfirstvar(self, *varnames, **kwds):
809 real_varnames = [self._pathstr + vn for vn in varnames] 843 real_varnames = [self._pathstr + vn for vn in varnames]
810 return self._base.getfirstvar(*real_varnames, **kwds) 844 return self._base.getfirstvar(*real_varnames, **kwds)
811 845