Fix config interpolation with non-default values.

Fixes #202.
This commit is contained in:
Florian Bruhin 2014-12-08 18:50:53 +01:00
parent 175d01f516
commit 0c1420112c
3 changed files with 22 additions and 7 deletions

View File

@ -369,11 +369,21 @@ class ConfigManager(QObject):
if (sectname, k) in self.RENAMED_OPTIONS: if (sectname, k) in self.RENAMED_OPTIONS:
k = self.RENAMED_OPTIONS[sectname, k] k = self.RENAMED_OPTIONS[sectname, k]
try: try:
self.set('conf', sectname, k, v) self.set('conf', sectname, k, v, validate=False)
except configtypes.ValidationError as e: except configtypes.ValidationError as e:
e.section = sectname e.section = sectname
e.option = k e.option = k
raise raise
self._validate_all()
def _validate_all(self):
"""Validate all values set in self._from_cp."""
for sectname, sect in self.sections.items():
mapping = {key: val.value() for key, val in sect.values.items()}
for optname, opt in sect.items():
interpolated = self._interpolation.before_get(
self, sectname, optname, opt.value(), mapping)
opt.typ.validate(interpolated)
def _changed(self, sectname, optname): def _changed(self, sectname, optname):
"""Notify other objects the config has changed.""" """Notify other objects the config has changed."""
@ -529,7 +539,7 @@ class ConfigManager(QObject):
raise cmdexc.CommandError("set: {} - {}".format( raise cmdexc.CommandError("set: {} - {}".format(
e.__class__.__name__, e)) e.__class__.__name__, e))
def set(self, layer, sectname, optname, value): def set(self, layer, sectname, optname, value, validate=True):
"""Set an option. """Set an option.
Args: Args:
@ -537,6 +547,7 @@ class ConfigManager(QObject):
sectname: The name of the section to change. sectname: The name of the section to change.
optname: The name of the option to change. optname: The name of the option to change.
value: The new value. value: The new value.
validate: Whether to validate the value immediately.
""" """
try: try:
value = self._interpolation.before_set(self, sectname, optname, value = self._interpolation.before_set(self, sectname, optname,
@ -548,8 +559,11 @@ class ConfigManager(QObject):
except KeyError: except KeyError:
raise NoSectionError(sectname) raise NoSectionError(sectname)
mapping = {key: val.value() for key, val in sect.values.items()} mapping = {key: val.value() for key, val in sect.values.items()}
interpolated = self._interpolation.before_get(self, sectname, optname, if validate:
value, mapping) interpolated = self._interpolation.before_get(
self, sectname, optname, value, mapping)
else:
interpolated = None
try: try:
sect.setv(layer, optname, value, interpolated) sect.setv(layer, optname, value, interpolated)
except KeyError: except KeyError:

View File

@ -78,7 +78,7 @@ class Section:
ValueLayers dict. ValueLayers dict.
key: The key of the element to set. key: The key of the element to set.
value: The value to set. value: The value to set.
interpolated: The interpolated value, for checking. interpolated: The interpolated value, for checking, or None.
""" """
raise NotImplementedError raise NotImplementedError

View File

@ -92,7 +92,8 @@ class SettingValue:
layer: The layer to set the value on, an element name of the layer: The layer to set the value on, an element name of the
ValueLayers dict. ValueLayers dict.
value: The value to set. value: The value to set.
interpolated: The interpolated value, for typechecking. interpolated: The interpolated value, for typechecking (or None).
""" """
self.typ.validate(interpolated) if interpolated is not None:
self.typ.validate(interpolated)
self.values[layer] = value self.values[layer] = value