# HG changeset patch # User Franz Glasner # Date 1626016086 -7200 # Node ID 0c65aac81807798f5511f63b34320c21bef4457c # Parent ac3e3cd6faae32d5917cb53cba1d0a28325f7f1d Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests diff -r ac3e3cd6faae -r 0c65aac81807 configmix/config.py --- a/configmix/config.py Sun Jul 11 16:43:14 2021 +0200 +++ b/configmix/config.py Sun Jul 11 17:08:06 2021 +0200 @@ -221,6 +221,17 @@ else: return s + def getfirstintvar_s(self, *varnames, **kwds): + """Get a (possibly substituted) variable and coerce text to a + number. + + """ + s = self.getfirstvar_s(*varnames, **kwds) + if isinstance(s, self._TEXTTYPE): + return int(s, 0) + else: + return s + def getboolvarl_s(self, *names, **kwds): """Get a (possibly substituted) variable and convert text to a boolean @@ -249,6 +260,20 @@ else: return s + def getfirstboolvar_s(self, *varnames, **kwds): + """Get a (possibly substituted) variable and convert text to a + boolean + + """ + s = self.getfirstvar_s(*varnames, **kwds) + if isinstance(s, self._TEXTTYPE): + sl = s.strip().lower() + if sl not in self._BOOL_CVT: + raise ValueError("Not a boolean: %r" % s) + return self._BOOL_CVT[sl] + else: + return s + # Conversion of booleans _BOOL_CVT = { u('1'): True, u('yes'): True, u('true'): True, u('on'): True, diff -r ac3e3cd6faae -r 0c65aac81807 tests/test.py --- a/tests/test.py Sun Jul 11 16:43:14 2021 +0200 +++ b/tests/test.py Sun Jul 11 17:08:06 2021 +0200 @@ -514,6 +514,73 @@ "intl.non.existing", "intl.non.existing2") + def test23_getfirstintvar_s_nonexisting(self): + cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml")) + self.assertIsNone(cfg.getfirstintvar_s("db.non.existing.key", + "db.non.existing.key2", + "intl.non.existing", + default=None)) + self.assertRaises( + KeyError, + cfg.getfirstintvar_s, + "db.non.existing.key", + "db.non.exksting.key2", + "intl.non.existing") + + def test23_getfirstintvar_s_nonexisting(self): + cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml")) + self.assertEqual(20, + cfg.getfirstintvar_s("db.non.existing.key", + "db.non.existing.key2", + "intl.non.existing", + default=u("20"))) + self.assertEqual(30, + cfg.getfirstintvar_s("db.non.existing.key", + "db.non.existing.key2", + "intl.non.existing", + default=30)) + self.assertRaises( + KeyError, + cfg.getfirstintvar_s, + "db.non.existing.key", + "db.non.exksting.key2", + "intl.non.existing") + + def test24_getfirstintvar_s_existing(self): + cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml")) + self.assertEqual(10, + cfg.getfirstintvar_s("db.non.existing.key", + "intl.cache.items", + "db.non.existing.key2", + default=u("20"))) + + def test25_getfirstboolvar_s_nonexisting(self): + cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml")) + self.assertFalse( + cfg.getfirstboolvar_s("db.non.existing.key", + "db.non.existing.key2", + "db.engines.rw.no-echo", + default=u("false"))) + self.assertTrue( + cfg.getfirstboolvar_s("db.non.existing.key", + "db.non.existing.key2", + "db.engines.rw.no-echo", + default=True)) + self.assertRaises( + KeyError, + cfg.getfirstboolvar_s, + "db.non.existing.key", + "db.non.exksting.key2", + "b.engines.rw.no-echo") + + def test26_getfirstboolvar_s_existing(self): + cfg = self._load(os.path.join(TESTDATADIR, "conf20.yml")) + self.assertFalse( + cfg.getfirstboolvar_s("db.non.existing.key", + "db.engines.rw.echo", + "db.non.existing.key2", + default=u("true"))) + class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase):