Return [] for none-values for configtypes.List

This commit is contained in:
Florian Bruhin 2017-06-30 19:43:51 +02:00
parent c141c33b32
commit 7ee222af88
5 changed files with 27 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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