diff configmix/config.py @ 666:0eff8441c4b9 v0.21

Implement iteration support that yields properly jailed configurations for each container item
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 03 Jun 2022 02:17:12 +0200
parents 2b1c7a68f913
children f39b96e2bb2a
line wrap: on
line diff
--- a/configmix/config.py	Fri Jun 03 01:38:04 2022 +0200
+++ b/configmix/config.py	Fri Jun 03 02:17:12 2022 +0200
@@ -1099,6 +1099,14 @@
             jc.rebind(self)
         return jc
 
+    def iter_jailed(self, rootpath=None, root=None):
+        """Iterator that yields properly jailed configurations.
+
+        `rootpath` or `root` must refer to a `list` or `dict` container.
+
+        """
+        return self.jailed(rootpath=rootpath, root=root).iter_jailed()
+
 
 class _JailedConfiguration(CoercingMethodsMixin):
 
@@ -1282,6 +1290,27 @@
         """Length support for containers"""
         return len(self._base.getvarl_s(*self._path))
 
+    def iter_jailed(self):
+        """Iteration support for containers which yields properly jailed
+        sub-jails.
+
+        Only supported for type `list` or type `dict` jails.
+
+        """
+        container = self._base.getvarl(*self._path)
+        if isinstance(container, dict):
+            for k in container:
+                yield self.jailed(rootpath=(k, ))
+        elif isinstance(container, list):
+            len_container = len(container)
+            idx = 0
+            while idx < len_container:
+                yield self.jailed(rootpath=(idx, ))
+                idx += 1
+        else:
+            raise TypeError(
+                "jailed iterators only supported for lists and dicts")
+
     if PY2:
 
         def __nonzero__(self):