Make dump_userconfig work correctly

This commit is contained in:
Florian Bruhin 2014-04-17 12:35:46 +02:00
parent c6df785c41
commit 3728381643
3 changed files with 30 additions and 20 deletions

1
TODO
View File

@ -1,7 +1,6 @@
Bugs Bugs
==== ====
dump_userconfig dumps everything for ValueList sections
All kind of FIXMEs All kind of FIXMEs
Weird font rendering Weird font rendering
https://bugreports.qt-project.org/browse/QTBUG-20973 https://bugreports.qt-project.org/browse/QTBUG-20973

View File

@ -346,17 +346,10 @@ class Config(QObject):
# FIXME adopt this for layering # FIXME adopt this for layering
lines = [] lines = []
for secname, section in self.config.items(): for secname, section in self.config.items():
changed_opts = [] changed = section.dump_userconfig()
for optname, option in section.items(): if changed:
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:
lines.append('[{}]'.format(secname)) lines.append('[{}]'.format(secname))
lines += changed_opts lines += ['{} = {}'.format(k, v) for k, v in changed]
return '\n'.join(lines) return '\n'.join(lines)
def optionxform(self, val): def optionxform(self, val):

View File

@ -93,6 +93,14 @@ class Section:
"""Initialize the values from a configparser section.""" """Initialize the values from a configparser section."""
raise NotImplementedError 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): class KeyValue(Section):
@ -124,6 +132,17 @@ class KeyValue(Section):
logging.debug("'{}' = '{}'".format(k, v)) logging.debug("'{}' = '{}'".format(k, v))
self.values[k].setv('conf', 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): class ValueList(Section):
@ -165,14 +184,6 @@ class ValueList(Section):
self.layers['default']) self.layers['default'])
def setv(self, layer, key, value): 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]: if key in self.layers[layer]:
self.layers[layer][key].setv(layer, value) self.layers[layer][key].setv(layer, value)
else: else:
@ -181,10 +192,17 @@ class ValueList(Section):
self.layers[layer][key] = val self.layers[layer][key] = val
def from_cp(self, sect): def from_cp(self, sect):
"""Initialize the values from a configparser section."""
keytype = self.keytype() keytype = self.keytype()
valtype = self.valtype() valtype = self.valtype()
for k, v in sect.items(): for k, v in sect.items():
keytype.validate(k) keytype.validate(k)
valtype.validate(v) valtype.validate(v)
self.setv('conf', k, 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