qutebrowser/qutebrowser/config/_sections.py

187 lines
5.8 KiB
Python
Raw Normal View History

2014-02-26 07:58:39 +01:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Setting sections used for qutebrowser."""
from collections import OrderedDict, ChainMap
2014-02-26 07:58:39 +01:00
from qutebrowser.config._value import SettingValue
2014-02-26 07:58:39 +01:00
class Section:
2014-02-26 07:58:39 +01:00
"""Base class for KeyValue/ValueList sections.
2014-02-26 07:58:39 +01:00
Attributes:
values: An OrderedDict with key as index and value as value.
key: string
value: SettingValue
descriptions: A dict with the description strings for the keys.
2014-02-26 07:58:39 +01:00
"""
def __init__(self):
self.values = None
self.descriptions = {}
2014-02-26 07:58:39 +01:00
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 __iter__(self):
"""Iterate over all set values."""
return self.values.__iter__()
2014-02-27 07:18:36 +01:00
def __bool__(self):
"""Get boolean state of section."""
2014-02-27 07:18:36 +01:00
return bool(self.values)
2014-04-07 17:53:57 +02:00
def __contains__(self, key):
"""Return whether the section contains a given key."""
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, interpolated):
"""Set the value on a layer.
2014-04-17 17:44:27 +02:00
Args:
layer: The layer to set the value on, an element name of the
ValueLayers dict.
key: The key of the element to set.
value: The value to set.
interpolated: The interpolated value, for checking.
"""
raise NotImplementedError
2014-04-17 12:35:46 +02:00
def dump_userconfig(self):
"""Dump the part of the config which was changed by the user.
Return:
A list of (key, valuestr) tuples.
"""
raise NotImplementedError
2014-04-14 17:54:11 +02:00
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, interpolated):
self.values[key].setv(layer, value, interpolated)
2014-03-10 00:37:35 +01:00
2014-04-17 12:35:46 +02:00
def dump_userconfig(self):
changed = []
for k, v in self.items():
if (v.values['temp'] is not None and
v.values['temp'] != v.values['default']):
changed.append((k, v.values['temp']))
elif (v.values['conf'] is not None and
v.values['conf'] != v.values['default']):
changed.append((k, v.values['conf']))
return changed
2014-02-26 07:58:39 +01:00
class ValueList(Section):
2014-02-27 23:09:53 +01:00
"""This class represents a section with a list key-value settings.
These are settings inside sections which don't have fixed keys, but instead
have a dynamic list of "key = value" pairs, like keybindings or
searchengines.
They basically consist of two different SettingValues.
2014-02-27 23:09:53 +01:00
Attributes:
default: An OrderedDict with the default configuration as strings.
2014-04-10 12:09:32 +02:00
keytype: The type to use for the key (only used for validating)
valtype: The type to use for the value.
2014-02-27 23:09:53 +01:00
valdict: The "true value" dict.
"""
# FIXME how to handle value layers here?
2014-04-07 17:46:48 +02:00
def __init__(self, keytype, valtype, *defaults):
"""Wrap types over default values. Take care when overriding this.
2014-04-17 17:44:27 +02:00
Args:
keytype: The type instance to be used for keys.
valtype: The type instance to be used for values.
*defaults: A (key, value) list of default values.
"""
super().__init__()
2014-04-10 12:09:32 +02:00
self.keytype = keytype
self.valtype = valtype
self.layers = OrderedDict([
('default', OrderedDict([(key, SettingValue(valtype, value))
for key, value in defaults])),
('conf', OrderedDict()),
('temp', OrderedDict()),
])
self.values = ChainMap(self.layers['temp'], self.layers['conf'],
self.layers['default'])
2014-02-27 23:09:53 +01:00
def setv(self, layer, key, value, interpolated):
self.keytype.validate(key)
if key in self.layers[layer]:
self.layers[layer][key].setv(layer, value, interpolated)
else:
val = SettingValue(self.valtype)
val.setv(layer, value, interpolated)
self.layers[layer][key] = val
2014-04-17 12:35:46 +02:00
def dump_userconfig(self):
changed = []
mapping = ChainMap(self.layers['temp'], self.layers['conf'])
for k, v in mapping.items():
try:
if v.value != self.layers['default'][k].value:
changed.append((k, v.value))
except KeyError:
2014-04-17 12:35:46 +02:00
changed.append((k, v.value))
return changed