changeset 756:cabd1046d95f

Extract new config methods extract_new_config
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 04 Dec 2023 09:20:30 +0100
parents 04c5a3a36499
children 5178794bb208
files CHANGES.txt configmix/config.py tests/test.py
diffstat 3 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.txt	Thu Nov 30 21:11:47 2023 +0100
+++ b/CHANGES.txt	Mon Dec 04 09:20:30 2023 +0100
@@ -15,6 +15,11 @@
 n/a (n/a)
 ~~~~~~~~~
 
+- **[feature]**
+
+  Implement new method
+  :py:meth:`~configmix.config.Configuration.extract_new_config`.
+
 - **[test]**
   Test fixes for the tests on Windows
 
--- a/configmix/config.py	Thu Nov 30 21:11:47 2023 +0100
+++ b/configmix/config.py	Mon Dec 04 09:20:30 2023 +0100
@@ -22,6 +22,7 @@
         from ordereddict import OrderedDict as ConfigurationBase
     except ImportError:
         ConfigurationBase = dict
+import copy
 try:
     from urllib.parse import urlsplit
 except ImportError:
@@ -1180,6 +1181,15 @@
         """
         return self.jailed(rootpath=rootpath, root=root).iter_jailed()
 
+    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.
+
+        """
+        return self.__class__(copy.deepcopy(self.getvarl(*path, **kwds)))
+
 
 class _JailedConfiguration(CoercingMethodsMixin):
 
--- a/tests/test.py	Thu Nov 30 21:11:47 2023 +0100
+++ b/tests/test.py	Mon Dec 04 09:20:30 2023 +0100
@@ -2606,5 +2606,35 @@
                 b":")
 
 
+class T11NewConfig(unittest.TestCase):
+
+    def setUp(self):
+        self._cfg = configmix.load(
+            os.path.join(TESTDATADIR, "extract-new-config.yml"))
+
+    def tearDown(self):
+        self._cfg = None
+
+    def test_extract_new_copy(self):
+        nc = self._cfg.extract_new_config("globals")
+        self.assertEqual(u(r"E:\APPS64\PostgreSQL-9.4\all\bin"),
+                         nc.getvarl_s(u"PATH"))
+        self.assertEqual(
+            u("postgresql+psycopg2://My%20Username:My%20Password@localhost:5432/postgres"),
+            nc.getvarl_s("engine0", "url"))
+        self.assertIsInstance(self._cfg.getvarl("parts"), list)
+        with self.assertRaises(KeyError):
+            nc.getvarl("parts")
+
+    def test_extract_new_copy_is_deepcopy(self):
+        nc = self._cfg.extract_new_config("globals")
+        user2 = nc.getvarl(u("user2"))
+        user2[u("pwd")] = u("new passphrase")
+        self.assertEqual(u("new passphrase"),
+                         nc.getvarl(u("user2"), u("pwd")))
+        self.assertEqual(u("abcQQQ123456"),
+                         self._cfg.getvarl(u("globals"), u("user2"), u("pwd")))
+
+
 if __name__ == "__main__":
     unittest.main()