diff configmix/config.py @ 417:83d537f1dfbb

Implement sub-jails: allow to get a jailed configuration from a jail
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 01 Dec 2021 23:10:20 +0100
parents 2abde0d3c735
children bb5f11abd12a
line wrap: on
line diff
--- a/configmix/config.py	Wed Dec 01 23:09:00 2021 +0100
+++ b/configmix/config.py	Wed Dec 01 23:10:20 2021 +0100
@@ -815,3 +815,38 @@
     def getfirstvar_s(self, *varnames, **kwds):
         real_varnames = [self._pathstr + vn for vn in varnames]
         return self._base.getfirstvar_s(*real_varnames, **kwds)
+
+    def jailed(self, rootpath=None, root=None, bind_root=True):
+        """Return a "jailed" configuration that effectively is a
+        subjail of the current jail
+
+        For a more complete description see :meth:`.Configuration.jailed`.
+
+        """
+        if rootpath is not None and root is not None:
+            raise ValueError("only one of `rootpath' or `root' can be given")
+        if rootpath is None and root is None:
+            raise ValueError("one of `rootpath' or `root' must be given")
+        if rootpath is not None and not isinstance(rootpath, (list, tuple)):
+            raise TypeError("`rootpath' must be a list or a tuple")
+        if root is not None:
+            # convert to path
+            varns, varname = self._base._split_ns(root)
+            if varns:
+                raise ValueError(
+                    "sub-jails do not support namespaces")
+            if varname:
+                rootpath = [
+                    self._base.unquote(p) for p in varname.split(
+                        self._base._HIER_SEPARATOR)
+                ]
+            else:
+                rootpath = tuple()
+        if self._path:
+            new_rootpath = self._path + tuple(rootpath)
+        else:
+            new_rootpath = rootpath
+        sjc = _JailedConfiguration(*new_rootpath)
+        if bind_root:
+            sjc.rebind(self._base)
+        return sjc