diff configmix/config.py @ 381:fe3dfd687621

Implemented Configuration.getfirstvarl() and Configuration.getfirstvarl_s()
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 10 Nov 2021 01:37:36 +0100
parents bb4a90fb58e0
children 24db29162d09
line wrap: on
line diff
--- a/configmix/config.py	Tue Nov 09 21:58:05 2021 +0100
+++ b/configmix/config.py	Wed Nov 10 01:37:36 2021 +0100
@@ -101,6 +101,38 @@
         else:
             return varvalue
 
+    def getfirstvarl(self, *paths, **kwds):
+        default = kwds.pop("default", _MARKER)
+        for path in paths:
+            if isinstance(path, (list, tuple)):
+                try:
+                    varvalue = self.getvarl(*path)
+                except KeyError:
+                    pass
+                else:
+                    return varvalue
+            elif isinstance(path, dict):
+                try:
+                    namespace = path["namespace"]
+                    p = path["path"]
+                except KeyError:
+                    raise TypeError("a paths dict item must have a `path'"
+                                    " and a `namespace' key")
+                else:
+                    try:
+                        varvalue = self.getvarl(*p, namespace=namespace)
+                    except KeyError:
+                        pass
+                    else:
+                        return varvalue
+            else:
+                raise TypeError("a paths item must be a dict, list or tuple")
+        if default is _MARKER:
+            raise KeyError(
+                "none of the given variables found: %r" % (paths,))
+        else:
+            return default
+
     def getvar(self, varname, default=_MARKER):
         """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including
         variables from other namespaces.
@@ -161,6 +193,39 @@
             else:
                 return default
 
+    def getfirstvarl_s(self, *paths, **kwds):
+        default = kwds.pop("default", _MARKER)
+        for path in paths:
+            if isinstance(path, (list, tuple)):
+                try:
+                    obj = self.getvarl(*path)
+                except KeyError:
+                    pass
+                else:
+                    return self.substitute_variables_in_obj(obj)
+
+            elif isinstance(path, dict):
+                try:
+                    namespace = path["namespace"]
+                    p = path["path"]
+                except KeyError:
+                    raise TypeError("a paths dict item must have a `path'"
+                                    " and a `namespace' key")
+                else:
+                    try:
+                        obj = self.getvarl(*p, namespace=namespace)
+                    except KeyError:
+                        pass
+                    else:
+                        return self.substitute_variables_in_obj(obj)
+            else:
+                raise TypeError("a paths item must be a dict, list or tuple")
+        if default is _MARKER:
+            raise KeyError(
+                "none of the given variables found: %r" % (paths,))
+        else:
+            return default
+
     def getvar_s(self, varname, default=_MARKER):
         """Get a variable - including variables from other namespaces.