import imp
import cherrypy
import inspect
+from ipsilon.util.config import Config
from ipsilon.util.data import AdminStore
from ipsilon.util.log import 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 set_config(self, config):
+ def import_config(self, config):
self._config = config
- def get_config_value(self, name):
- value = None
- if self._config:
- value = self._config.get(name, None)
- if not value:
- 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
+ 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 = self.export_config()
+
self._data.save_options(facility, self.name, config)
def get_data(self, idval=None, name=None, value=None):
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)