From: Ondrej Lichtner olichtne@redhat.com
ListParam accepts list objects (not other iterables, such as a tuple or string), and accepts an optional type parameter, if this parameter is provided it will be used to type check each individual item in the list. This can be useful for occasions where you want a parameter that is a list of Integers or Strings.
Signed-off-by: Ondrej Lichtner olichtne@redhat.com --- lnst/Common/Parameters.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
diff --git a/lnst/Common/Parameters.py b/lnst/Common/Parameters.py index 00c7832..b139d6c 100644 --- a/lnst/Common/Parameters.py +++ b/lnst/Common/Parameters.py @@ -120,6 +120,24 @@ class DictParam(Param): else: return value
+class ListParam(Param): + def __init__(self, type=None, **kwargs): + self._type = type + super(ListParam, self).__init__(**kwargs) + + def type_check(self, value): + if not isinstance(value, list): + raise ParamError("Value must be a List. Not {}".format(type(value))) + + if self._type is not None: + for item in value: + try: + self._type.type_check(item) + except ParamError as e: + raise ParamError("Value {} failed type check:\n{}" + .format(str(e))) + return value + class Parameters(object): def __init__(self): self._attrs = {}