Make sure the autoconfig.yml is saved periodically

Fixes #2982
This commit is contained in:
Florian Bruhin 2017-09-26 06:41:55 +02:00
parent 38038df703
commit 38449e3e2b
4 changed files with 13 additions and 7 deletions

View File

@ -29,7 +29,7 @@ import configparser
import contextlib import contextlib
import yaml import yaml
from PyQt5.QtCore import QSettings from PyQt5.QtCore import pyqtSignal, QObject, QSettings
import qutebrowser import qutebrowser
from qutebrowser.config import configexc, config, configdata from qutebrowser.config import configexc, config, configdata
@ -72,7 +72,7 @@ class StateConfig(configparser.ConfigParser):
self.write(f) self.write(f)
class YamlConfig: class YamlConfig(QObject):
"""A config stored on disk as YAML file. """A config stored on disk as YAML file.
@ -81,8 +81,10 @@ class YamlConfig:
""" """
VERSION = 1 VERSION = 1
changed = pyqtSignal()
def __init__(self): def __init__(self, parent=None):
super().__init__(parent)
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 = {}
@ -94,12 +96,13 @@ class YamlConfig:
We do this outside of __init__ because the config gets created before We do this outside of __init__ because the config gets created before
the save_manager exists. the save_manager exists.
""" """
save_manager.add_saveable('yaml-config', self._save) save_manager.add_saveable('yaml-config', self._save, self.changed)
def __getitem__(self, name): def __getitem__(self, name):
return self._values[name] return self._values[name]
def __setitem__(self, name, value): def __setitem__(self, name, value):
self.changed.emit()
self._dirty = True self._dirty = True
self._values[name] = value self._values[name] = value

View File

@ -43,6 +43,7 @@ def early_init(args):
config.instance = config.Config(yaml_config=yaml_config) config.instance = config.Config(yaml_config=yaml_config)
config.val = config.ConfigContainer(config.instance) config.val = config.ConfigContainer(config.instance)
config.key_instance = config.KeyConfig(config.instance) config.key_instance = config.KeyConfig(config.instance)
yaml_config.setParent(config.instance)
for cf in config.change_filters: for cf in config.change_filters:
cf.validate() cf.validate()

View File

@ -127,7 +127,7 @@ class TestYaml:
('confirm_quit', True), ('confirm_quit', True),
('confirm_quit', False), ('confirm_quit', False),
]) ])
def test_changed(self, config_tmpdir, old_config, key, value): def test_changed(self, qtbot, config_tmpdir, old_config, key, value):
autoconfig = config_tmpdir / 'autoconfig.yml' autoconfig = config_tmpdir / 'autoconfig.yml'
if old_config is not None: if old_config is not None:
autoconfig.write_text(old_config, 'utf-8') autoconfig.write_text(old_config, 'utf-8')
@ -135,7 +135,9 @@ class TestYaml:
yaml = configfiles.YamlConfig() yaml = configfiles.YamlConfig()
yaml.load() yaml.load()
with qtbot.wait_signal(yaml.changed):
yaml[key] = value yaml[key] = value
assert key in yaml assert key in yaml
assert yaml[key] == value assert yaml[key] == value

View File

@ -183,7 +183,7 @@ def test_late_init(init_patch, monkeypatch, fake_save_manager, fake_args,
fake_save_manager.add_saveable.assert_any_call( fake_save_manager.add_saveable.assert_any_call(
'state-config', unittest.mock.ANY) 'state-config', unittest.mock.ANY)
fake_save_manager.add_saveable.assert_any_call( fake_save_manager.add_saveable.assert_any_call(
'yaml-config', unittest.mock.ANY) 'yaml-config', unittest.mock.ANY, unittest.mock.ANY)
if errors: if errors:
assert len(msgbox_mock.call_args_list) == 1 assert len(msgbox_mock.call_args_list) == 1
_call_posargs, call_kwargs = msgbox_mock.call_args_list[0] _call_posargs, call_kwargs = msgbox_mock.call_args_list[0]