Mercurial > hgrepos > Python > libs > ConfigMix
diff configmix/config.py @ 412:816327e178b0
Provide coercing methods for the jailed configuration: getintXXX(), getboolXXX(), getfloatXXX() and friends.
This is done be refactoring Configuration and putting the coercing
methods into a common mixin.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 20 Nov 2021 13:52:08 +0100 |
| parents | 3c95faa91dad |
| children | 2abde0d3c735 |
line wrap: on
line diff
--- a/configmix/config.py Fri Nov 19 15:52:01 2021 +0100 +++ b/configmix/config.py Sat Nov 20 13:52:08 2021 +0100 @@ -50,7 +50,169 @@ return v -class Configuration(_AttributeDict): +class CoercingMethodsMixin(object): + + """Mixin to provide some common implementations for retrieval + methods that convert return values to a fixed type (int, bool, + float). + + Both :class:`~.Configuration` and :class:`~._JailedConfiguration` use + this mixin. + + """ + + def getintvarl_s(self, *path, **kwds): + """Get a (possibly substituted) variable and coerce text to a + number. + + """ + s = self.getvarl_s(*path, **kwds) + if isinstance(s, Configuration._TEXTTYPE): + return int(s, 0) + else: + return s + + def getfirstintvarl_s(self, *paths, **kwds): + """Get a (possibly substituted) variable and coerce text to a + number. + + """ + s = self.getfirstvarl_s(*paths, **kwds) + if isinstance(s, Configuration._TEXTTYPE): + return int(s, 0) + else: + return s + + def getintvar_s(self, varname, default=_MARKER): + """Get a (possibly substituted) variable and coerce text to a + number. + + """ + s = self.getvar_s(varname, default=default) + if isinstance(s, Configuration._TEXTTYPE): + return int(s, 0) + else: + return s + + def getfirstintvar_s(self, *varnames, **kwds): + """A variant of :meth:`~.getintvar_s` that returns the first found + variable in the list of given variables in `varnames`. + + """ + s = self.getfirstvar_s(*varnames, **kwds) + if isinstance(s, Configuration._TEXTTYPE): + return int(s, 0) + else: + return s + + def getboolvarl_s(self, *path, **kwds): + """Get a (possibly substituted) variable and convert text to a + boolean + + """ + s = self.getvarl_s(*path, **kwds) + if isinstance(s, Configuration._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 + + def getfirstboolvarl_s(self, *paths, **kwds): + """Get a (possibly substituted) variable and convert text to a + boolean + + """ + s = self.getfirstvarl_s(*paths, **kwds) + if isinstance(s, Configuration._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 + + def getboolvar_s(self, varname, default=_MARKER): + """Get a (possibly substituted) variable and convert text to a + boolean + + """ + s = self.getvar_s(varname, default=default) + if isinstance(s, Configuration._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 + + def getfirstboolvar_s(self, *varnames, **kwds): + """A variant of :meth:`~.getboolvar_s` that returns the first found + variable in the list of given variables in `varnames`. + + """ + s = self.getfirstvar_s(*varnames, **kwds) + if isinstance(s, Configuration._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, + u('0'): False, u('no'): False, u('false'): False, u('off'): False + } + + def getfloatvarl_s(self, *path, **kwds): + """Get a (possibly substituted) variable and convert text to a + float + + """ + s = self.getvarl_s(*path, **kwds) + if isinstance(s, Configuration._TEXTTYPE): + return float(s) + else: + return s + + def getfirstfloatvarl_s(self, *path, **kwds): + """Get a (possibly substituted) variable and convert text to a + float + + """ + s = self.getfirstvarl_s(*path, **kwds) + if isinstance(s, Configuration._TEXTTYPE): + return float(s) + else: + return s + + def getfloatvar_s(self, varname, default=_MARKER): + """Get a (possibly substituted) variable and convert text to a + float + + """ + s = self.getvar_s(varname, default) + if isinstance(s, Configuration._TEXTTYPE): + return float(s) + else: + return s + + def getfirstfloatvar_s(self, varname, default=_MARKER): + """Get a (possibly substituted) variable and convert text to a + float + + """ + s = self.getfirstvar_s(varname, default) + if isinstance(s, Configuration._TEXTTYPE): + return float(s) + else: + return s + + +class Configuration(CoercingMethodsMixin, _AttributeDict): """The configuration dictionary with attribute support or variable substitution. @@ -297,156 +459,6 @@ else: return default - def getintvarl_s(self, *path, **kwds): - """Get a (possibly substituted) variable and coerce text to a - number. - - """ - s = self.getvarl_s(*path, **kwds) - if isinstance(s, self._TEXTTYPE): - return int(s, 0) - else: - return s - - def getfirstintvarl_s(self, *paths, **kwds): - """Get a (possibly substituted) variable and coerce text to a - number. - - """ - s = self.getfirstvarl_s(*paths, **kwds) - if isinstance(s, self._TEXTTYPE): - return int(s, 0) - else: - return s - - def getintvar_s(self, varname, default=_MARKER): - """Get a (possibly substituted) variable and coerce text to a - number. - - """ - s = self.getvar_s(varname, default=default) - if isinstance(s, self._TEXTTYPE): - return int(s, 0) - else: - return s - - def getfirstintvar_s(self, *varnames, **kwds): - """A variant of :meth:`~.getintvar_s` that returns the first found - variable in the list of given variables in `varnames`. - - """ - s = self.getfirstvar_s(*varnames, **kwds) - if isinstance(s, self._TEXTTYPE): - return int(s, 0) - else: - return s - - def getboolvarl_s(self, *path, **kwds): - """Get a (possibly substituted) variable and convert text to a - boolean - - """ - s = self.getvarl_s(*path, **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 - - def getfirstboolvarl_s(self, *paths, **kwds): - """Get a (possibly substituted) variable and convert text to a - boolean - - """ - s = self.getfirstvarl_s(*paths, **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 - - def getboolvar_s(self, varname, default=_MARKER): - """Get a (possibly substituted) variable and convert text to a - boolean - - """ - s = self.getvar_s(varname, default=default) - 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 - - def getfirstboolvar_s(self, *varnames, **kwds): - """A variant of :meth:`~.getboolvar_s` that returns the first found - variable in the list of given variables in `varnames`. - - """ - 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, - u('0'): False, u('no'): False, u('false'): False, u('off'): False - } - - def getfloatvarl_s(self, *path, **kwds): - """Get a (possibly substituted) variable and convert text to a - float - - """ - s = self.getvarl_s(*path, **kwds) - if isinstance(s, self._TEXTTYPE): - return float(s) - else: - return s - - def getfirstfloatvarl_s(self, *path, **kwds): - """Get a (possibly substituted) variable and convert text to a - float - - """ - s = self.getfirstvarl_s(*path, **kwds) - if isinstance(s, self._TEXTTYPE): - return float(s) - else: - return s - - def getfloatvar_s(self, varname, default=_MARKER): - """Get a (possibly substituted) variable and convert text to a - float - - """ - s = self.getvar_s(varname, default) - if isinstance(s, self._TEXTTYPE): - return float(s) - else: - return s - - def getfirstfloatvar_s(self, varname, default=_MARKER): - """Get a (possibly substituted) variable and convert text to a - float - - """ - s = self.getfirstvar_s(varname, default) - if isinstance(s, self._TEXTTYPE): - return float(s) - else: - return s - def _split_ns(self, s): nameparts = s.split(self._NS_SEPARATOR, 1) if len(nameparts) == 1: @@ -693,7 +705,7 @@ return jc -class _JailedConfiguration(object): +class _JailedConfiguration(CoercingMethodsMixin): """A jailed and restricted variant of :class:`Configuration`.
