diff options
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/__init__.py | 2 | ||||
-rw-r--r-- | ipalib/parameters.py | 50 |
2 files changed, 51 insertions, 1 deletions
diff --git a/ipalib/__init__.py b/ipalib/__init__.py index 76310cad0..b2fc1f857 100644 --- a/ipalib/__init__.py +++ b/ipalib/__init__.py @@ -876,7 +876,7 @@ from backend import Backend from frontend import Command, LocalOrRemote, Application from frontend import Object, Method, Property from crud import Create, Retrieve, Update, Delete, Search -from parameters import DefaultFrom, Bool, Flag, Int, Float, Bytes, Str, Password +from parameters import DefaultFrom, Bool, Flag, Int, Float, Bytes, Str, Password,List from parameters import BytesEnum, StrEnum from errors2 import SkipPluginModule diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 4746e7652..e5f4e8ef1 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -36,6 +36,7 @@ from request import ugettext from plugable import ReadOnly, lock, check_name from errors2 import ConversionError, RequirementError, ValidationError from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR +import csv class DefaultFrom(ReadOnly): @@ -1071,6 +1072,55 @@ class StrEnum(Enum): type = unicode +class List(Param): + """ + Base class for parameters as a list of values. The input is a delimited + string. + """ + 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, + 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): + (name, kw_from_spec) = parse_param_spec(name) + kw.update(kw_from_spec) + kw.update(multivalue=True) + super(List, self).__init__(name, *rules, **kw) + + def normalize(self, value): + if not isinstance(value, tuple): + reader = self.__unicode_csv_reader([value]) + value = [] + for row in reader: + value = value + row + value = tuple(value) + return super(List, self).normalize(value) + + def _convert_scalar(self, value, index=None): + return value + + def _validate_scalar(self, value, index=None): + return + def create_param(spec): """ Create an `Str` instance from the shorthand ``spec``. |