commit 5bb505d5d3ef6184fe3ce60ad6c7aae971404834
parent 9007967173ca14b0bdac6028771f26ae7136a620
Author: nolash <dev@holbrook.no>
Date:   Sun, 31 Oct 2021 08:23:14 +0100
Add tests for remove method
Diffstat:
7 files changed, 64 insertions(+), 3 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,5 +1,6 @@
 0.5.0
 	- License change from GPL to WTFPL
+	- Add remove method
 0.4.2
 	- Enable multiple config base and override for the dump tool
 0.4.1
diff --git a/MANIFEST.in b/MANIFEST.in
@@ -0,0 +1 @@
+include LICENSE WAIVER WARRANTY
diff --git a/WAIVER b/WAIVER
@@ -0,0 +1,17 @@
+# Copyright waiver for the python package "leveldir"
+
+I dedicate any and all copyright interest in this software to the
+public domain. I make this dedication for the benefit of the public at
+large and to the detriment of my heirs and successors. I intend this
+dedication to be an overt act of relinquishment in perpetuity of all
+present and future rights to this software under copyright law.
+
+To the best of my knowledge and belief, my contributions are either
+originally authored by me or are derived from prior works which I have
+verified are also in the public domain and are not subject to claims
+of copyright by other parties.
+
+To the best of my knowledge and belief, no individual, business,
+organization, government, or other entity has any copyright interest
+in my contributions, and I affirm that I will not make contributions
+that are otherwise encumbered.
diff --git a/WARRANTY b/WARRANTY
@@ -0,0 +1,5 @@
+This program is free software. It comes without any warranty, to
+     * the extent permitted by applicable law. You can redistribute it
+     * and/or modify it under the terms of the Do What The Fuck You Want
+     * To Public License, Version 2, as published by Sam Hocevar. See
+     * http://www.wtfpl.net/ for more details.
diff --git a/confini/config.py b/confini/config.py
@@ -243,9 +243,19 @@ class Config:
         return self._decrypt(k, v, self.src_dirs.get(k))
 
 
-    def remove(self, k):
-        logg.debug('removing key {}'.format(k))
-        del self.store[k]
+    def remove(self, k, strict=True):
+        removes = []
+        if strict:
+            removes = [k]
+        else:
+            l = len(k)
+            re_s = r'^' + k
+            for v in self.all():
+                if len(v) >= l and re.match(re_s, v):
+                    removes.append(v)
+        for v in removes:
+            del self.store[v]
+            logg.debug('removing key: {}'.format(v))
 
 
     def have(self, k):
diff --git a/tests/files/remove/foo.ini b/tests/files/remove/foo.ini
@@ -0,0 +1,4 @@
+[foo]
+bar = 1
+baz = 2
+xyzzy = 4
diff --git a/tests/test_basic.py b/tests/test_basic.py
@@ -84,6 +84,29 @@ class TestBasic(unittest.TestCase):
         logg.debug(c)
 
 
+    def test_remove_strict(self):
+        inidir = os.path.join(self.wd, 'files/default')
+        c = Config(inidir)
+        c.process()
+        c.remove('FOO_BAR')
+        with self.assertRaises(KeyError):
+            c.get('FOO_BAR')
+
+
+    def test_remove_wild(self):
+        inidir = os.path.join(self.wd, 'files/remove')
+        c = Config(inidir)
+        c.process()
+        c.remove('FOO', strict=False)
+        self.assertEqual(len(c.all()), 0)
+
+        c = Config(inidir)
+        c.process()
+        c.remove('FOO_BA', strict=False)
+        c.get('FOO_XYZZY') 
+        self.assertEqual(len(c.all()), 1)
+
+
     def test_all(self):
         inidir = os.path.join(self.wd, 'files')
         c = Config(inidir)