Mercurial > hgrepos > Python > libs > ConfigMix
changeset 460:d22985d6806e
Proper boolean context for jailed configurations: __bool__()/__nonzero__()
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 15 Dec 2021 09:10:01 +0100 |
| parents | 9dc9cef1b9cd |
| children | 5de1a6f213a5 |
| files | configmix/config.py tests/data/conf10.py tests/test.py |
| diffstat | 3 files changed, 35 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- 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': {} } }
--- 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()
