summaryrefslogtreecommitdiffstats
path: root/ipalib/parameters.py
diff options
context:
space:
mode:
authorJan Cholasta <jcholast@redhat.com>2011-11-21 10:50:27 -0500
committerMartin Kosek <mkosek@redhat.com>2011-11-30 17:08:35 +0100
commit135ccf89de866ea2cdda96993ed2743394e1e716 (patch)
tree1b3c7bef4d5653255b75014218d1d0be9b10d5bb /ipalib/parameters.py
parent2ac9d4816a85822825257e16f4fcf74e15a8ea02 (diff)
downloadfreeipa-135ccf89de866ea2cdda96993ed2743394e1e716.tar.gz
freeipa-135ccf89de866ea2cdda96993ed2743394e1e716.tar.xz
freeipa-135ccf89de866ea2cdda96993ed2743394e1e716.zip
Parse comma-separated lists of values in all parameter types. This can be enabled for a specific parameter by setting the "csv" option to True.
Remove "List" parameter type and replace all occurences of it with appropriate multi-valued parameter ("Str" in most cases) with csv enabled. Add new parameter type "Any", capable of holding values of any type. This is needed by the "batch" command, as "Str" is not suitable type for the "methods" parameter. ticket 2007
Diffstat (limited to 'ipalib/parameters.py')
-rw-r--r--ipalib/parameters.py82
1 files changed, 38 insertions, 44 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index 96dde7fc2..287304d3b 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -381,6 +381,9 @@ class Param(ReadOnly):
('hint', (str, Gettext), None),
('alwaysask', bool, False),
('sortorder', int, 2), # see finalize()
+ ('csv', bool, False),
+ ('csv_separator', str, ','),
+ ('csv_skipspace', bool, True),
# The 'default' kwarg gets appended in Param.__init__():
# ('default', self.type, None),
@@ -493,6 +496,10 @@ class Param(ReadOnly):
)
)
+ # Check that if csv is set, multivalue is set too
+ if self.csv and not self.multivalue:
+ raise ValueError('%s: cannot have csv without multivalue' % self.nice)
+
# Check that all the rules are callable
self.class_rules = tuple(class_rules)
self.rules = rules
@@ -663,6 +670,23 @@ class Param(ReadOnly):
kw.update(overrides)
return klass(name, *self.rules, **kw)
+ # The following 2 functions were taken from the Python
+ # documentation at http://docs.python.org/library/csv.html
+ def __utf_8_encoder(self, unicode_csv_data):
+ for line in unicode_csv_data:
+ yield line.encode('utf-8')
+
+ def __unicode_csv_reader(self, unicode_csv_data, dialect=csv.excel, **kwargs):
+ # csv.py doesn't do Unicode; encode temporarily as UTF-8:
+ csv_reader = csv.reader(self.__utf_8_encoder(unicode_csv_data),
+ dialect=dialect,
+ delimiter=self.csv_separator, escapechar='\\',
+ skipinitialspace=self.csv_skipspace,
+ **kwargs)
+ for row in csv_reader:
+ # decode UTF-8 back to Unicode, cell by cell:
+ yield [unicode(cell, 'utf-8') for cell in row]
+
def normalize(self, value):
"""
Normalize ``value`` using normalizer callback.
@@ -686,15 +710,20 @@ class Param(ReadOnly):
:param value: A proposed value for this parameter.
"""
+ if self.multivalue:
+ if self.csv and isinstance(value, basestring):
+ csvreader = self.__unicode_csv_reader([unicode(value)])
+ value = tuple(csvreader.next()) #pylint: disable=E1101
+ elif type(value) not in (tuple, list):
+ value = (value,)
if self.normalizer 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),) # Return a tuple
- return self._normalize_scalar(value)
+ return tuple(
+ self._normalize_scalar(v) for v in value
+ )
+ else:
+ return self._normalize_scalar(value)
def _normalize_scalar(self, value):
"""
@@ -1525,47 +1554,12 @@ class StrEnum(Enum):
type = unicode
-class List(Param):
+class Any(Param):
"""
- Base class for parameters as a list of values. The input is a delimited
- string.
+ A parameter capable of holding values of any type. For internal use only.
"""
- type = tuple
- kwargs = Param.kwargs + (
- ('separator', str, ','),
- ('skipspace', bool, True),
- )
-
- # The following 2 functions were taken from the Python
- # documentation at http://docs.python.org/library/csv.html
- def __utf_8_encoder(self, unicode_csv_data):
- for line in unicode_csv_data:
- yield line.encode('utf-8')
-
- def __unicode_csv_reader(self, unicode_csv_data, dialect=csv.excel, **kwargs):
- # csv.py doesn't do Unicode; encode temporarily as UTF-8:
- csv_reader = csv.reader(self.__utf_8_encoder(unicode_csv_data),
- dialect=dialect,
- delimiter=self.separator, escapechar='\\',
- skipinitialspace=self.skipspace,
- **kwargs)
- for row in csv_reader:
- # decode UTF-8 back to Unicode, cell by cell:
- yield [unicode(cell, 'utf-8') for cell in row]
-
- def __init__(self, name, *rules, **kw):
- kw['multivalue'] = True
- super(List, self).__init__(name, *rules, **kw)
-
- def normalize(self, value):
- if value and not type(value) in (list, tuple):
- reader = self.__unicode_csv_reader([value])
- value = []
- for row in reader:
- value = value + row
- value = tuple(value)
- return super(List, self).normalize(value)
+ type = object
def _convert_scalar(self, value, index=None):
return value