def get(self, sectname, optname, raw=False, transformed=True): """Get the value from a section/option. Args: sectname: The section to get the option from. optname: The option name raw: Whether to get the uninterpolated, untransformed value. transformed: Whether the value should be transformed. Return: The value of the option. """ if not self._initialized: raise Exception("get got called before initialization was " "complete!") try: sect = self.sections[sectname] except KeyError: raise configexc.NoSectionError(sectname) try: val = sect[optname] except KeyError: raise configexc.NoOptionError(optname, sectname) if raw: return val.value() mapping = {key: val.value() for key, val in sect.values.items()} newval = self._interpolation.before_get(self, sectname, optname, val.value(), mapping) if transformed: newval = val.typ.transform(newval) return newval
def set(self, layer, sectname, optname, value, validate=True): """Set an option. Args: layer: A layer name as string (conf/temp/default). sectname: The name of the section to change. optname: The name of the option to change. value: The new value. validate: Whether to validate the value immediately. """ try: value = self._interpolation.before_set(self, sectname, optname, value) except ValueError as e: raise configexc.InterpolationSyntaxError(optname, sectname, str(e)) try: sect = self.sections[sectname] except KeyError: raise configexc.NoSectionError(sectname) mapping = {key: val.value() for key, val in sect.values.items()} if validate: interpolated = self._interpolation.before_get( self, sectname, optname, value, mapping) else: interpolated = None try: sect.setv(layer, optname, value, interpolated) except KeyError: raise configexc.NoOptionError(optname, sectname) else: if self._initialized: self._after_set(sectname, optname)
def __init__(self, sectname, optname=None): """Save decorator arguments. Gets called on parse-time with the decorator arguments. Args: See class attributes. """ if sectname not in configdata.DATA: raise configexc.NoSectionError(sectname) if optname is not None and optname not in configdata.DATA[sectname]: raise configexc.NoOptionError(optname, sectname) self._sectname = sectname self._optname = optname
def _from_cp(self, cp, relaxed=False): """Read the config from a configparser instance. Args: cp: The configparser instance to read the values from. relaxed: Whether to ignore inexistent sections/options. """ for sectname in cp: if sectname in self.RENAMED_SECTIONS: sectname = self.RENAMED_SECTIONS[sectname] if sectname is not 'DEFAULT' and sectname not in self.sections: if not relaxed: raise configexc.NoSectionError(sectname) for sectname in self.sections: self._from_cp_section(sectname, cp, relaxed)
def __init__(self, sectname, optname=None, function=False): """Save decorator arguments. Gets called on parse-time with the decorator arguments. Args: sectname: The section to be filtered. optname: The option to be filtered. function: Whether a function rather than a method is decorated. """ if sectname not in configdata.DATA: raise configexc.NoSectionError(sectname) if optname is not None and optname not in configdata.DATA[sectname]: raise configexc.NoOptionError(optname, sectname) self._sectname = sectname self._optname = optname self._function = function
def set(self, layer, sectname, optname, value, validate=True): """Set an option. Args: layer: A layer name as string (conf/temp/default). sectname: The name of the section to change. optname: The name of the option to change. value: The new value. validate: Whether to validate the value immediately. """ try: value = self._interpolation.before_set(self, sectname, optname, value) except ValueError as e: raise configexc.InterpolationSyntaxError(optname, sectname, str(e)) try: sect = self.sections[sectname] except KeyError: raise configexc.NoSectionError(sectname) mapping = {key: val.value() for key, val in sect.values.items()} if validate: interpolated = self._interpolation.before_get( self, sectname, optname, value, mapping) try: allowed_backends = sect.values[optname].backends except KeyError: # Will be handled later in .setv() pass else: backend = usertypes.arg2backend[objreg.get('args').backend] if (allowed_backends is not None and backend not in allowed_backends): raise configexc.BackendError(backend) else: interpolated = None try: sect.setv(layer, optname, value, interpolated) except KeyError: raise configexc.NoOptionError(optname, sectname) else: if self._initialized: self._after_set(sectname, optname)
def remove_option(self, sectname, optname): """Remove an option. Args: sectname: The section where to remove an option. optname: The option name to remove. Return: True if the option existed, False otherwise. """ try: sectdict = self.sections[sectname] except KeyError: raise configexc.NoSectionError(sectname) optname = self.optionxform(optname) existed = optname in sectdict if existed: del sectdict[optname] self.get.cache_clear() return existed
def remove_option(self, sectname, optname): """Remove an option. Args: sectname: The section where to remove an option. optname: The option name to remove. Return: True if the option existed, False otherwise. """ try: sectdict = self.sections[sectname] except KeyError: raise configexc.NoSectionError(sectname) optname = self.optionxform(optname) existed = optname in sectdict if existed: sectdict.delete(optname) # WORKAROUND for https://bitbucket.org/logilab/pylint/issues/659/ self.get.cache_clear() # pylint: disable=no-member return existed
def _from_cp(self, cp): """Read the config from a configparser instance. Args: cp: The configparser instance to read the values from. """ for sectname in cp: if sectname in self.RENAMED_SECTIONS: sectname = self.RENAMED_SECTIONS[sectname] if sectname is not 'DEFAULT' and sectname not in self.sections: raise configexc.NoSectionError(sectname) for sectname in self.sections: real_sectname = self._get_real_sectname(cp, sectname) if real_sectname is None: continue for k, v in cp[real_sectname].items(): if k.startswith(self.ESCAPE_CHAR): k = k[1:] if (sectname, k) in self.RENAMED_OPTIONS: k = self.RENAMED_OPTIONS[sectname, k] self.set('conf', sectname, k, v, validate=False)
def test_no_section_error(): e = configexc.NoSectionError('sect') assert e.section == 'sect' assert str(e) == "Section 'sect' does not exist!"