diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 58359a335..846c75642 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -500,15 +500,17 @@ class ConfigContainer: Attributes: _config: The Config object. _prefix: The __getattr__ chain leading up to this object. + _confpy: If True, get values suitable for config.py. """ - def __init__(self, config, prefix=''): + def __init__(self, config, confpy=False, prefix=''): self._config = config self._prefix = prefix + self._confpy = confpy def __repr__(self): return utils.get_repr(self, constructor=True, config=self._config, - prefix=self._prefix) + confpy=self._confpy, prefix=self._prefix) def __getattr__(self, attr): """Get an option or a new ConfigContainer with the added prefix. @@ -524,9 +526,13 @@ class ConfigContainer: name = self._join(attr) if configdata.is_valid_prefix(name): - return ConfigContainer(config=self._config, prefix=name) + return ConfigContainer(config=self._config, confpy=self._confpy, + prefix=name) - return self._config.get(name) + if self._confpy: + return self._config.get_obj(name) + else: + return self._config.get(name) def __setattr__(self, attr, value): """Set the given option in the config.""" diff --git a/qutebrowser/config/configfiles.py b/qutebrowser/config/configfiles.py index 1ac6668c8..f4ff912ae 100644 --- a/qutebrowser/config/configfiles.py +++ b/qutebrowser/config/configfiles.py @@ -134,7 +134,8 @@ def read_config_py(filename=None): if not os.path.exists(filename): return None - api = ConfigAPI(config.instance, config.key_instance, config.val) + container = config.ConfigContainer(config.instance, confpy=True) + api = ConfigAPI(config.instance, config.key_instance, container) module = types.ModuleType('config') module.config = api module.c = api.val @@ -145,6 +146,8 @@ def read_config_py(filename=None): code = compile(source, filename, 'exec') exec(code, module.__dict__) + config.instance.update_mutables() + return api diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index 9ddf5d373..99a57d479 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -777,8 +777,14 @@ class TestContainer: new_container = new_container.favicons assert new_container._prefix == 'tabs.favicons' - def test_getattr_option(self, container): - assert container.tabs.show == 'always' + @pytest.mark.parametrize('confpy, expected', [ + (True, 'rgb'), + (False, QColor.Rgb), + ]) + def test_getattr_option(self, container, confpy, expected): + container._confpy = confpy + # Use an option with a to_py() so we can check the conversion. + assert container.colors.downloads.system.fg == expected def test_getattr_invalid(self, container): with pytest.raises(configexc.NoOptionError) as excinfo: diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index d077e9918..c598f4acb 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -161,6 +161,11 @@ class TestConfigPy: expected = {'normal': {'o': None}} assert config.instance._values['bindings.commands'] == expected + def test_mutating(self, confpy): + confpy.write('c.aliases["foo"] = "message-info foo"') + configfiles.read_config_py(confpy.filename) + assert config.instance._values['aliases']['foo'] == 'message-info foo' + def test_reading_default_location(self, config_tmpdir): (config_tmpdir / 'config.py').write_text( 'c.colors.hints.bg = "red"', 'utf-8')