diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-09-04 08:16:12 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-09-04 08:16:12 +0000 |
commit | 915486dadc476df4915cefdfeb8d61c43664ca60 (patch) | |
tree | 17621ba4e43f600be52f8044c8ba8ca5bfd7548a /ipalib | |
parent | c7cd694d4f307e65f8e4cc5fb2e724e5f9700dea (diff) | |
download | freeipa-915486dadc476df4915cefdfeb8d61c43664ca60.tar.gz freeipa-915486dadc476df4915cefdfeb8d61c43664ca60.tar.xz freeipa-915486dadc476df4915cefdfeb8d61c43664ca60.zip |
260: Option.normalize() now does same conversion for multivalue as Option.convert() does
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/cli.py | 15 | ||||
-rw-r--r-- | ipalib/public.py | 23 | ||||
-rw-r--r-- | ipalib/tests/test_public.py | 6 |
3 files changed, 27 insertions, 17 deletions
diff --git a/ipalib/cli.py b/ipalib/cli.py index a495924ef..d199f7211 100644 --- a/ipalib/cli.py +++ b/ipalib/cli.py @@ -26,6 +26,7 @@ import sys import code import optparse import public +import errors def to_cli(name): @@ -161,14 +162,14 @@ class CLI(object): while True: if error is not None: print '>>> %s: %s' % (option.name, error) - value = raw_input(prompt) - if default is not None and len(value) == 0: - value = default - if len(value) == 0: - error = 'Must supply a value' - else: - kw[option.name] = value + raw = raw_input(prompt) + try: + value = option(raw) + if value is not None: + kw[option.name] = value break + except errors.ValidationError, e: + error = e.error cmd(*args, **kw) diff --git a/ipalib/public.py b/ipalib/public.py index cc385da13..d1f2ff358 100644 --- a/ipalib/public.py +++ b/ipalib/public.py @@ -127,15 +127,18 @@ class Option(plugable.ReadOnly): def __normalize_scalar(self, value): if not isinstance(value, basestring): raise_TypeError(value, basestring, 'value') - return self.__normalize(value) + try: + return self.__normalize(value) + except Exception: + return value def normalize(self, value): if self.__normalize is None: return value if self.multivalue: - if type(value) is not tuple: - raise_TypeError(value, tuple, 'value') - return tuple(self.__normalize_scalar(v) for v in value) + if type(value) in (tuple, list): + return tuple(self.__normalize_scalar(v) for v in value) + return (self.__normalize_scalar(value),) # tuple return self.__normalize_scalar(value) def __validate_scalar(self, value, index=None): @@ -170,8 +173,16 @@ class Option(plugable.ReadOnly): return tuple() def __call__(self, value, **kw): - pass - + if value in ('', tuple(), []): + value = None + if value is None: + value = self.get_default(**kw) + if value is None: + if self.required: + raise RequirementError(option.name) + return None + else: + pass class Command(plugable.Plugin): diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py index 6adc393f6..6cdfc5afb 100644 --- a/ipalib/tests/test_public.py +++ b/ipalib/tests/test_public.py @@ -215,12 +215,10 @@ class test_Option(ClassChecker): # Scenario 4: multivalue=True, normalize=callback o = self.cls(name, doc, t, multivalue=True, normalize=callback) - for value in [(u'Hello',), (u'hello',)]: # Okay + for value in [(u'Hello',), (u'hello',), 'Hello', ['Hello']]: # Okay assert o.normalize(value) == (u'hello',) - for v in (None, u'Hello', [u'hello']): # Not tuple - check_TypeError(v, tuple, 'value', o.normalize, v) fail = 42 # Not basestring - for v in [(fail,), (u'Hello', fail)]: # Non unicode member + for v in [fail, [fail], (u'Hello', fail)]: # Non unicode member check_TypeError(fail, basestring, 'value', o.normalize, v) def test_validate(self): |