Use common base class for config section

This commit is contained in:
Florian Bruhin 2014-04-17 12:21:22 +02:00
parent 3eca7d6847
commit c6df785c41

View File

@ -23,12 +23,9 @@ from collections import OrderedDict, ChainMap
from qutebrowser.config.value import SettingValue from qutebrowser.config.value import SettingValue
class KeyValue: class Section:
"""Representation of a section with ordinary key-value mappings. """Base class for KeyValue/ValueList sections.
This is a section which contains normal "key = value" pairs with a fixed
set of keys.
Attributes: Attributes:
values: An OrderedDict with key as index and value as value. values: An OrderedDict with key as index and value as value.
@ -37,20 +34,9 @@ class KeyValue:
descriptions: A dict with the description strings for the keys. descriptions: A dict with the description strings for the keys.
""" """
def __init__(self, *args): def __init__(self):
"""Constructor. self.values = None
Args:
*args: Key/Value pairs to set.
key: string
value: SettingValue
"""
if args:
self.descriptions = {} self.descriptions = {}
self.values = OrderedDict()
for (k, settingval, desc) in args:
self.values[k] = settingval
self.descriptions[k] = desc
def __getitem__(self, key): def __getitem__(self, key):
"""Get the value for key. """Get the value for key.
@ -74,17 +60,24 @@ class KeyValue:
def __iter__(self): def __iter__(self):
"""Iterate over all set values.""" """Iterate over all set values."""
# FIXME using a custom iterator this could be done more efficiently.
return self.values.__iter__() return self.values.__iter__()
def __bool__(self): def __bool__(self):
"""Get boolean state.""" """Get boolean state of section."""
return bool(self.values) return bool(self.values)
def __contains__(self, key): def __contains__(self, key):
"""Return whether the section contains a given key.""" """Return whether the section contains a given key."""
return key in self.values return key in self.values
def items(self):
"""Get dict items."""
return self.values.items()
def keys(self):
"""Get value keys."""
return self.values.keys()
def setv(self, layer, key, value): def setv(self, layer, key, value):
"""Set the value on a layer. """Set the value on a layer.
@ -94,27 +87,45 @@ class KeyValue:
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.
""" """
self.values[key].setv(layer, value) raise NotImplementedError
def items(self):
"""Get dict item tuples."""
return self.values.items()
def keys(self):
"""Get value keys."""
return self.values.keys()
def from_cp(self, sect): def from_cp(self, sect):
"""Initialize the values from a configparser section. """Initialize the values from a configparser section."""
raise NotImplementedError
We assume all keys already exist from the defaults.
class KeyValue(Section):
"""Representation of a section with ordinary key-value mappings.
This is a section which contains normal "key = value" pairs with a fixed
set of keys.
""" """
def __init__(self, *defaults):
"""Constructor.
Args:
*defaults: A (key, value, description) list of defaults.
"""
super().__init__()
if not defaults:
return
self.values = OrderedDict()
for (k, v, desc) in defaults:
self.values[k] = v
self.descriptions[k] = desc
def setv(self, layer, key, value):
self.values[key].setv(layer, value)
def from_cp(self, sect):
for k, v in sect.items(): for k, v in sect.items():
logging.debug("'{}' = '{}'".format(k, v)) logging.debug("'{}' = '{}'".format(k, v))
self.values[k].setv('conf', v) self.values[k].setv('conf', v)
class ValueList: class ValueList(Section):
"""This class represents a section with a list key-value settings. """This class represents a section with a list key-value settings.
@ -122,23 +133,26 @@ class ValueList:
have a dynamic list of "key = value" pairs, like keybindings or have a dynamic list of "key = value" pairs, like keybindings or
searchengines. searchengines.
They basically consist of two different SettingValues and have no defaults. They basically consist of two different SettingValues.
Attributes: Attributes:
values: An OrderedDict with key as index and value as value.
default: An OrderedDict with the default configuration as strings. default: An OrderedDict with the default configuration as strings.
keytype: The type to use for the key (only used for validating) keytype: The type to use for the key (only used for validating)
valtype: The type to use for the value. valtype: The type to use for the value.
valdict: The "true value" dict. valdict: The "true value" dict.
#descriptions: A dict with the description strings for the keys.
# Currently a global empty dict to be compatible with
# KeyValue section.
""" """
# FIXME how to handle value layers here? # FIXME how to handle value layers here?
def __init__(self, keytype, valtype, *defaults): def __init__(self, keytype, valtype, *defaults):
"""Wrap types over default values. Take care when overriding this.""" """Wrap types over default values. Take care when overriding this.
Arguments:
keytype: The type to be used for keys.
valtype: The type to be used for values.
*defaults: A (key, value) list of default values.
"""
super().__init__()
self.keytype = keytype self.keytype = keytype
self.valtype = valtype self.valtype = valtype
self.layers = OrderedDict([ self.layers = OrderedDict([
@ -150,39 +164,6 @@ class ValueList:
self.values = ChainMap(self.layers['temp'], self.layers['conf'], self.values = ChainMap(self.layers['temp'], self.layers['conf'],
self.layers['default']) self.layers['default'])
def __getitem__(self, key):
"""Get the value for key.
Args:
key: The key to get a value for, as a string.
Return:
The value, as value class.
"""
return self.values[key]
def __setitem__(self, key, value):
"""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.setv('conf', key, value)
def __iter__(self):
"""Iterate over all set values."""
# FIXME using a custon iterator this could be done more efficiently
return self.values.__iter__()
def __bool__(self):
"""Get boolean state of section."""
return bool(self.values)
def __contains__(self, key):
"""Return whether the section contains a given key."""
return key in self.values
def setv(self, layer, key, value): def setv(self, layer, key, value):
"""Set the value on a layer. """Set the value on a layer.
@ -199,14 +180,6 @@ class ValueList:
val.setv(layer, value) val.setv(layer, value)
self.layers[layer][key] = val self.layers[layer][key] = val
def items(self):
"""Get dict items."""
return self.values.items()
def keys(self):
"""Get value keys."""
return self.values.keys()
def from_cp(self, sect): def from_cp(self, sect):
"""Initialize the values from a configparser section.""" """Initialize the values from a configparser section."""
keytype = self.keytype() keytype = self.keytype()