qutebrowser/qutebrowser/config/sections.py

222 lines
6.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."""
2014-03-10 00:37:35 +01:00
import logging
2014-02-26 07:58:39 +01:00
from collections import OrderedDict
2014-04-11 06:28:07 +02:00
from qutebrowser.config.value import SettingValue
2014-02-26 07:58:39 +01:00
class KeyValue:
"""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.
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, *args):
"""Constructor.
Args:
*args: Key/Value pairs to set.
key: string
value: SettingValue
"""
if args:
self.descriptions = {}
self.values = OrderedDict()
for (k, settingval, desc) in args:
self.values[k] = settingval
self.descriptions[k] = desc
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 __setitem__(self, key, value):
"""Set the config value for key.
2014-02-26 07:58:39 +01:00
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)
2014-02-26 07:58:39 +01:00
def __iter__(self):
"""Iterate over all set values."""
2014-02-27 23:21:21 +01:00
# FIXME using a custom iterator this could be done more efficiently.
2014-02-26 07:58:39 +01:00
return self.values.__iter__()
2014-02-27 07:18:36 +01:00
def __bool__(self):
"""Get boolean state."""
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 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()
2014-03-10 00:37:35 +01:00
def from_cp(self, sect):
"""Initialize the values from a configparser section.
We assume all keys already exist from the defaults.
"""
2014-03-10 00:37:35 +01:00
for k, v in sect.items():
logging.debug("'{}' = '{}'".format(k, v))
self.values[k].setv('conf', v)
2014-03-10 00:37:35 +01:00
2014-02-26 07:58:39 +01:00
2014-02-27 23:09:53 +01:00
class ValueList:
"""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 and have no defaults.
Attributes:
values: An OrderedDict with key as index and value as value.
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.
2014-04-02 16:47:21 +02:00
#descriptions: A dict with the description strings for the keys.
# Currently a global empty dict to be compatible with
# KeyValue section.
2014-02-27 23:09:53 +01:00
"""
# FIXME use a ChainMap for this
# FIXME how to handle value layers here?
2014-04-07 17:46:48 +02:00
def __init__(self, keytype, valtype, *defaults):
2014-02-27 23:09:53 +01:00
"""Wrap types over default values. Take care when overriding this."""
2014-04-10 12:09:32 +02:00
self.keytype = keytype
self.valtype = valtype
2014-02-27 23:09:53 +01:00
self.values = OrderedDict()
self.default = OrderedDict(
2014-04-11 06:28:07 +02:00
[(key, SettingValue(valtype, value))
2014-04-07 17:46:48 +02:00
for key, value in defaults])
2014-02-27 23:21:21 +01:00
self.valdict = OrderedDict()
2014-02-27 23:09:53 +01:00
def update_valdict(self):
2014-02-27 23:21:21 +01:00
"""Update the global "true" value dict."""
self.valdict.clear()
2014-02-27 23:09:53 +01:00
self.valdict.update(self.default)
if self.values is not None:
self.valdict.update(self.values)
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.
"""
try:
return self.values[key]
except KeyError:
return self.default[key]
2014-04-10 12:05:32 +02:00
def __setitem__(self, key, value):
"""Set the config value for key.
2014-04-10 12:05:32 +02:00
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)
2014-04-10 12:05:32 +02:00
2014-02-27 23:09:53 +01:00
def __iter__(self):
"""Iterate over all set values."""
# FIXME using a custon iterator this could be done more efficiently
self.update_valdict()
return self.valdict.__iter__()
def __bool__(self):
"""Get boolean state of section."""
self.update_valdict()
return bool(self.valdict)
2014-04-07 17:53:57 +02:00
def __contains__(self, key):
"""Return whether the section contains a given key."""
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
2014-02-27 23:09:53 +01:00
def items(self):
"""Get dict items."""
self.update_valdict()
return self.valdict.items()
2014-03-10 00:37:35 +01:00
def from_cp(self, sect):
"""Initialize the values from a configparser section."""
2014-04-10 12:09:32 +02:00
keytype = self.keytype()
valtype = self.valtype()
2014-03-10 00:37:35 +01:00
for k, v in sect.items():
keytype.validate(k)
valtype.validate(v)
self.setv('conf', k, v)