Handle lists type options in plugins configuration
authorSimo Sorce <simo@redhat.com>
Tue, 14 Oct 2014 15:57:28 +0000 (11:57 -0400)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Fri, 24 Oct 2014 16:02:20 +0000 (18:02 +0200)
Autodetect and convert config values based on the options definition.
If the option is marked as list split a string on setting the configuration
or join the list into a string before saving it to the database.

Signed-off-by: Simo Sorce <simo@redhat.com>
ipsilon/util/plugin.py
templates/admin/plugin_config.html

index 48edf0e..9222a35 100755 (executable)
@@ -132,8 +132,23 @@ class PluginObject(Log):
             return ''
         return opt[0]
 
+    def _value_to_list(self, name):
+        if name not in self._config:
+            return
+        value = self._config[name]
+        if type(value) is list:
+            return
+        vlist = [x.strip() for x in value.split(',')]
+        self._config[name] = vlist
+
     def set_config(self, config):
         self._config = config
+        if self._config is None:
+            return
+        if self._options:
+            for name, opt in self._options.iteritems():
+                if opt[1] == 'list':
+                    self._value_to_list(name)
 
     def get_config_value(self, name):
         value = None
@@ -154,6 +169,9 @@ class PluginObject(Log):
         if not self._config:
             self._config = dict()
         self._config[option] = value
+        if self._options and option in self._options:
+            if self._options[option][1] == 'list':
+                self._value_to_list(option)
 
     def get_plugin_config(self, facility):
         return self._data.load_options(facility, self.name)
@@ -165,6 +183,12 @@ class PluginObject(Log):
     def save_plugin_config(self, facility, config=None):
         if config is None:
             config = self._config
+        config = config.copy()
+
+        for key, value in config.items():
+            if type(value) is list:
+                config[key] = ','.join(value)
+
         self._data.save_options(facility, self.name, config)
 
     def get_data(self, idval=None, name=None, value=None):
index 70d56f0..e722aa1 100644 (file)
         {% for o in options_order %}
             <div class="form-group">
             <label for="{{ o }}">{{ o }}:</label>
-                <input type="text" class="form-control" name="{{ o }}" value="{{ plugin.get_config_value(o) }}">
+              {% set val = plugin.get_config_value(o) %}
+              {% if val is string %}
+                <input type="text" class="form-control" name="{{ o }}" value="{{ val }}">
+              {% else %}
+                <input type="text" class="form-control" name="{{ o }}" value="{{ val|join(', ') }}">
+              {% endif %}
             </div>
             <span class="help-block">{{ plugin.get_config_desc(o) }}</span>
         {% endfor %}