From 3728381643cecdd55c0ad34862f0cb9575052615 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 17 Apr 2014 12:35:46 +0200 Subject: [PATCH] Make dump_userconfig work correctly --- TODO | 1 - qutebrowser/config/config.py | 13 +++--------- qutebrowser/config/sections.py | 36 +++++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/TODO b/TODO index af6f5d04e..cb5c76dd6 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ Bugs ==== -dump_userconfig dumps everything for ValueList sections All kind of FIXMEs Weird font rendering https://bugreports.qt-project.org/browse/QTBUG-20973 diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 7d5a853aa..f344eed53 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -346,17 +346,10 @@ class Config(QObject): # FIXME adopt this for layering lines = [] for secname, section in self.config.items(): - changed_opts = [] - for optname, option in section.items(): - if (option.values['temp'] is not None and - option.values['temp'] != option.values['default'] or - option.values['conf'] is not None and - option.values['conf'] != option.values['default']): - keyval = '{} = {}'.format(optname, option) # FIXME layer? - changed_opts.append(keyval) - if changed_opts: + changed = section.dump_userconfig() + if changed: lines.append('[{}]'.format(secname)) - lines += changed_opts + lines += ['{} = {}'.format(k, v) for k, v in changed] return '\n'.join(lines) def optionxform(self, val): diff --git a/qutebrowser/config/sections.py b/qutebrowser/config/sections.py index e3c85b67a..63b887aab 100644 --- a/qutebrowser/config/sections.py +++ b/qutebrowser/config/sections.py @@ -93,6 +93,14 @@ class Section: """Initialize the values from a configparser section.""" raise NotImplementedError + def dump_userconfig(self): + """Dump the part of the config which was changed by the user. + + Return: + A list of (key, valuestr) tuples. + """ + raise NotImplementedError + class KeyValue(Section): @@ -124,6 +132,17 @@ class KeyValue(Section): logging.debug("'{}' = '{}'".format(k, v)) self.values[k].setv('conf', v) + def dump_userconfig(self): + changed = [] + for k, v in self.items(): + if (v.values['temp'] is not None and + v.values['temp'] != v.values['default']): + changed.append((k, v.values['temp'])) + elif (v.values['conf'] is not None and + v.values['conf'] != v.values['default']): + changed.append((k, v.values['conf'])) + return changed + class ValueList(Section): @@ -165,14 +184,6 @@ class ValueList(Section): self.layers['default']) 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 dict. - key: The key of the element to set. - value: The value to set. - """ if key in self.layers[layer]: self.layers[layer][key].setv(layer, value) else: @@ -181,10 +192,17 @@ class ValueList(Section): self.layers[layer][key] = val def from_cp(self, sect): - """Initialize the values from a configparser section.""" keytype = self.keytype() valtype = self.valtype() for k, v in sect.items(): keytype.validate(k) valtype.validate(v) self.setv('conf', k, v) + + def dump_userconfig(self): + changed = [] + mapping = ChainMap(self.layers['temp'], self.layers['conf']) + for k, v in mapping.items(): + if v.value != self.layers['default'][k].value: + changed.append((k, v.value)) + return changed