From 83da2bf3963db3e4427bced3b4c0681e751e54da Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Thu, 23 Oct 2014 11:45:32 -0400 Subject: 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 Reviewed-by: Patrick Uiterwijk --- ipsilon/admin/common.py | 74 +++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 39 deletions(-) (limited to 'ipsilon/admin') diff --git a/ipsilon/admin/common.py b/ipsilon/admin/common.py index b9dfbf4..3d5313a 100755 --- a/ipsilon/admin/common.py +++ b/ipsilon/admin/common.py @@ -21,6 +21,7 @@ import cherrypy from ipsilon.util.page import Page from ipsilon.util.page import admin_protect from ipsilon.util.plugin import PluginObject +from ipsilon.util import config as pconfig class AdminPage(Page): @@ -46,33 +47,13 @@ class AdminPluginConfig(AdminPage): self.menu = [parent] self.back = parent.url - # Get the defaults - options = po.get_config_desc() - if options is None: - options = dict() - - self.options_order = [] - if hasattr(po, 'conf_opt_order'): - self.options_order = po.conf_opt_order - - # append any undefined options - add = [] - for k in options.keys(): - if k not in self.options_order: - add.append(k) - if len(add): - add.sort() - for k in add: - self.options_order.append(k) - def root_with_msg(self, message=None, message_type=None): return self._template('admin/plugin_config.html', title=self.title, menu=self.menu, action=self.url, back=self.back, message=message, message_type=message_type, name='admin_%s_%s_form' % (self.facility, self._po.name), - options_order=self.options_order, - plugin=self._po) + config=self._po.get_config_obj()) @admin_protect def GET(self, *args, **kwargs): @@ -83,31 +64,46 @@ class AdminPluginConfig(AdminPage): message = "Nothing was modified." message_type = "info" - new_values = dict() - - # Get the defaults - options = self._po.get_config_desc() - if options is None: - options = dict() - - for key, value in kwargs.iteritems(): - if key in options: - if value != self._po.get_config_value(key): - cherrypy.log.error("Storing [%s]: %s = %s" % - (self._po.name, key, value)) - new_values[key] = value - - if len(new_values) != 0: + new_db_values = dict() + + conf = self._po.get_config_obj() + + for name, option in conf.iteritems(): + if name in kwargs: + value = kwargs[name] + if isinstance(option, pconfig.List): + value = [x.strip() for x in value.split('\n')] + elif isinstance(option, pconfig.Condition): + value = True + else: + if isinstance(option, pconfig.Condition): + value = False + elif isinstance(option, pconfig.Choice): + value = list() + for a in option.get_allowed(): + aname = '%s_%s' % (name, a) + if aname in kwargs: + value.append(a) + else: + continue + + if value != option.get_value(): + cherrypy.log.error("Storing [%s]: %s = %s" % + (self._po.name, name, value)) + option.set_value(value) + new_db_values[name] = option.export_value() + + if len(new_db_values) != 0: # First we try to save in the database try: - self._po.save_plugin_config(self.facility, new_values) + self._po.save_plugin_config(self.facility, new_db_values) message = "New configuration saved." message_type = "success" except Exception: # pylint: disable=broad-except message = "Failed to save data!" message_type = "error" - # And only if it succeeds we change the live object + # Then refresh the actual objects self._po.refresh_plugin_config(self.facility) return self.root_with_msg(message=message, @@ -210,7 +206,7 @@ class AdminPlugins(AdminPage): po.name = "global" globalconf = dict() globalconf['order'] = ','.join(names) - po.set_config(globalconf) + po.import_config(globalconf) po.save_plugin_config(self.facility) def reorder_plugins(self, names): -- cgit