Change how iterating over Config/YamlConfig works

This commit is contained in:
Florian Bruhin 2018-02-19 06:35:14 +01:00
parent 6abb42a066
commit 8504ad6ff3
4 changed files with 40 additions and 29 deletions

View File

@ -259,9 +259,8 @@ class Config(QObject):
self._values[name] = configutils.Values(opt) self._values[name] = configutils.Values(opt)
def __iter__(self): def __iter__(self):
"""Iterate over Option, configutils.Values tuples.""" """Iterate over configutils.Values items."""
for name, values in sorted(self._values.items()): yield from self._values.values()
yield self.get_opt(name), values
def init_save_manager(self, save_manager): def init_save_manager(self, save_manager):
"""Make sure the config gets saved properly. """Make sure the config gets saved properly.
@ -287,9 +286,10 @@ class Config(QObject):
def read_yaml(self): def read_yaml(self):
"""Read the YAML settings from self._yaml.""" """Read the YAML settings from self._yaml."""
self._yaml.load() self._yaml.load()
# FIXME:conf implement in self._yaml for values in self._yaml:
for pattern, name, value in self._yaml: for scoped in values:
self._set_value(self.get_opt(name), value, pattern=pattern) self._set_value(values.opt, scoped.value,
pattern=scoped.pattern)
def get_opt(self, name): def get_opt(self, name):
"""Get a configdata.Option object for the given setting.""" """Get a configdata.Option object for the given setting."""
@ -443,17 +443,14 @@ class Config(QObject):
Return: Return:
The changed config part as string. The changed config part as string.
""" """
lines = [] blocks = []
for pattern, opt, value in self: for values in self:
str_value = opt.typ.to_str(value) if values:
if pattern is None: blocks.append(str(values))
lines.append('{} = {}'.format(opt.name, str_value))
else: if not blocks:
lines.append('{}: {} = {}'.format(pattern, opt.name,
str_value))
if not lines:
lines = ['<Default configuration>'] lines = ['<Default configuration>']
return '\n'.join(lines) return '\n'.join(blocks)
class ConfigContainer: class ConfigContainer:

View File

@ -102,8 +102,8 @@ class YamlConfig(QObject):
save_manager.add_saveable('yaml-config', self._save, self.changed) save_manager.add_saveable('yaml-config', self._save, self.changed)
def __iter__(self): def __iter__(self):
for _name, values in sorted(self._values.items()): """Iterate over configutils.Values items."""
yield from values yield from self._values.values()
def _mark_changed(self): def _mark_changed(self):
"""Mark the YAML config as changed.""" """Mark the YAML config as changed."""
@ -183,7 +183,7 @@ class YamlConfig(QObject):
# FIXME:conf test this # FIXME:conf test this
for name, yaml_values in settings_obj.items(): for name, yaml_values in settings_obj.items():
values = configutils.Values(self._config.get_opt(name)) values = configutils.Values(configdata.DATA[name])
if 'global' in yaml_values: if 'global' in yaml_values:
values.add(yaml_values.pop('global')) values.add(yaml_values.pop('global'))
@ -224,10 +224,9 @@ class YamlConfig(QObject):
def _validate(self): def _validate(self):
"""Make sure all settings exist.""" """Make sure all settings exist."""
unknown = [] unknown = []
for _pattern, name, _value in self: for values in self:
# FIXME:conf show pattern if values.opt.name not in configdata.DATA:
if name not in configdata.DATA: unknown.append(values.opt.name)
unknown.append(name)
if unknown: if unknown:
errors = [configexc.ConfigErrorDesc("While loading options", errors = [configexc.ConfigErrorDesc("While loading options",

View File

@ -58,13 +58,28 @@ class Values:
all ScopedValues for the given setting. all ScopedValues for the given setting.
Attributes: Attributes:
_opt: The Option being customized. opt: The Option being customized.
""" """
def __init__(self, opt): def __init__(self, opt):
self._opt = opt self.opt = opt
self._values = [] self._values = []
def __str__(self):
"""Get the values as human-readable string."""
if not self:
return '{}: <unchanged>'.format(self.opt.name)
lines = []
for scoped in self._values:
str_value = self.opt.typ.to_str(scoped.value)
if scoped.pattern is None:
lines.append('{} = {}'.format(self.opt.name, str_value))
else:
lines.append('{}: {} = {}'.format(
scoped.pattern, self.opt.name, str_value))
return '\n'.join(lines)
def __iter__(self): def __iter__(self):
"""Yield ScopedValue elements. """Yield ScopedValue elements.
@ -107,7 +122,7 @@ class Values:
# default for a given URL. # default for a given URL.
return scoped.value return scoped.value
return self._opt.default return self.opt.default
def get_for_url(self, url=None, *, fallback=True): def get_for_url(self, url=None, *, fallback=True):
"""Get a config value, falling back when needed. """Get a config value, falling back when needed.

View File

@ -222,12 +222,12 @@ def update_mappings(mappings, option):
def update_for_tab(mappings, tab, url): def update_for_tab(mappings, tab, url):
"""Update settings customized for the given tab.""" """Update settings customized for the given tab."""
for opt, values in config.instance: for values in config.instance:
if opt.name not in mappings: if values.opt.name not in mappings:
continue continue
# FIXME:conf handle settings != None with global/static setters # FIXME:conf handle settings != None with global/static setters
mapping = mappings[opt.name] mapping = mappings[values.opt.name]
value = values.get_for_url(url, fallback=False) value = values.get_for_url(url, fallback=False)
# FIXME:conf have a proper API for this. # FIXME:conf have a proper API for this.