From 45e7e35233aefaccac59b0775bba15bf1ae59c74 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 20 Jun 2017 14:55:33 +0200 Subject: [PATCH] Detect mutated values in new config This will allow config.py to get a value and then mutate it, and we can also make things easier for :bind and :unbind. --- qutebrowser/config/config.py | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 326cf4126..ca4df6115 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -19,6 +19,7 @@ """Configuration storage and config-related utilities.""" +import copy import os.path import contextlib import functools @@ -165,25 +166,25 @@ class NewKeyConfig: except cmdexc.PrerequisitesError as e: raise configexc.KeybindingError(str(e)) - bindings = val.bindings.commands + bindings = instance.get_obj('bindings.commands')[mode] log.keyboard.vdebug("Adding binding {} -> {} in mode {}.".format( key, command, mode)) - if key in bindings[mode] and not force: + if key in bindings and not force: raise configexc.DuplicateKeyError(key) - bindings[mode][key] = command - val.bindings.commands = bindings # FIXME:conf + bindings[key] = command + self._manager.update_mutables() def unbind(self, key, *, mode='normal'): """Unbind the given key in the given mode.""" key = self._prepare(key, mode) - bindings = val.bindings.commands + bindings = instance.get_obj('bindings.commands')[mode] try: - del bindings[mode][key] + del bindings[key] except KeyError: raise configexc.KeybindingError("Can't find binding '{}' in section '{}'!" .format(key, mode)) - val.bindings.commands = bindings # FIXME:conf + self._manager.update_mutables() def get_command(self, key, mode): """Get the command for a given key (or None).""" @@ -338,6 +339,7 @@ class NewConfigManager(QObject): super().__init__(parent) self.options = {} self._values = {} # FIXME:conf stub + self._mutables = [] def _changed(self, name, value): self.changed.emit(name) @@ -355,8 +357,19 @@ class NewConfigManager(QObject): def get(self, name): opt = self.get_opt(name) - value = self._values.get(name, opt.default) - return opt.typ.to_py(value) + obj = self.get_obj(name, mutable=False) + return opt.typ.to_py(obj) + + def get_obj(self, name, *, mutable=True): + opt = self.get_opt(name) + obj = self._values.get(name, opt.default) + if isinstance(obj, (dict, list)): + if mutable: + self._mutables.append((name, copy.deepcopy(obj), obj)) + else: + # Shouldn't be mutable (and thus hashable) + assert obj.__hash__ is not None, obj + return obj def get_str(self, name): opt = self.get_opt(name) @@ -376,6 +389,13 @@ class NewConfigManager(QObject): self._values[name] = opt.typ.from_str(value) self._changed(name, value) + def update_mutables(self): + for name, old_value, new_value in self._mutables: + if old_value != new_value: + log.config.debug("{} was mutated, updating".format(name)) + self.set(name, new_value) + self._mutables = [] + def dump_userconfig(self): """Get the part of the config which was changed by the user.