From 4f665784f2dce1393ab419e2b40056eee121723d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 14 Jun 2018 13:24:06 +0200 Subject: [PATCH] Make it possible to call config.get with fallback=False --- qutebrowser/config/config.py | 25 ++++++++++++++++++++----- tests/unit/config/test_config.py | 12 ++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index d173eb1e0..1c8ce9f0a 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -311,10 +311,19 @@ class Config(QObject): name, deleted=deleted, renamed=renamed) raise exception from None - def get(self, name, url=None): - """Get the given setting converted for Python code.""" + def get(self, name, url=None, *, fallback=True): + """Get the given setting converted for Python code. + + Args: + name: The name of the setting to get. + url: The QUrl to get the setting for. + fallback: If False, return configutils.UNSET when there's no + override for this domain. + """ opt = self.get_opt(name) - obj = self.get_obj(name, url=url) + obj = self.get_obj(name, url=url, fallback=fallback) + if obj is configutils.UNSET: + return obj return opt.typ.to_py(obj) def _maybe_copy(self, value): @@ -328,14 +337,20 @@ class Config(QObject): assert value.__hash__ is not None, value return value - def get_obj(self, name, *, url=None): + def get_obj(self, name, *, url=None, fallback=True): """Get the given setting as object (for YAML/config.py). Note that the returned values are not watched for mutation. If a URL is given, return the value which should be used for that URL. + + Args: + name: The name of the setting to get. + url: The QUrl to get the setting for. + fallback: If False, return configutils.UNSET when there's no + override for this domain. """ self.get_opt(name) # To make sure it exists - value = self._values[name].get_for_url(url) + value = self._values[name].get_for_url(url, fallback=fallback) return self._maybe_copy(value) def get_obj_for_pattern(self, name, *, pattern): diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index e1ef7ef94..ba0658810 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -480,6 +480,18 @@ class TestConfig: conf.set_obj(name, False, pattern=pattern) assert conf.get(name, url=QUrl('https://example.com/')) is False + @pytest.mark.parametrize('fallback', [True, False]) + def test_get_for_url_unset(self, conf, fallback): + """Test config.get() with falling back to a global object.""" + name = 'content.javascript.enabled' + conf.set_obj(name, False) + val = conf.get(name, + url=QUrl('https://example.com/'), + fallback=fallback) + + expected = False if fallback else configutils.UNSET + assert val == expected + @pytest.mark.parametrize('value', [{}, {'normal': {'a': 'nop'}}]) def test_get_bindings(self, config_stub, conf, value): """Test conf.get() with bindings which have missing keys."""