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 get(self, sect, opt, raw=True): """Get a value from the config.""" data = self.data[sect] try: return data[opt] except KeyError: raise configexc.NoOptionError(opt, sect)
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 validate(self) -> None: """Make sure the configured option or prefix exists. We can't do this in __init__ as configdata isn't ready yet. """ if (self._option not in configdata.DATA and not configdata.is_valid_prefix(self._option)): raise configexc.NoOptionError(self._option)
def set(self, sect, opt, value): """Set a value in the config.""" data = self.data[sect] try: data[opt] = value self.changed.emit(sect, opt) except KeyError: raise configexc.NoOptionError(opt, sect)
def test_no_option_error(deleted, renamed, all_names, expected): e = configexc.NoOptionError( 'opt', deleted=deleted, renamed=renamed, all_names=all_names, ) assert e.option == 'opt' assert str(e) == expected
def get_opt(self, name): """Get a configdata.Option object for the given setting.""" try: return configdata.DATA[name] except KeyError: deleted = name in configdata.MIGRATIONS.deleted renamed = configdata.MIGRATIONS.renamed.get(name) exception = configexc.NoOptionError( name, deleted=deleted, renamed=renamed) raise exception from None
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 __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 test_no_option_error(): e = configexc.NoOptionError('opt', 'sect') assert e.section == 'sect' assert e.option == 'opt' assert str(e) == "No option 'opt' in section 'sect'"
def test_no_option_error_clash(): with pytest.raises(AssertionError): configexc.NoOptionError('opt', deleted=True, renamed='foo')
def get_opt(self, name): """Get a configdata.Option object for the given setting.""" try: return configdata.DATA[name] except KeyError: raise configexc.NoOptionError(name) from None