Don't make default config a mutable global.

Before, configdata.DATA only existed once - that means when something
manipulated it, instantiating a new ConfigManager actually gave us the
*modified* rather than the default data.

There's still a (now readonly) configdata.DATA for performance reasons -
before, the settings completion model called data() many times, which caused
initializing of it taking a few (instead of nearly 0) seconds.

See https://github.com/hackebrot/qutebrowser/pull/16#discussion-diff-27770433
This commit is contained in:
Florian Bruhin 2015-04-04 21:20:26 +02:00
parent b2df5a5b47
commit 57158e7191
3 changed files with 898 additions and 833 deletions

View File

@ -281,7 +281,7 @@ class ConfigManager(QObject):
def __init__(self, configdir, fname, relaxed=False, parent=None): def __init__(self, configdir, fname, relaxed=False, parent=None):
super().__init__(parent) super().__init__(parent)
self._initialized = False self._initialized = False
self.sections = configdata.DATA self.sections = configdata.data()
self._interpolation = configparser.ExtendedInterpolation() self._interpolation = configparser.ExtendedInterpolation()
self._proxies = {} self._proxies = {}
for sectname in self.sections.keys(): for sectname in self.sections.keys():

File diff suppressed because it is too large Load Diff

View File

@ -29,6 +29,7 @@ class Section:
"""Base class for KeyValue/ValueList sections. """Base class for KeyValue/ValueList sections.
Attributes: Attributes:
_readonly: Whether this section is read-only.
values: An OrderedDict with key as index and value as value. values: An OrderedDict with key as index and value as value.
key: string key: string
value: SettingValue value: SettingValue
@ -38,6 +39,7 @@ class Section:
def __init__(self): def __init__(self):
self.values = None self.values = None
self.descriptions = {} self.descriptions = {}
self._readonly = False
def __getitem__(self, key): def __getitem__(self, key):
"""Get the value for key. """Get the value for key.
@ -99,13 +101,15 @@ class KeyValue(Section):
set of keys. set of keys.
""" """
def __init__(self, *defaults): def __init__(self, *defaults, readonly=False):
"""Constructor. """Constructor.
Args: Args:
*defaults: A (key, value, description) list of defaults. *defaults: A (key, value, description) list of defaults.
readonly: Whether this config is readonly.
""" """
super().__init__() super().__init__()
self._readonly = readonly
if not defaults: if not defaults:
return return
self.values = collections.OrderedDict() self.values = collections.OrderedDict()
@ -115,6 +119,8 @@ class KeyValue(Section):
self.descriptions[k] = desc self.descriptions[k] = desc
def setv(self, layer, key, value, interpolated): def setv(self, layer, key, value, interpolated):
if self._readonly:
raise ValueError("Trying to modify a read-only config!")
self.values[key].setv(layer, value, interpolated) self.values[key].setv(layer, value, interpolated)
def dump_userconfig(self): def dump_userconfig(self):
@ -143,17 +149,20 @@ class ValueList(Section):
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.
_ordered_value_cache: A ChainMap-like OrderedDict of all values. _ordered_value_cache: A ChainMap-like OrderedDict of all values.
_readonly: Whether this section is read-only.
""" """
def __init__(self, keytype, valtype, *defaults): def __init__(self, keytype, valtype, *defaults, readonly=False):
"""Wrap types over default values. Take care when overriding this. """Wrap types over default values. Take care when overriding this.
Args: Args:
keytype: The type instance to be used for keys. keytype: The type instance to be used for keys.
valtype: The type instance to be used for values. valtype: The type instance to be used for values.
*defaults: A (key, value) list of default values. *defaults: A (key, value) list of default values.
readonly: Whether this config is readonly.
""" """
super().__init__() super().__init__()
self._readonly = readonly
self._ordered_value_cache = None self._ordered_value_cache = None
self.keytype = keytype self.keytype = keytype
self.valtype = valtype self.valtype = valtype
@ -182,6 +191,8 @@ class ValueList(Section):
return self._ordered_value_cache return self._ordered_value_cache
def setv(self, layer, key, value, interpolated): def setv(self, layer, key, value, interpolated):
if self._readonly:
raise ValueError("Trying to modify a read-only config!")
self.keytype.validate(key) self.keytype.validate(key)
if key in self.layers[layer]: if key in self.layers[layer]:
self.layers[layer][key].setv(layer, value, interpolated) self.layers[layer][key].setv(layer, value, interpolated)