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():
lines += wrapper.wrap(descline)
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:
for val in valid_values:
desc = valid_values.descriptions[val]

View File

@ -44,13 +44,11 @@ class ValidValues:
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):
def __init__(self, *vals):
self.descriptions = {}
self.values = []
self.show = show
for v in vals:
if isinstance(v, str):
# Value without description
@ -119,6 +117,27 @@ class BaseType:
raise NotImplementedError("{} does not implement validate.".format(
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):
@ -155,7 +174,6 @@ class Bool(BaseType):
_BOOLEAN_STATES: A dictionary of strings mapped to their bool meanings.
"""
valid_values = ValidValues('true', 'false', show=False)
typestr = 'bool'
# Taken from configparser
@ -169,6 +187,9 @@ class Bool(BaseType):
if value.lower() not in Bool._BOOLEAN_STATES:
raise ValidationError(value, "must be a boolean!")
def complete(self):
return [('true', ''), ('false', '')]
class Int(BaseType):

View File

@ -62,12 +62,8 @@ class SettingValueCompletionModel(CompletionModel):
def __init__(self, section, option, parent=None):
super().__init__(parent)
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:
raise NoCompletionsError
for val in vals:
try:
desc = vals.descriptions[val]
except KeyError:
desc = ""
for (val, desc) in vals:
self.new_item(cat, val, desc)