diff options
author | Nathaniel McCallum <npmccallum@redhat.com> | 2013-10-09 10:16:46 +0200 |
---|---|---|
committer | Petr Viktorin <pviktori@redhat.com> | 2013-10-09 18:05:37 +0200 |
commit | 88d003c68b3350185148a5499441c55db3b90398 (patch) | |
tree | d1cc0540cca5314aa9dc3b417e57ed30618bb751 /ipalib | |
parent | 4f6580f11ded1c456e0891023232b0d715d8aef7 (diff) | |
download | freeipa-88d003c68b3350185148a5499441c55db3b90398.tar.gz freeipa-88d003c68b3350185148a5499441c55db3b90398.tar.xz freeipa-88d003c68b3350185148a5499441c55db3b90398.zip |
Add IntEnum parameter to ipalib
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/__init__.py | 2 | ||||
-rw-r--r-- | ipalib/parameters.py | 66 |
2 files changed, 43 insertions, 25 deletions
diff --git a/ipalib/__init__.py b/ipalib/__init__.py index d822ba595..ab89ab77e 100644 --- a/ipalib/__init__.py +++ b/ipalib/__init__.py @@ -886,7 +886,7 @@ from frontend import Command, LocalOrRemote, Updater, Advice from frontend import Object, Method, Property from crud import Create, Retrieve, Update, Delete, Search from parameters import DefaultFrom, Bool, Flag, Int, Decimal, Bytes, Str, IA5Str, Password, DNParam, DeprecatedParam -from parameters import BytesEnum, StrEnum, AccessTime, File +from parameters import BytesEnum, StrEnum, IntEnum, AccessTime, File from errors import SkipPluginModule from text import _, ngettext, GettextFactory, NGettextFactory diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 97e449c29..9472c9006 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -1045,6 +1045,21 @@ class Int(Number): ('maxvalue', (int, long), int(MAXINT)), ) + @staticmethod + def convert_int(value): + if type(value) in Int.allowed_types: + return value + + if type(value) is float: + return int(value) + + if type(value) is unicode: + if u'.' in value: + return int(float(value)) + return int(value, 0) + + raise ValueError(value) + def __init__(self, name, *rules, **kw): super(Int, self).__init__(name, *rules, **kw) @@ -1058,30 +1073,12 @@ class Int(Number): """ Convert a single scalar value. """ - if type(value) in (int, long): - return value - if type(value) is unicode: - # permit floating point strings - if value.find(u'.') >= 0: - try: - return int(float(value)) - except ValueError: - pass - else: - try: - # 2nd arg is radix base, 2nd arg only accepted for strings. - # Zero means determine radix base from prefix (e.g. 0x for hex) - return int(value, 0) - except ValueError: - pass - if type(value) is float: - try: - return int(value) - except ValueError: - pass - raise ConversionError(name=self.get_param_name(), index=index, - error=ugettext(self.type_error), - ) + try: + return Int.convert_int(value) + except ValueError: + raise ConversionError(name=self.get_param_name(), + index=index, + error=ugettext(self.type_error)) def _rule_minvalue(self, _, value): """ @@ -1549,6 +1546,27 @@ class StrEnum(Enum): type = unicode +class IntEnum(Enum): + """ + Enumerable for integer data (stored in the ``int`` type). + """ + + type = int + allowed_types = int, long + type_error = Int.type_error + + def _convert_scalar(self, value, index=None): + """ + Convert a single scalar value. + """ + try: + return Int.convert_int(value) + except ValueError: + raise ConversionError(name=self.get_param_name(), + index=index, + error=ugettext(self.type_error)) + + class Any(Param): """ A parameter capable of holding values of any type. For internal use only. |