Add support for enumNames

This commit is contained in:
Pedro Algarvio 2015-07-03 16:40:48 +01:00
parent 0d9173c57e
commit f84c3e4fbd
2 changed files with 34 additions and 1 deletions

View File

@ -675,8 +675,9 @@ class BaseConfigItem(BaseItem):
title = None title = None
default = None default = None
enum = None enum = None
enumNames = None
def __init__(self, title=None, description=None, default=None, enum=None, **kwargs): def __init__(self, title=None, description=None, default=None, enum=None, enumNames=None, **kwargs):
''' '''
:param required: :param required:
If the configuration item is required. Defaults to ``False``. If the configuration item is required. Defaults to ``False``.
@ -698,6 +699,8 @@ class BaseConfigItem(BaseItem):
self.default = default self.default = default
if enum is not None: if enum is not None:
self.enum = enum self.enum = enum
if enumNames is not None:
self.enumNames = enumNames
super(BaseConfigItem, self).__init__(**kwargs) super(BaseConfigItem, self).__init__(**kwargs)
def __validate_attributes__(self): def __validate_attributes__(self):
@ -709,6 +712,18 @@ class BaseConfigItem(BaseItem):
) )
if not isinstance(self.enum, list): if not isinstance(self.enum, list):
self.enum = list(self.enum) self.enum = list(self.enum)
if self.enumNames is not None:
if not isinstance(self.enumNames, (list, tuple, set)):
raise RuntimeError(
'Only the \'list\', \'tuple\' and \'set\' python types can be used '
'to define \'enumNames\''
)
if len(self.enum) != len(self.enumNames):
raise RuntimeError(
'The size of \'enumNames\' must match the size of \'enum\''
)
if not isinstance(self.enumNames, list):
self.enumNames = list(self.enumNames)
def serialize(self): def serialize(self):
''' '''

View File

@ -151,6 +151,24 @@ class ConfigTestCase(TestCase):
} }
) )
item = config.StringConfig(title='Foo',
description='Foo Item',
min_length=1,
max_length=3,
enum=('foo', 'bar'),
enumNames=('Foo', 'Bar'))
self.assertDictEqual(
item.serialize(), {
'type': 'string',
'title': item.title,
'description': item.description,
'minLength': item.min_length,
'maxLength': item.max_length,
'enum': ['foo', 'bar'],
'enumNames': ['Foo', 'Bar']
}
)
item = config.StringConfig(title='Foo', item = config.StringConfig(title='Foo',
description='Foo Item', description='Foo Item',
pattern=r'^([\w_-]+)$') pattern=r'^([\w_-]+)$')