mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
333: Param.convert() now uses name Param.__multivalue() helper method as Param.normalize()
This commit is contained in:
@@ -105,16 +105,18 @@ class Param(plugable.ReadOnly):
|
|||||||
self.rules = (type_.validate,) + rules
|
self.rules = (type_.validate,) + rules
|
||||||
lock(self)
|
lock(self)
|
||||||
|
|
||||||
def __if_multivalue(self, value, scalar):
|
def __multivalue(self, value, scalar):
|
||||||
if self.multivalue:
|
if self.multivalue:
|
||||||
if type(value) in (tuple, list):
|
if type(value) in (tuple, list):
|
||||||
if len(value) == 0:
|
if len(value) == 0:
|
||||||
return None
|
return None
|
||||||
return tuple(scalar(v) for v in value)
|
return tuple(
|
||||||
return (scalar(value),) # tuple
|
scalar(v, i) for (i, v) in enumerate(value)
|
||||||
|
)
|
||||||
|
return (scalar(value, 0),) # tuple
|
||||||
return scalar(value)
|
return scalar(value)
|
||||||
|
|
||||||
def __normalize_scalar(self, value):
|
def __normalize_scalar(self, value, index=None):
|
||||||
if not isinstance(value, basestring):
|
if not isinstance(value, basestring):
|
||||||
return value
|
return value
|
||||||
try:
|
try:
|
||||||
@@ -140,7 +142,7 @@ class Param(plugable.ReadOnly):
|
|||||||
"""
|
"""
|
||||||
if self.__normalize is None:
|
if self.__normalize is None:
|
||||||
return value
|
return value
|
||||||
return self.__if_multivalue(value, self.__normalize_scalar)
|
return self.__multivalue(value, self.__normalize_scalar)
|
||||||
|
|
||||||
def __convert_scalar(self, value, index=None):
|
def __convert_scalar(self, value, index=None):
|
||||||
if value is None:
|
if value is None:
|
||||||
@@ -153,13 +155,7 @@ class Param(plugable.ReadOnly):
|
|||||||
return converted
|
return converted
|
||||||
|
|
||||||
def convert(self, value):
|
def convert(self, value):
|
||||||
if self.multivalue:
|
return self.__multivalue(value, self.__convert_scalar)
|
||||||
if type(value) in (tuple, list):
|
|
||||||
return tuple(
|
|
||||||
self.__convert_scalar(v, i) for (i, v) in enumerate(value)
|
|
||||||
)
|
|
||||||
return (self.__convert_scalar(value, 0),) # tuple
|
|
||||||
return self.__convert_scalar(value)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,7 @@ class test_Param(ClassChecker):
|
|||||||
|
|
||||||
# Scenario 2: multivalue=True
|
# Scenario 2: multivalue=True
|
||||||
o = self.cls(name, type_, multivalue=True)
|
o = self.cls(name, type_, multivalue=True)
|
||||||
|
assert o.convert([]) is None
|
||||||
for none in [None, (7, None)]:
|
for none in [None, (7, None)]:
|
||||||
e = raises(TypeError, o.convert, none)
|
e = raises(TypeError, o.convert, none)
|
||||||
assert str(e) == 'value cannot be None'
|
assert str(e) == 'value cannot be None'
|
||||||
|
|||||||
Reference in New Issue
Block a user