From 7ee222af883cb5c211185e01ac73ac58a9566848 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 30 Jun 2017 19:43:51 +0200 Subject: [PATCH] Return [] for none-values for configtypes.List --- qutebrowser/browser/adblock.py | 15 ++++---------- qutebrowser/config/configdata.yml | 2 +- qutebrowser/config/configtypes.py | 28 +++++++++++++++++++-------- qutebrowser/misc/keyhintwidget.py | 4 +--- tests/unit/config/test_configtypes.py | 2 +- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py index eff0a430a..a57622e76 100644 --- a/qutebrowser/browser/adblock.py +++ b/qutebrowser/browser/adblock.py @@ -67,11 +67,7 @@ def is_whitelisted_host(host): Args: host: The host of the request as string. """ - whitelist = config.val.content.host_blocking.whitelist - if whitelist is None: - return False - - for pattern in whitelist: + for pattern in config.val.content.host_blocking.whitelist: if fnmatch.fnmatch(host, pattern.lower()): return True return False @@ -164,7 +160,7 @@ class HostBlocker: if not found: args = objreg.get('args') - if (config.val.content.host_blocking.lists is not None and + if (config.val.content.host_blocking.lists and args.basedir is None and config.val.content.host_blocking.enabled): message.info("Run :adblock-update to get adblock lists.") @@ -180,12 +176,9 @@ class HostBlocker: self._config_blocked_hosts) self._blocked_hosts = set() self._done_count = 0 - urls = config.val.content.host_blocking.lists download_manager = objreg.get('qtnetwork-download-manager', scope='window', window='last-focused') - if urls is None: - return - for url in urls: + for url in config.val.content.host_blocking.lists: if url.scheme() == 'file': try: fileobj = open(url.path(), 'rb') @@ -295,7 +288,7 @@ class HostBlocker: @config.change_filter('content.host_blocking.lists') def _update_files(self): """Update files when the config changed.""" - if config.val.content.host_blocking.lists is None: + if not config.val.content.host_blocking.lists: try: os.remove(self._local_hosts_file) except FileNotFoundError: diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index 37ce89879..fa56cfad6 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -915,7 +915,7 @@ keyhint.blacklist: none_ok: true valtype: name: String - default: null + default: [] desc: >- Keychains that shouldn\'t be shown in the keyhint dialog. diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 74aae361a..d8c689e28 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -388,6 +388,18 @@ class List(BaseType): self.valtype = valtype self.length = length + def _none_value(self, value=None): + """Return the value to be used when the setting is None. + + Args: + value: An existing value to be returned. + """ + if value is None: + return [] + else: + assert value == [], value + return value + def get_name(self): name = super().get_name() if self._show_valtype: @@ -400,7 +412,7 @@ class List(BaseType): def from_str(self, value): self._basic_str_validation(value) if not value: - return None + return self._none_value() try: yaml_val = utils.yaml_load(value) @@ -415,7 +427,7 @@ class List(BaseType): def to_py(self, value): self._basic_py_validation(value, list) if not value: - return None + return self._none_value(value) if self.length is not None and len(value) != self.length: raise configexc.ValidationError(value, "Exactly {} values need to " @@ -452,8 +464,7 @@ class FlagList(List): def to_py(self, value): vals = super().to_py(value) - if vals is not None: - self._check_duplicates(vals) + self._check_duplicates(vals) return vals def complete(self): @@ -1038,7 +1049,7 @@ class Dict(BaseType): """Return the value to be used when the setting is None. Args: - value: An existing value to be mutated when given. + value: An existing value to be returned. """ if self.fixed_keys is None: if value is None: @@ -1179,7 +1190,8 @@ class ShellCommand(List): def from_str(self, value): self._basic_str_validation(value) if not value: - return None + return self._none_value() + try: split_val = shlex.split(value) except ValueError as e: @@ -1191,7 +1203,7 @@ class ShellCommand(List): def to_py(self, value): value = super().to_py(value) if not value: - return None + return value if self.placeholder and '{}' not in ' '.join(value): raise configexc.ValidationError(value, "needs to contain a " @@ -1439,7 +1451,7 @@ class ConfirmQuit(FlagList): def to_py(self, value): values = super().to_py(value) if not values: - return None + return values # Never can't be set with other options if 'never' in values and len(values) > 1: diff --git a/qutebrowser/misc/keyhintwidget.py b/qutebrowser/misc/keyhintwidget.py index 4592c39b2..6d3c6e370 100644 --- a/qutebrowser/misc/keyhintwidget.py +++ b/qutebrowser/misc/keyhintwidget.py @@ -90,11 +90,9 @@ class KeyHintView(QLabel): self.hide() return - blacklist = config.val.keyhint.blacklist or [] - def blacklisted(keychain): return any(fnmatch.fnmatchcase(keychain, glob) - for glob in blacklist) + for glob in config.val.keyhint.blacklist) bindings_dict = config.key_instance.get_bindings_for(modename) if not bindings_dict: diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 97f998081..c34666a4b 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -252,7 +252,7 @@ class TestAll: def test_none_ok_true(self, klass): """Test None and empty string values with none_ok=True.""" typ = klass(none_ok=True) - if isinstance(typ, configtypes.Dict): + if isinstance(typ, (configtypes.Dict, configtypes.List)): expected = typ._none_value() else: expected = None