# HG changeset patch # User Franz Glasner # Date 1639555801 -3600 # Node ID d22985d6806e4b65270adac13afbe3d47e25d749 # Parent 9dc9cef1b9cd37b94f1875fa100d88be2e132f20 Proper boolean context for jailed configurations: __bool__()/__nonzero__() diff -r 9dc9cef1b9cd -r d22985d6806e configmix/config.py --- a/configmix/config.py Wed Dec 15 08:48:37 2021 +0100 +++ b/configmix/config.py Wed Dec 15 09:10:01 2021 +0100 @@ -28,7 +28,7 @@ from urlparse import urlsplit from .variables import lookup_varns, lookup_filter -from .compat import u, uchr, n +from .compat import u, uchr, n, PY2 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER @@ -943,6 +943,16 @@ def __iter__(self): return iter(self._base.getvarl_s(*self._path)) + if PY2: + + def __nonzero__(self): + return bool(self._base.getvarl_s(*self._path)) + + else: + + def __bool__(self): + return bool(self._base.getvarl_s(*self._path)) + def jailed(self, rootpath=None, root=None, bind_root=True): """Return a "jailed" configuration that effectively is a subjail of the current jail diff -r 9dc9cef1b9cd -r d22985d6806e tests/data/conf10.py --- a/tests/data/conf10.py Wed Dec 15 08:48:37 2021 +0100 +++ b/tests/data/conf10.py Wed Dec 15 09:10:01 2021 +0100 @@ -15,6 +15,7 @@ u"val1", u"val2", u"{{key1}}" - ] + ], + 'key9': {} } } diff -r 9dc9cef1b9cd -r d22985d6806e tests/test.py --- a/tests/test.py Wed Dec 15 08:48:37 2021 +0100 +++ b/tests/test.py Wed Dec 15 09:10:01 2021 +0100 @@ -1553,11 +1553,11 @@ jcfg = cfg.jailed(root=u"tree1") self.assertEqual( - set([u"key4", u"key5", u"key6", u"key7", u"key8"]), + set([u"key4", u"key5", u"key6", u"key7", u"key8", u"key9"]), set(jcfg.getkeysl(u"tree2"))) self.assertEqual( - set([u"key4", u"key5", u"key6", u"key7", u"key8"]), + set([u"key4", u"key5", u"key6", u"key7", u"key8", u"key9"]), set(jcfg.getkeys(u"tree2"))) self.assertEqual( @@ -1734,6 +1734,26 @@ s.sort() self.assertEqual([u"in the root namespace", u"val1", u"val2"], s) + def test_boolean_context_list_false(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py")) + jcfg = cfg.jailed(rootpath=(u"tree1", u"tree2", u"key7")) + self.assertFalse(jcfg) + + def test_boolean_context_list_true(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py")) + jcfg = cfg.jailed(rootpath=(u"tree1", u"tree2", u"key8")) + self.assertTrue(jcfg) + + def test_boolean_context_dict_false(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py")) + jcfg = cfg.jailed(rootpath=(u"tree1", u"tree2", u"key9")) + self.assertFalse(jcfg) + + def test_boolean_context_dict_true(self): + cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py")) + jcfg = cfg.jailed(rootpath=(u"tree1", u"tree2")) + self.assertTrue(jcfg) + if __name__ == "__main__": unittest.main()