don't copy values but set dirty
This commit is contained in:
parent
7b192d426e
commit
8db630d358
@ -408,7 +408,7 @@ 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()
|
||||||
for name, value in self._yaml.values.items():
|
for name, value in self._yaml:
|
||||||
self._set_value(self.get_opt(name), value)
|
self._set_value(self.get_opt(name), value)
|
||||||
|
|
||||||
def get_opt(self, name):
|
def get_opt(self, name):
|
||||||
@ -455,7 +455,7 @@ class Config(QObject):
|
|||||||
"""
|
"""
|
||||||
self._set_value(self.get_opt(name), value)
|
self._set_value(self.get_opt(name), value)
|
||||||
if save_yaml:
|
if save_yaml:
|
||||||
self._yaml.values[name] = value
|
self._yaml[name] = value
|
||||||
|
|
||||||
def set_str(self, name, value, *, save_yaml=False):
|
def set_str(self, name, value, *, save_yaml=False):
|
||||||
"""Set the given setting from a string.
|
"""Set the given setting from a string.
|
||||||
@ -469,7 +469,7 @@ class Config(QObject):
|
|||||||
value))
|
value))
|
||||||
self._set_value(opt, converted)
|
self._set_value(opt, converted)
|
||||||
if save_yaml:
|
if save_yaml:
|
||||||
self._yaml.values[name] = converted
|
self._yaml[name] = converted
|
||||||
|
|
||||||
def update_mutables(self, *, save_yaml=False):
|
def update_mutables(self, *, save_yaml=False):
|
||||||
"""Update mutable settings if they changed.
|
"""Update mutable settings if they changed.
|
||||||
|
@ -84,8 +84,8 @@ class YamlConfig:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._filename = os.path.join(standarddir.config(auto=True),
|
self._filename = os.path.join(standarddir.config(auto=True),
|
||||||
'autoconfig.yml')
|
'autoconfig.yml')
|
||||||
self.values = {}
|
self._values = {}
|
||||||
self._initial_values = {}
|
self._changed = None
|
||||||
|
|
||||||
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.
|
||||||
@ -95,12 +95,26 @@ class YamlConfig:
|
|||||||
"""
|
"""
|
||||||
save_manager.add_saveable('yaml-config', self._save)
|
save_manager.add_saveable('yaml-config', self._save)
|
||||||
|
|
||||||
|
def __getitem__(self, name):
|
||||||
|
return self._values[name]
|
||||||
|
|
||||||
|
def __setitem__(self, name, value):
|
||||||
|
if self._values.get(name) != value:
|
||||||
|
self._changed = True
|
||||||
|
self._values[name] = value
|
||||||
|
|
||||||
|
def __contains__(self, name):
|
||||||
|
return name in self._values
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self._values.items())
|
||||||
|
|
||||||
def _save(self):
|
def _save(self):
|
||||||
"""Save the settings to the YAML file if they've changed."""
|
"""Save the settings to the YAML file if they've changed."""
|
||||||
if self.values == self._initial_values:
|
if not self._changed:
|
||||||
return
|
return
|
||||||
|
|
||||||
data = {'config_version': self.VERSION, 'global': self.values}
|
data = {'config_version': self.VERSION, 'global': self._values}
|
||||||
with qtutils.savefile_open(self._filename) as f:
|
with qtutils.savefile_open(self._filename) as f:
|
||||||
f.write(textwrap.dedent("""
|
f.write(textwrap.dedent("""
|
||||||
# DO NOT edit this file by hand, qutebrowser will overwrite it.
|
# DO NOT edit this file by hand, qutebrowser will overwrite it.
|
||||||
@ -110,11 +124,6 @@ class YamlConfig:
|
|||||||
utils.yaml_dump(data, f)
|
utils.yaml_dump(data, f)
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
"""Load self.values from the configured YAML file."""
|
|
||||||
self._initial_values = self._load()
|
|
||||||
self.values = copy.deepcopy(self._initial_values)
|
|
||||||
|
|
||||||
def _load(self):
|
|
||||||
"""Load configuration from the configured YAML file."""
|
"""Load configuration from the configured YAML file."""
|
||||||
try:
|
try:
|
||||||
with open(self._filename, 'r', encoding='utf-8') as f:
|
with open(self._filename, 'r', encoding='utf-8') as f:
|
||||||
@ -146,7 +155,8 @@ class YamlConfig:
|
|||||||
"'global' object is not a dict")
|
"'global' object is not a dict")
|
||||||
raise configexc.ConfigFileErrors('autoconfig.yml', [desc])
|
raise configexc.ConfigFileErrors('autoconfig.yml', [desc])
|
||||||
|
|
||||||
return global_obj
|
self._values = global_obj
|
||||||
|
self._changed = False
|
||||||
|
|
||||||
|
|
||||||
class ConfigAPI:
|
class ConfigAPI:
|
||||||
|
@ -414,12 +414,24 @@ class FakeYamlConfig:
|
|||||||
"""Fake configfiles.YamlConfig object."""
|
"""Fake configfiles.YamlConfig object."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.values = {}
|
|
||||||
self.loaded = False
|
self.loaded = False
|
||||||
|
self._values = {}
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
self.loaded = True
|
self.loaded = True
|
||||||
|
|
||||||
|
def __contains__(self, item):
|
||||||
|
return item in self._values
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self._values.items())
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self._values[key] = value
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return self._values[key]
|
||||||
|
|
||||||
|
|
||||||
class StatusBarCommandStub(QLineEdit):
|
class StatusBarCommandStub(QLineEdit):
|
||||||
|
|
||||||
|
@ -317,9 +317,9 @@ class TestSetConfigCommand:
|
|||||||
assert config_stub.get(option) == new_value
|
assert config_stub.get(option) == new_value
|
||||||
|
|
||||||
if temp:
|
if temp:
|
||||||
assert option not in config_stub._yaml.values
|
assert option not in config_stub._yaml
|
||||||
else:
|
else:
|
||||||
assert config_stub._yaml.values[option] == new_value
|
assert config_stub._yaml[option] == new_value
|
||||||
|
|
||||||
@pytest.mark.parametrize('temp', [True, False])
|
@pytest.mark.parametrize('temp', [True, False])
|
||||||
def test_set_temp_override(self, commands, config_stub, temp):
|
def test_set_temp_override(self, commands, config_stub, temp):
|
||||||
@ -335,7 +335,7 @@ class TestSetConfigCommand:
|
|||||||
commands.set(0, 'url.auto_search', 'never', temp=True)
|
commands.set(0, 'url.auto_search', 'never', temp=True)
|
||||||
|
|
||||||
assert config_stub.val.url.auto_search == 'never'
|
assert config_stub.val.url.auto_search == 'never'
|
||||||
assert config_stub._yaml.values['url.auto_search'] == 'dns'
|
assert config_stub._yaml['url.auto_search'] == 'dns'
|
||||||
|
|
||||||
def test_set_print(self, config_stub, commands, message_mock):
|
def test_set_print(self, config_stub, commands, message_mock):
|
||||||
"""Run ':set -p url.auto_search never'.
|
"""Run ':set -p url.auto_search never'.
|
||||||
@ -357,7 +357,7 @@ class TestSetConfigCommand:
|
|||||||
assert not config_stub.val.auto_save.session
|
assert not config_stub.val.auto_save.session
|
||||||
commands.set(0, 'auto_save.session!')
|
commands.set(0, 'auto_save.session!')
|
||||||
assert config_stub.val.auto_save.session
|
assert config_stub.val.auto_save.session
|
||||||
assert config_stub._yaml.values['auto_save.session']
|
assert config_stub._yaml['auto_save.session']
|
||||||
|
|
||||||
def test_set_toggle_nonbool(self, commands, config_stub):
|
def test_set_toggle_nonbool(self, commands, config_stub):
|
||||||
"""Run ':set url.auto_search!'.
|
"""Run ':set url.auto_search!'.
|
||||||
@ -439,7 +439,7 @@ class TestSetConfigCommand:
|
|||||||
config_stub.set_obj(opt, initial)
|
config_stub.set_obj(opt, initial)
|
||||||
commands.set(0, opt, 'green', 'magenta', 'blue', 'yellow')
|
commands.set(0, opt, 'green', 'magenta', 'blue', 'yellow')
|
||||||
assert config_stub.get(opt) == expected
|
assert config_stub.get(opt) == expected
|
||||||
assert config_stub._yaml.values[opt] == expected
|
assert config_stub._yaml[opt] == expected
|
||||||
|
|
||||||
|
|
||||||
class TestBindConfigCommand:
|
class TestBindConfigCommand:
|
||||||
@ -464,7 +464,7 @@ class TestBindConfigCommand:
|
|||||||
|
|
||||||
commands.bind('a', command)
|
commands.bind('a', command)
|
||||||
assert keyconf.get_command('a', 'normal') == command
|
assert keyconf.get_command('a', 'normal') == command
|
||||||
yaml_bindings = config_stub._yaml.values['bindings.commands']['normal']
|
yaml_bindings = config_stub._yaml['bindings.commands']['normal']
|
||||||
assert yaml_bindings['a'] == command
|
assert yaml_bindings['a'] == command
|
||||||
|
|
||||||
@pytest.mark.parametrize('key, mode, expected', [
|
@pytest.mark.parametrize('key, mode, expected', [
|
||||||
@ -565,7 +565,7 @@ class TestBindConfigCommand:
|
|||||||
commands.unbind(key)
|
commands.unbind(key)
|
||||||
assert keyconf.get_command(key, 'normal') is None
|
assert keyconf.get_command(key, 'normal') is None
|
||||||
|
|
||||||
yaml_bindings = config_stub._yaml.values['bindings.commands']['normal']
|
yaml_bindings = config_stub._yaml['bindings.commands']['normal']
|
||||||
if key in 'bc':
|
if key in 'bc':
|
||||||
# Custom binding
|
# Custom binding
|
||||||
assert normalized not in yaml_bindings
|
assert normalized not in yaml_bindings
|
||||||
@ -612,7 +612,7 @@ class TestConfig:
|
|||||||
|
|
||||||
def test_read_yaml(self, conf):
|
def test_read_yaml(self, conf):
|
||||||
assert not conf._yaml.loaded
|
assert not conf._yaml.loaded
|
||||||
conf._yaml.values['content.plugins'] = True
|
conf._yaml['content.plugins'] = True
|
||||||
|
|
||||||
conf.read_yaml()
|
conf.read_yaml()
|
||||||
|
|
||||||
@ -620,7 +620,7 @@ class TestConfig:
|
|||||||
assert conf._values['content.plugins'] is True
|
assert conf._values['content.plugins'] is True
|
||||||
|
|
||||||
def test_read_yaml_invalid(self, conf):
|
def test_read_yaml_invalid(self, conf):
|
||||||
conf._yaml.values['foo.bar'] = True
|
conf._yaml['foo.bar'] = True
|
||||||
with pytest.raises(configexc.NoOptionError):
|
with pytest.raises(configexc.NoOptionError):
|
||||||
conf.read_yaml()
|
conf.read_yaml()
|
||||||
|
|
||||||
@ -743,9 +743,9 @@ class TestConfig:
|
|||||||
meth(option, value, save_yaml=save_yaml)
|
meth(option, value, save_yaml=save_yaml)
|
||||||
assert conf._values[option] is True
|
assert conf._values[option] is True
|
||||||
if save_yaml:
|
if save_yaml:
|
||||||
assert conf._yaml.values[option] is True
|
assert conf._yaml[option] is True
|
||||||
else:
|
else:
|
||||||
assert option not in conf._yaml.values
|
assert option not in conf._yaml
|
||||||
|
|
||||||
@pytest.mark.parametrize('method', ['set_obj', 'set_str'])
|
@pytest.mark.parametrize('method', ['set_obj', 'set_str'])
|
||||||
def test_set_invalid(self, conf, qtbot, method):
|
def test_set_invalid(self, conf, qtbot, method):
|
||||||
|
@ -68,7 +68,7 @@ def test_yaml_config(fake_save_manager, config_tmpdir, old_config, insert):
|
|||||||
yaml.load()
|
yaml.load()
|
||||||
|
|
||||||
if insert:
|
if insert:
|
||||||
yaml.values['tabs.show'] = 'never'
|
yaml['tabs.show'] = 'never'
|
||||||
|
|
||||||
yaml._save()
|
yaml._save()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user