diff configmix/config.py @ 762:3eb2c451026b

Implement Configuration.copy_new_config_without()
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 07 Dec 2023 08:43:04 +0100
parents cabd1046d95f
children
line wrap: on
line diff
--- a/configmix/config.py	Thu Dec 07 08:42:33 2023 +0100
+++ b/configmix/config.py	Thu Dec 07 08:43:04 2023 +0100
@@ -1184,12 +1184,36 @@
     def extract_new_config(self, *path, **kwds):
         """Get the value at `path` and make a new :class:`~.Configuration` from it.
 
-        The new config is a deepcopy and completely independent of the source
-        configuration.
+        The new configuration is a deepcopy and completely independent
+        of the source configuration.
 
         """
         return self.__class__(copy.deepcopy(self.getvarl(*path, **kwds)))
 
+    def copy_new_config_without(self, *path):
+        """Copy the current configuration but leave out the value at key
+        `path`.
+
+        The new configuration is a deepcopy and completely independent
+        of the source configuration.
+
+        .. note:: Currently only a "simple" `path` with length 1 is
+                  supported. References are not supported.
+
+        """
+        #
+        # Manual copy: avoid direct and indirect calls to self.__getitem__
+        # because it interpolates.
+        #
+        if path:
+            if len(path) != 1:
+                raise ValueError("Only a `path' with length 1 is supported")
+        nc = {}
+        for i in self:
+            if (not path) or (i != path[0]):
+                nc[i] = copy.deepcopy(self.getvarl(i))
+        return self.__class__(nc)
+
 
 class _JailedConfiguration(CoercingMethodsMixin):