Implement ValidValues class

This commit is contained in:
Florian Bruhin 2014-02-28 15:10:34 +01:00
parent 4ea77c7ec2
commit 4f5b6040ad
2 changed files with 51 additions and 23 deletions

View File

@ -113,13 +113,14 @@ class NewConfig:
for descline in desc.splitlines(): for descline in desc.splitlines():
lines += wrapper.wrap(descline) lines += wrapper.wrap(descline)
valid_values = option.typ.valid_values valid_values = option.typ.valid_values
if valid_values is not None and option.typ.show_valid_values: if valid_values is not None and valid_values.show:
if isinstance(valid_values[0], str): if valid_values.descriptions:
for val in valid_values:
desc = valid_values.descriptions[val]
lines += wrapper.wrap(' {}: {}'.format(val, desc))
else:
lines += wrapper.wrap('Valid values: {}'.format(', '.join( lines += wrapper.wrap('Valid values: {}'.format(', '.join(
valid_values))) valid_values)))
else:
for (val, desc) in valid_values:
lines += wrapper.wrap(' {}: {}'.format(val, desc))
lines += wrapper.wrap('Default: {}'.format( lines += wrapper.wrap('Default: {}'.format(
option.default_conf if option.default_conf is not None option.default_conf if option.default_conf is not None
else option.default)) else option.default))

View File

@ -20,23 +20,50 @@
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
class ValidValues:
"""Container for valid values for a given type.
Attributes:
values: A list with the allowed untransformed values.
descriptions: A dict with value/desc mappings.
show: Whether to show the values in the config or not.
"""
def __init__(self, *vals, show=True):
self.descriptions = {}
self.values = []
self.show = show
for v in vals:
if isinstance(v, str):
# Value without description
self.values.append(v)
else:
# (value, description) tuple
self.values.append(v[0])
self.descriptions[v[0]] = v[1]
def __contains__(self, val):
return val in self.values
def __iter__(self):
return self.values.__iter__()
class BaseType: class BaseType:
"""A type used for a setting value. """A type used for a setting value.
Attributes: Attributes:
valid_values: Possible values if they can be expressed as a fixed valid_values: Possible values if they can be expressed as a fixed
string. Either a list of strings, or a list of (value, string. ValidValues instance.
desc) tuples.
# FIXME actually handle tuples and stuff when validating
show_valid_values" Whether to show valid values in config or not.
typestr: The name of the type to appear in the config. typestr: The name of the type to appear in the config.
""" """
typestr = None typestr = None
valid_values = None valid_values = None
show_valid_values = True
def transform(self, value): def transform(self, value):
"""Transform the setting value. """Transform the setting value.
@ -91,8 +118,7 @@ class Bool(BaseType):
"""Base class for a boolean setting.""" """Base class for a boolean setting."""
valid_values = ['true', 'false'] valid_values = ValidValues('true', 'false', show=False)
show_valid_values = False
typestr = 'bool' typestr = 'bool'
# Taken from configparser # Taken from configparser
@ -183,7 +209,7 @@ class Command(BaseType):
typestr = 'command' typestr = 'command'
valid_values = cmdutils.cmd_dict.items() valid_values = ValidValues(*cmdutils.cmd_dict.items())
def validate(self, value): def validate(self, value):
# We need to import this here to avoid circular dependencies # We need to import this here to avoid circular dependencies
@ -249,9 +275,9 @@ class AutoSearch(BaseType):
"""Whether to start a search when something else than an URL is entered.""" """Whether to start a search when something else than an URL is entered."""
valid_values = [("naive", "Use simple/naive check."), valid_values = ValidValues(("naive", "Use simple/naive check."),
("dns", "Use DNS requests (might be slow!)."), ("dns", "Use DNS requests (might be slow!)."),
("false", "Never search automatically.")] ("false", "Never search automatically."))
def validate(self, value): def validate(self, value):
if value.lower() in ["naive", "dns"]: if value.lower() in ["naive", "dns"]:
@ -273,25 +299,26 @@ class Position(String):
"""The position of the tab bar.""" """The position of the tab bar."""
valid_values = ["north", "south", "east", "west"] valid_values = ValidValues("north", "south", "east", "west")
class SelectOnRemove(String): class SelectOnRemove(String):
"""Which tab to select when the focused tab is removed.""" """Which tab to select when the focused tab is removed."""
valid_values = [("left", "Select the tab on the left."), valid_values = ValidValues(
("left", "Select the tab on the left."),
("right", "Select the tab on the right."), ("right", "Select the tab on the right."),
("previous", "Select the previously selected tab.")] ("previous", "Select the previously selected tab."))
class LastClose(String): class LastClose(String):
"""Behaviour when the last tab is closed.""" """Behaviour when the last tab is closed."""
valid_values = [("ignore", "Don't do anything."), valid_values = ValidValues(("ignore", "Don't do anything."),
("blank", "Load about:blank."), ("blank", "Load about:blank."),
("quit", "Quit qutebrowser.")] ("quit", "Quit qutebrowser."))
class KeyBinding(Command): class KeyBinding(Command):