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

View File

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

View File

@ -58,13 +58,28 @@ class Values:
all ScopedValues for the given setting.
Attributes:
_opt: The Option being customized.
opt: The Option being customized.
"""
def __init__(self, opt):
self._opt = opt
self.opt = opt
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):
"""Yield ScopedValue elements.
@ -107,7 +122,7 @@ class Values:
# default for a given URL.
return scoped.value
return self._opt.default
return self.opt.default
def get_for_url(self, url=None, *, fallback=True):
"""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):
"""Update settings customized for the given tab."""
for opt, values in config.instance:
if opt.name not in mappings:
for values in config.instance:
if values.opt.name not in mappings:
continue
# 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)
# FIXME:conf have a proper API for this.