Start implementing different config layers.

With a namedtuple though, which is pretty much a fail.
This commit is contained in:
Florian Bruhin 2014-04-11 07:16:16 +02:00
parent 575a934892
commit e7c23312d7
3 changed files with 78 additions and 25 deletions

View File

@ -322,13 +322,16 @@ class Config(QObject):
Return:
The changed config part as string.
"""
# FIXME adopt this for layering
lines = []
for secname, section in self.config.items():
changed_opts = []
for optname, option in section.items():
if (option.rawvalue is not None and
option.rawvalue != option.default):
keyval = '{} = {}'.format(optname, option)
if (option.values.temp is not None and
option.values.temp != option.default or
option.values.conf is not None and
option.values.conf != option.default):
keyval = '{} = {}'.format(optname, option) # FIXME layer?
changed_opts.append(keyval)
if changed_opts:
lines.append('[{}]'.format(secname))

View File

@ -64,13 +64,13 @@ class KeyValue:
return self.values[key]
def __setitem__(self, key, value):
"""Set the value for key.
"""Set the config value for key.
Args:
key: The key to set the value for, as a string.
value: The value to set, as a string
"""
self.values[key].value = value
self.setv('conf', key, value)
def __iter__(self):
"""Iterate over all set values."""
@ -85,15 +85,29 @@ class KeyValue:
"""Return whether the section contains a given key."""
return key in self.values
def setv(self, layer, key, value):
"""Set the value on a layer.
Arguments:
layer: The layer to set the value on, an element name of the
ValueLayers namedtuple.
key: The key of the element to set.
value: The value to set.
"""
self.values[key].setv(layer, value)
def items(self):
"""Get dict item tuples."""
return self.values.items()
def from_cp(self, sect):
"""Initialize the values from a configparser section."""
"""Initialize the values from a configparser section.
We assume all keys already exist from the defaults.
"""
for k, v in sect.items():
logging.debug("'{}' = '{}'".format(k, v))
self.values[k].value = v
self.values[k].setv('conf', v)
class ValueList:
@ -117,6 +131,9 @@ class ValueList:
# KeyValue section.
"""
# FIXME use a ChainMap for this
# FIXME how to handle value layers here?
def __init__(self, keytype, valtype, *defaults):
"""Wrap types over default values. Take care when overriding this."""
self.keytype = keytype
@ -149,14 +166,13 @@ class ValueList:
return self.default[key]
def __setitem__(self, key, value):
"""Set the value for key.
"""Set the config value for key.
Args:
key: The key to set the value for, as a string.
value: The value to set, as a string
"""
self.values[key] = SettingValue(self.valtype)
self.values[key].value = value
self.setv('conf', key, value)
def __iter__(self):
"""Iterate over all set values."""
@ -174,6 +190,22 @@ class ValueList:
self.update_valdict()
return key in self.valdict
def setv(self, layer, key, value):
"""Set the value on a layer.
Arguments:
layer: The layer to set the value on, an element name of the
ValueLayers namedtuple.
key: The key of the element to set.
value: The value to set.
"""
if key in self.values:
self.values[key].setv(layer, value)
else:
val = SettingValue(self.valtype)
val.setv(layer, value)
self.values[key] = val
def items(self):
"""Get dict items."""
self.update_valdict()
@ -186,4 +218,4 @@ class ValueList:
for k, v in sect.items():
keytype.validate(k)
valtype.validate(v)
self.values[k] = SettingValue(self.valtype, v)
self.setv('conf', k, v)

View File

@ -17,6 +17,10 @@
"""A single value (with multiple layers possibly) in the config."""
from collections import namedtuple
ValueLayers = namedtuple('ValueLayers', 'temp, conf, default')
class SettingValue:
@ -26,9 +30,9 @@ class SettingValue:
Attributes:
typ: A BaseType subclass.
default: Default value if the user has not overridden it, as a string.
value: (property) The currently valid, most important value.
rawvalue: The current value as a raw string.
value: (readonly property) The currently valid, most important value.
_values: A namedtuple with the values on different layers, with the
most significant layer first.
"""
def __init__(self, typ, default=None):
@ -39,25 +43,39 @@ class SettingValue:
default: Raw value to set.
"""
self.typ = typ()
self.rawvalue = None
self.default = default
self._values = ValueLayers(None, None, None)
self._values.default = default
def __str__(self):
"""Get raw string value."""
return self.value
@property
def value(self):
"""Get the currently valid value."""
for val in self._values:
if val is not None:
return val
else:
raise ValueError("No valid config value found!")
@property
def values(self):
"""Readonly property for _values."""
return self._values
def transformed(self):
"""Get the transformed value."""
v = self.value
return self.typ.transform(v)
@property
def value(self):
"""Get the currently valid value."""
return self.rawvalue if self.rawvalue is not None else self.default
def setv(self, layer, value):
"""Set the value on a layer.
@value.setter
def value(self, val):
"""Set the currently valid value."""
self.typ.validate(val)
self.rawvalue = val
Arguments:
layer: The layer to set the value on, an element name of the
ValueLayers namedtuple.
value: The value to set.
"""
self.typ.validate(value)
setattr(self._values, layer, value)