diff options
author | Simo Sorce <simo@redhat.com> | 2015-02-16 10:14:33 -0500 |
---|---|---|
committer | Patrick Uiterwijk <puiterwijk@redhat.com> | 2015-02-24 16:37:45 +0100 |
commit | 7aa8e0744f50e4f94a58b318fa4bfb43f4128a12 (patch) | |
tree | d1ba58670f0638c4bd99160162a822629bf6dc36 /ipsilon/util/config.py | |
parent | e577dd94ace3f664c0ba0966af270781ae4990ec (diff) | |
download | ipsilon-7aa8e0744f50e4f94a58b318fa4bfb43f4128a12.tar.gz ipsilon-7aa8e0744f50e4f94a58b318fa4bfb43f4128a12.tar.xz ipsilon-7aa8e0744f50e4f94a58b318fa4bfb43f4128a12.zip |
Add config option to load mapping lists
This requires careful handling, and should be used sparingly
Signed-off-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Diffstat (limited to 'ipsilon/util/config.py')
-rw-r--r-- | ipsilon/util/config.py | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/ipsilon/util/config.py b/ipsilon/util/config.py index ee5c39c..947c697 100644 --- a/ipsilon/util/config.py +++ b/ipsilon/util/config.py @@ -1,6 +1,7 @@ # Copyright (C) 2014 Ipsilon project Contributors, for licensee see COPYING from ipsilon.util.log import Log +import json class Config(Log): @@ -156,7 +157,7 @@ class List(Option): def __init__(self, name, description, default_list=None): super(List, self).__init__(name, description) if default_list: - self._default_value = list(default_list) + self._default_value = default_list else: self._default_value = [] @@ -174,6 +175,51 @@ class List(Option): self._assigned_value = [x.strip() for x in value.split(',')] +class ComplexList(List): + + def _check_value(self, value): + if type(value) is not list: + raise ValueError('The value type must be a list, not "%s"' % + type(value)) + + def set_value(self, value): + self._check_value(value) + self._assigned_value = value + + def export_value(self): + if self._assigned_value: + return json.dumps(self._assigned_value) + return None + + def import_value(self, value): + if type(value) is not str: + raise ValueError('The value type must be a string, not "%s"' % + type(value)) + jsonval = json.loads(value) + self.set_value(jsonval) + + +class MappingList(ComplexList): + + def _check_value(self, value): + if type(value) is not list: + raise ValueError('The value type must be a list, not "%s"' % + type(value)) + for v in value: + if type(v) is not list: + raise ValueError('Each element must be a list, not "%s"' % + type(v)) + if len(v) != 2: + raise ValueError('Each element must contain 2 values,' + ' not %d' % len(v)) + + def import_value(self, value): + if type(value) is not str: + raise ValueError('Value (type: %s) must be string' % type(value)) + jsonval = json.loads(value) + self.set_value(jsonval) + + class Choice(Option): def __init__(self, name, description, allowed=None, default=None): |