diff options
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/__init__.py | 2 | ||||
-rw-r--r-- | ipalib/parameters.py | 41 |
2 files changed, 42 insertions, 1 deletions
diff --git a/ipalib/__init__.py b/ipalib/__init__.py index e5aa65d6..8abb9029 100644 --- a/ipalib/__init__.py +++ b/ipalib/__init__.py @@ -875,7 +875,7 @@ from backend import Backend, Context from frontend import Command, LocalOrRemote, Application from frontend import Object, Method, Property from parameters import DefaultFrom, Bool, Flag, Int, Float, Bytes, Str, Password - +from parameters import BytesEnum, StrEnum def create_api(mode='dummy'): """ diff --git a/ipalib/parameters.py b/ipalib/parameters.py index fd693e71..0d764d60 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -898,6 +898,47 @@ class Password(Str): """ +class Enum(Param): + """ + Base class for parameters with enumerable values. + """ + + kwargs = Param.kwargs + ( + ('values', tuple, tuple()), + ) + + def __init__(self, name, *rules, **kw): + super(Enum, self).__init__(name, *rules, **kw) + for (i, v) in enumerate(self.values): + if type(v) is not self.type: + n = '%s values[%d]' % (self.nice, i) + raise TypeError( + TYPE_ERROR % (n, self.type, v, type(v)) + ) + + def _rule_values(self, _, value, **kw): + if value not in self.values: + return _('must be one of %(values)r') % dict( + values=self.values, + ) + + +class BytesEnum(Enum): + """ + Enumerable for binary data (stored in the ``str`` type). + """ + + type = unicode + + +class StrEnum(Enum): + """ + Enumerable for Unicode text (stored in the ``unicode`` type). + """ + + type = unicode + + def create_param(spec): """ Create an `Str` instance from the shorthand ``spec``. |