Add a complete() method to config types

This commit is contained in:
Florian Bruhin 2014-04-22 15:19:01 +02:00
parent 7758289f8b
commit 1638823fb3
3 changed files with 28 additions and 11 deletions

View File

@ -174,7 +174,7 @@ class Config(QObject):
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 valid_values.show: if valid_values is not None:
if valid_values.descriptions: if valid_values.descriptions:
for val in valid_values: for val in valid_values:
desc = valid_values.descriptions[val] desc = valid_values.descriptions[val]

View File

@ -44,13 +44,11 @@ class ValidValues:
Attributes: Attributes:
values: A list with the allowed untransformed values. values: A list with the allowed untransformed values.
descriptions: A dict with value/desc mappings. descriptions: A dict with value/desc mappings.
show: Whether to show the values in the config or not.
""" """
def __init__(self, *vals, show=True): def __init__(self, *vals):
self.descriptions = {} self.descriptions = {}
self.values = [] self.values = []
self.show = show
for v in vals: for v in vals:
if isinstance(v, str): if isinstance(v, str):
# Value without description # Value without description
@ -119,6 +117,27 @@ class BaseType:
raise NotImplementedError("{} does not implement validate.".format( raise NotImplementedError("{} does not implement validate.".format(
self.__class__.__name__)) self.__class__.__name__))
def complete(self):
"""Return a list of possible values for completion.
The default implementation just returns valid_values, but it might be
useful to override this for special cases.
Return:
A list of (value, description) tuples or None.
"""
if self.valid_values is None:
return None
else:
out = []
for val in self.valid_values:
try:
desc = self.valid_values.descriptions[val]
except KeyError:
desc = ""
out.append((val, desc))
return out
class String(BaseType): class String(BaseType):
@ -155,7 +174,6 @@ class Bool(BaseType):
_BOOLEAN_STATES: A dictionary of strings mapped to their bool meanings. _BOOLEAN_STATES: A dictionary of strings mapped to their bool meanings.
""" """
valid_values = ValidValues('true', 'false', show=False)
typestr = 'bool' typestr = 'bool'
# Taken from configparser # Taken from configparser
@ -169,6 +187,9 @@ class Bool(BaseType):
if value.lower() not in Bool._BOOLEAN_STATES: if value.lower() not in Bool._BOOLEAN_STATES:
raise ValidationError(value, "must be a boolean!") raise ValidationError(value, "must be a boolean!")
def complete(self):
return [('true', ''), ('false', '')]
class Int(BaseType): class Int(BaseType):

View File

@ -62,12 +62,8 @@ class SettingValueCompletionModel(CompletionModel):
def __init__(self, section, option, parent=None): def __init__(self, section, option, parent=None):
super().__init__(parent) super().__init__(parent)
cat = self.new_category("Setting values for {}".format(option)) cat = self.new_category("Setting values for {}".format(option))
vals = configdata.DATA[section][option].typ.valid_values vals = configdata.DATA[section][option].typ.complete()
if vals is None: if vals is None:
raise NoCompletionsError raise NoCompletionsError
for val in vals: for (val, desc) in vals:
try:
desc = vals.descriptions[val]
except KeyError:
desc = ""
self.new_item(cat, val, desc) self.new_item(cat, val, desc)