More configparser adjustments
This commit is contained in:
parent
f4d363082d
commit
89e21499c8
@ -93,7 +93,7 @@ class Config:
|
|||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
"""Get a section from the config."""
|
"""Get a section from the config."""
|
||||||
return self.config[key]
|
return self._proxies[key]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Get the whole config as a string."""
|
"""Get the whole config as a string."""
|
||||||
@ -163,8 +163,17 @@ class Config:
|
|||||||
return lines
|
return lines
|
||||||
|
|
||||||
@cmdutils.register(instance='config', completion=['setting'])
|
@cmdutils.register(instance='config', completion=['setting'])
|
||||||
def get(self, section, option, fallback=_UNSET):
|
def get(self, section, option, fallback=_UNSET, raw=False):
|
||||||
"""Get the real (transformed) value from a section/option."""
|
"""Get the value from a section/option.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
section: The section to get the option from.
|
||||||
|
option: The option name
|
||||||
|
fallback: A fallback value.
|
||||||
|
raw: Whether to get the uninterpolated (but transformed!) value.
|
||||||
|
|
||||||
|
"""
|
||||||
|
logging.debug("getting {} -> {}".format(section, option))
|
||||||
try:
|
try:
|
||||||
val = self.config[section][option]
|
val = self.config[section][option]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -173,7 +182,14 @@ class Config:
|
|||||||
else:
|
else:
|
||||||
return fallback
|
return fallback
|
||||||
else:
|
else:
|
||||||
return val.value
|
if raw:
|
||||||
|
return val.transformed()
|
||||||
|
newval = self._interpolation.before_get(
|
||||||
|
self, section, option, val.value,
|
||||||
|
self.config[section].values)
|
||||||
|
logging.debug("interpolated val: {}".format(newval))
|
||||||
|
newval = val.typ.transform(newval)
|
||||||
|
return newval
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
"""Save the config file."""
|
"""Save the config file."""
|
||||||
@ -258,11 +274,10 @@ class SectionProxy(configparser.SectionProxy):
|
|||||||
|
|
||||||
def _options(self):
|
def _options(self):
|
||||||
# TODO
|
# TODO
|
||||||
return self._parser.options(self._name)
|
return self._parser.config[self._name].values.keys()
|
||||||
|
|
||||||
def get(self, option, fallback=None, *, raw=False, vars=None):
|
def get(self, option, fallback=None, *, raw=False, vars=None):
|
||||||
return self._parser.get(self._name, option, raw=raw, vars=vars,
|
return self._parser.get(self._name, option, raw=raw, fallback=fallback)
|
||||||
fallback=fallback)
|
|
||||||
|
|
||||||
def getint(self, option, fallback=None, *, raw=False, vars=None):
|
def getint(self, option, fallback=None, *, raw=False, vars=None):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -304,8 +304,7 @@ class SettingValue:
|
|||||||
Attributes:
|
Attributes:
|
||||||
typ: A BaseType subclass.
|
typ: A BaseType subclass.
|
||||||
default: Default value if the user has not overridden it, as a string.
|
default: Default value if the user has not overridden it, as a string.
|
||||||
value: (property) The currently valid, most important, transformed
|
value: (property) The currently valid, most important value.
|
||||||
value.
|
|
||||||
rawvalue: The current value as a raw string.
|
rawvalue: The current value as a raw string.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -324,17 +323,14 @@ class SettingValue:
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Get raw string value."""
|
"""Get raw string value."""
|
||||||
if self.rawvalue is not None:
|
return self.value
|
||||||
val = self.rawvalue
|
|
||||||
else:
|
def transformed(self):
|
||||||
val = self.default
|
"""Get the transformed value."""
|
||||||
return val
|
v = self.value
|
||||||
|
return self.typ.transform(v)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
"""Get the currently valid value."""
|
"""Get the currently valid value."""
|
||||||
if self.rawvalue is not None:
|
return self.rawvalue if self.rawvalue is not None else self.default
|
||||||
val = self.rawvalue
|
|
||||||
else:
|
|
||||||
val = self.default
|
|
||||||
return self.typ.transform(val)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user