From b5cd082e4387689bba6b49ef866b59b9d261e579 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 8 Jun 2015 19:34:11 +0200 Subject: [PATCH] config: Make it possible to migrate values. Needed for #729. --- qutebrowser/config/config.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index e195310ef..503ebc0c7 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -252,6 +252,25 @@ def init(parent=None): _init_misc() +def _get_value_transformer(old, new): + """Get a function which transforms a value for CHANGED_OPTIONS. + + Args: + old: The old value - if the supplied value doesn't match this, it's + returned untransformed. + new: The new value. + + Return: + A function which takes a value and transforms it. + """ + def transformer(val): + if val == old: + return new + else: + return val + return transformer + + class ConfigManager(QObject): """Configuration manager for qutebrowser. @@ -263,6 +282,10 @@ class ConfigManager(QObject): RENAMED_SECTIONS: A mapping of renamed sections, {'oldname': 'newname'} RENAMED_OPTIONS: A mapping of renamed options, {('section', 'oldname'): 'newname'} + CHANGED_OPTIONS: A mapping of arbitrarily changed options, + {('section', 'option'): callable}. + The callable takes the old value and returns the new + one. DELETED_OPTIONS: A (section, option) list of deleted options. Attributes: @@ -304,6 +327,8 @@ class ConfigManager(QObject): ('colors', 'tabs.separator'), ('colors', 'completion.item.bg'), ] + CHANGED_OPTIONS = { + } changed = pyqtSignal(str, str) style_changed = pyqtSignal(str, str) @@ -462,10 +487,15 @@ class ConfigManager(QObject): for k, v in cp[real_sectname].items(): if k.startswith(self.ESCAPE_CHAR): k = k[1:] + if (sectname, k) in self.DELETED_OPTIONS: return - elif (sectname, k) in self.RENAMED_OPTIONS: + if (sectname, k) in self.RENAMED_OPTIONS: k = self.RENAMED_OPTIONS[sectname, k] + if (sectname, k) in self.CHANGED_OPTIONS: + func = self.CHANGED_OPTIONS[(sectname, k)] + v = func(v) + try: self.set('conf', sectname, k, v, validate=False) except configexc.NoOptionError: