qutebrowser/qutebrowser/config/value.py

101 lines
3.2 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2015-01-03 15:51:31 +01:00
# Copyright 2014-2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-04-11 06:28:07 +02:00
#
# 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/>.
"""A single value (with multiple layers possibly) in the config."""
2014-08-26 19:10:14 +02:00
import collections
2014-04-11 06:28:07 +02:00
class SettingValue:
"""Base class for setting values.
2015-03-31 20:49:29 +02:00
Intended to be sub-classed by config value "types".
2014-04-11 06:28:07 +02:00
Attributes:
typ: A BaseType subclass instance.
value: (readonly property) The currently valid, most important value.
values: An OrderedDict with the values on different layers, with the
most significant layer first.
2014-04-11 06:28:07 +02:00
"""
def __init__(self, typ, default=None):
"""Constructor.
Args:
typ: The BaseType to use.
default: Raw value to set.
"""
self.typ = typ
self.values = collections.OrderedDict.fromkeys(
2014-08-26 19:10:14 +02:00
['temp', 'conf', 'default'])
self.values['default'] = default
2014-04-11 06:28:07 +02:00
def __str__(self):
"""Get raw string value."""
2014-09-30 06:32:21 +02:00
return self.value()
2014-04-11 06:28:07 +02:00
2014-05-28 22:39:02 +02:00
def default(self):
"""Get the default value."""
return self.values['default']
2014-04-11 19:34:34 +02:00
def getlayers(self, startlayer):
"""Get a dict of values starting with startlayer.
Args:
startlayer: The first layer to include.
"""
idx = list(self.values.keys()).index(startlayer)
d = collections.OrderedDict(list(self.values.items())[idx:])
2014-04-11 19:34:34 +02:00
return d
def value(self, startlayer=None):
2014-04-11 19:34:34 +02:00
"""Get the first valid value starting from startlayer.
Args:
startlayer: The first layer to include.
"""
if startlayer is None:
d = self.values
2014-04-11 19:34:34 +02:00
else:
d = self.getlayers(startlayer)
for val in d.values():
if val is not None:
return val
2014-08-04 03:47:09 +02:00
else: # pylint: disable=useless-else-on-loop
2015-03-11 20:14:39 +01:00
# https://bitbucket.org/logilab/pylint/issue/489/
2014-04-11 19:34:34 +02:00
raise ValueError("No valid config value found!")
2014-04-11 06:28:07 +02:00
def transformed(self):
"""Get the transformed value."""
return self.typ.transform(self.value())
2014-04-11 06:28:07 +02:00
def setv(self, layer, value, interpolated):
"""Set the value on a layer.
2014-04-11 06:28:07 +02:00
2014-04-17 17:44:27 +02:00
Args:
layer: The layer to set the value on, an element name of the
ValueLayers dict.
value: The value to set.
interpolated: The interpolated value, for typechecking (or None).
"""
if interpolated is not None:
self.typ.validate(interpolated)
self.values[layer] = value