mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
260: Option.normalize() now does same conversion for multivalue as Option.convert() does
This commit is contained in:
parent
c7cd694d4f
commit
915486dadc
@ -26,6 +26,7 @@ import sys
|
|||||||
import code
|
import code
|
||||||
import optparse
|
import optparse
|
||||||
import public
|
import public
|
||||||
|
import errors
|
||||||
|
|
||||||
|
|
||||||
def to_cli(name):
|
def to_cli(name):
|
||||||
@ -161,14 +162,14 @@ class CLI(object):
|
|||||||
while True:
|
while True:
|
||||||
if error is not None:
|
if error is not None:
|
||||||
print '>>> %s: %s' % (option.name, error)
|
print '>>> %s: %s' % (option.name, error)
|
||||||
value = raw_input(prompt)
|
raw = raw_input(prompt)
|
||||||
if default is not None and len(value) == 0:
|
try:
|
||||||
value = default
|
value = option(raw)
|
||||||
if len(value) == 0:
|
if value is not None:
|
||||||
error = 'Must supply a value'
|
kw[option.name] = value
|
||||||
else:
|
|
||||||
kw[option.name] = value
|
|
||||||
break
|
break
|
||||||
|
except errors.ValidationError, e:
|
||||||
|
error = e.error
|
||||||
cmd(*args, **kw)
|
cmd(*args, **kw)
|
||||||
|
|
||||||
|
|
||||||
|
@ -127,15 +127,18 @@ class Option(plugable.ReadOnly):
|
|||||||
def __normalize_scalar(self, value):
|
def __normalize_scalar(self, value):
|
||||||
if not isinstance(value, basestring):
|
if not isinstance(value, basestring):
|
||||||
raise_TypeError(value, basestring, 'value')
|
raise_TypeError(value, basestring, 'value')
|
||||||
return self.__normalize(value)
|
try:
|
||||||
|
return self.__normalize(value)
|
||||||
|
except Exception:
|
||||||
|
return value
|
||||||
|
|
||||||
def normalize(self, value):
|
def normalize(self, value):
|
||||||
if self.__normalize is None:
|
if self.__normalize is None:
|
||||||
return value
|
return value
|
||||||
if self.multivalue:
|
if self.multivalue:
|
||||||
if type(value) is not tuple:
|
if type(value) in (tuple, list):
|
||||||
raise_TypeError(value, tuple, 'value')
|
return tuple(self.__normalize_scalar(v) for v in value)
|
||||||
return tuple(self.__normalize_scalar(v) for v in value)
|
return (self.__normalize_scalar(value),) # tuple
|
||||||
return self.__normalize_scalar(value)
|
return self.__normalize_scalar(value)
|
||||||
|
|
||||||
def __validate_scalar(self, value, index=None):
|
def __validate_scalar(self, value, index=None):
|
||||||
@ -170,8 +173,16 @@ class Option(plugable.ReadOnly):
|
|||||||
return tuple()
|
return tuple()
|
||||||
|
|
||||||
def __call__(self, value, **kw):
|
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):
|
class Command(plugable.Plugin):
|
||||||
|
@ -215,12 +215,10 @@ class test_Option(ClassChecker):
|
|||||||
|
|
||||||
# Scenario 4: multivalue=True, normalize=callback
|
# Scenario 4: multivalue=True, normalize=callback
|
||||||
o = self.cls(name, doc, t, 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',)
|
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
|
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)
|
check_TypeError(fail, basestring, 'value', o.normalize, v)
|
||||||
|
|
||||||
def test_validate(self):
|
def test_validate(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user