Mercurial > hgrepos > Python > libs > ConfigMix
diff configmix/config.py @ 373:0c65aac81807
Implement ".getfirstintvar_s()" and ".getfirstboolvar_s()" with unittests
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 11 Jul 2021 17:08:06 +0200 |
| parents | 4ee53f6fcac1 |
| children | 4d7ad20cb8f9 |
line wrap: on
line diff
--- 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,
