diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-09-24 06:11:46 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-09-24 06:11:46 +0000 |
commit | d56f4c643b486bfbcb6523a0fe80252343fa594e (patch) | |
tree | ccc092fba9df6f8fa82415fd2e5374085764d247 | |
parent | 95abdcd7147399c9bb10adc2a04e41ddc97b2302 (diff) | |
download | freeipa-d56f4c643b486bfbcb6523a0fe80252343fa594e.tar.gz freeipa-d56f4c643b486bfbcb6523a0fe80252343fa594e.tar.xz freeipa-d56f4c643b486bfbcb6523a0fe80252343fa594e.zip |
331: Param.normalize() no longer raises a TypeError when value in not a basestring
-rw-r--r-- | ipalib/frontend.py | 47 | ||||
-rw-r--r-- | ipalib/tests/test_frontend.py | 6 |
2 files changed, 35 insertions, 18 deletions
diff --git a/ipalib/frontend.py b/ipalib/frontend.py index f3264d81..59cdf69f 100644 --- a/ipalib/frontend.py +++ b/ipalib/frontend.py @@ -105,6 +105,38 @@ class Param(plugable.ReadOnly): self.rules = (type_.validate,) + rules lock(self) + def __normalize_scalar(self, value): + if not isinstance(value, basestring): + return value + try: + return self.__normalize(value) + except StandardError: + return value + + def normalize(self, value): + """ + Normalize ``value`` using normalize callback. + + If this `Param` instance does not have a normalize callback, + ``value`` is returned unchanged. + + If this `Param` instance has a normalize callback and ``value`` is + a basestring, the normalize callback is called and its return value + is returned. + + If ``value`` is not a basestring, or if an exception is caught + when calling the normalize callback, ``value`` is returned unchanged. + + :param value: A proposed value for this parameter. + """ + if self.__normalize is None: + return value + if self.multivalue: + 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 __convert_scalar(self, value, index=None): if value is None: raise TypeError('value cannot be None') @@ -124,22 +156,7 @@ class Param(plugable.ReadOnly): return (self.__convert_scalar(value, 0),) # tuple return self.__convert_scalar(value) - def __normalize_scalar(self, value): - if not isinstance(value, basestring): - raise_TypeError(value, basestring, '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) 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): if type(value) is not self.type.type: diff --git a/ipalib/tests/test_frontend.py b/ipalib/tests/test_frontend.py index 88232bed..c473ad58 100644 --- a/ipalib/tests/test_frontend.py +++ b/ipalib/tests/test_frontend.py @@ -202,7 +202,7 @@ class test_Option(ClassChecker): for v in (u'Hello', u'hello', 'Hello'): # Okay assert o.normalize(v) == 'hello' for v in [None, 42, (u'Hello',)]: # Not basestring - check_TypeError(v, basestring, 'value', o.normalize, v) + assert o.normalize(v) is v # Scenario 3: multivalue=True, normalize=None o = self.cls(name, t, multivalue=True) @@ -215,8 +215,8 @@ class test_Option(ClassChecker): for value in [(u'Hello',), (u'hello',), 'Hello', ['Hello']]: # Okay assert o.normalize(value) == (u'hello',) fail = 42 # Not basestring - for v in [fail, [fail], (u'Hello', fail)]: # Non unicode member - check_TypeError(fail, basestring, 'value', o.normalize, v) + for v in [[fail], (u'hello', fail)]: # Non unicode member + assert o.normalize(v) == tuple(v) def test_validate(self): """ |