Make it possible to call config.get with fallback=False

This commit is contained in:
Florian Bruhin 2018-06-14 13:24:06 +02:00
parent 18f4d1d546
commit 4f665784f2
2 changed files with 32 additions and 5 deletions

View File

@ -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):

View File

@ -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."""