summaryrefslogtreecommitdiffstats
path: root/ipsilon/util
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2014-10-23 11:45:32 -0400
committerPatrick Uiterwijk <puiterwijk@redhat.com>2014-11-12 23:47:15 +0100
commit83da2bf3963db3e4427bced3b4c0681e751e54da (patch)
tree53f03ce8e60d2c68453cdb5fe6be9aad7ce2c362 /ipsilon/util
parent0c14f7600de70baf5b3ee609288207dcdb65e1ae (diff)
downloadipsilon-83da2bf3963db3e4427bced3b4c0681e751e54da.tar.gz
ipsilon-83da2bf3963db3e4427bced3b4c0681e751e54da.tar.xz
ipsilon-83da2bf3963db3e4427bced3b4c0681e751e54da.zip
Refactor plugin configuration
Fork a PluginConfig class out of PluginObject, the base object now supports a simple dictionary config, while using PluginConfig provide access to structured util.config based configuration. Change UI code that deal with plugins configuration to properly use the new structured config objects in order to represent data in appropriate format based on the data type. Use the new util.config objects to represent plugins configuration. Signed-off-by: Simo Sorce <simo@redhat.com> Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Diffstat (limited to 'ipsilon/util')
-rwxr-xr-xipsilon/util/config.py51
-rwxr-xr-xipsilon/util/plugin.py107
2 files changed, 60 insertions, 98 deletions
diff --git a/ipsilon/util/config.py b/ipsilon/util/config.py
index 304c630..94443e3 100755
--- a/ipsilon/util/config.py
+++ b/ipsilon/util/config.py
@@ -165,7 +165,7 @@ class List(Option):
def export_value(self):
if self._assigned_value:
- return ', '.join(self._assigned_value)
+ return ','.join(self._assigned_value)
return None
def import_value(self, value):
@@ -205,19 +205,16 @@ class Choice(Option):
def set_value(self, value):
if type(value) is not list:
value = [value]
+ self._assigned_value = list()
for val in value:
- if type(val) is not tuple:
- val = (val, True)
- if val[0] not in self._allowed_values:
+ if val not in self._allowed_values:
raise ValueError(
- 'Value "%s" not allowed [%s]' % (val[0],
+ 'Value "%s" not allowed [%s]' % (val,
self._allowed_values))
- if val[1] is True:
- if val[0] not in self._assigned_value:
- self._assigned_value.append(val[0])
- else:
- if val[0] in self._assigned_value:
- self._assigned_value.remove(val[0])
+ self._assigned_value.append(val)
+
+ if not self._assigned_value:
+ self._assigned_value = None
def unset_value(self, value):
if type(value) is str:
@@ -236,11 +233,14 @@ class Choice(Option):
def import_value(self, value):
enabled = [x.strip() for x in value.split(',')]
+ if enabled:
+ if self._assigned_value is None:
+ self._assigned_value = list()
for val in enabled:
if val not in self._allowed_values:
# We silently ignore invalid options on import for now
continue
- self._assigned_value[val] = True
+ self._assigned_value.append(val)
class Pick(Option):
@@ -268,30 +268,11 @@ class Pick(Option):
self._str_import_value(value)
-class Condition(Option):
+class Condition(Pick):
def __init__(self, name, description, default_value=False):
- super(Condition, self).__init__(name, description)
- self._default_value = default_value
-
- def set_value(self, value):
- if value is True:
- self._assigned_value = True
- elif value is False:
- self._assigned_value = False
- else:
- raise ValueError('Value must be True or False, got %s' % value)
-
- def export_value(self):
- if self._assigned_value is True:
- return '1'
- elif self._assigned_value is False:
- return '0'
- else:
- return None
+ super(Condition, self).__init__(name, description,
+ [True, False], default_value)
def import_value(self, value):
- if value in ['1', 'YES']:
- self._assigned_value = True
- else:
- self._assigned_value = False
+ self._assigned_value = value == 'True'
diff --git a/ipsilon/util/plugin.py b/ipsilon/util/plugin.py
index 919a28d..f303078 100755
--- a/ipsilon/util/plugin.py
+++ b/ipsilon/util/plugin.py
@@ -21,6 +21,7 @@ import os
import imp
import cherrypy
import inspect
+from ipsilon.util.config import Config
from ipsilon.util.data import AdminStore
from ipsilon.util.log import Log
@@ -112,82 +113,24 @@ class PluginObject(Log):
def __init__(self):
self.name = None
self._config = None
- self._options = None
self._data = AdminStore()
- def get_config_desc(self, name=None):
- """ The configuration description is a dictionary that provides
- A description of the supported configuration options, as well
- as the default configuration option values.
- The key is the option name, the value is an array of 3 elements:
- - description
- - option type
- - default value
- """
- if name is None:
- return self._options
-
- opt = self._options.get(name, None)
- if opt is None:
- 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):
+ def import_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
- if self._config:
- value = self._config.get(name, None)
- if value is None:
- if self._options:
- opt = self._options.get(name, None)
- if opt:
- value = opt[2]
- if cherrypy.config.get('debug', False):
- cherrypy.log('[%s] %s: %s' % (self.name, name, value))
-
- return value
-
- def set_config_value(self, option, value):
- 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 export_config(self):
+ return self._config
def get_plugin_config(self, facility):
return self._data.load_options(facility, self.name)
def refresh_plugin_config(self, facility):
config = self.get_plugin_config(facility)
- self.set_config(config)
+ self.import_config(config)
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)
+ config = self.export_config()
self._data.save_options(facility, self.name, config)
@@ -209,3 +152,41 @@ class PluginObject(Log):
def wipe_data(self):
self._data.wipe_data(self.name)
+
+
+class PluginConfig(Log):
+
+ def __init__(self):
+ self._config = None
+
+ def new_config(self, name, *config_args):
+ self._config = Config(name, *config_args)
+
+ def get_config_obj(self):
+ if self._config is None:
+ raise AttributeError('Config not initialized')
+ return self._config
+
+ def import_config(self, config):
+ if not self._config:
+ raise AttributeError('Config not initialized, cannot import')
+
+ for key, value in config.iteritems():
+ if key in self._config:
+ self._config[key].import_value(str(value))
+
+ def export_config(self):
+ config = dict()
+ for name, option in self._config.iteritems():
+ config[name] = option.export_value()
+ return config
+
+ def get_config_value(self, name):
+ if not self._config:
+ raise AttributeError('Config not initialized')
+ return self._config[name].get_value()
+
+ def set_config_value(self, name, value):
+ if not self._config:
+ raise AttributeError('Config not initialized')
+ return self._config[name].set_value(value)