3 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 from ipsilon.util.data import Store
25 from ipsilon.util.log import Log
28 class Plugins(object):
31 self._providers_tree = None
33 def _load_class(self, tree, class_type, file_name):
34 cherrypy.log.error('Check module %s for class %s' % (file_name,
36 name, ext = os.path.splitext(os.path.split(file_name)[-1])
38 if ext.lower() == '.py':
39 mod = imp.load_source(name, file_name)
40 # elif ext.lower() == '.pyc':
41 # mod = imp.load_compiled(name, file_name)
44 except Exception, e: # pylint: disable=broad-except
45 cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
48 if hasattr(mod, class_type):
49 instance = getattr(mod, class_type)()
50 public_name = getattr(instance, 'name', name)
51 tree[public_name] = instance
52 cherrypy.log.error('Added module %s as %s' % (name, public_name))
54 def _load_classes(self, tree, path, class_type):
57 files = os.listdir(path)
58 except Exception, e: # pylint: disable=broad-except
59 cherrypy.log.error('No modules in %s: [%s]' % (path, e))
63 filename = os.path.join(path, name)
64 self._load_class(tree, class_type, filename)
66 def get_plugins(self, path, class_type):
68 self._load_classes(plugins, path, class_type)
72 class PluginLoader(object):
74 def __init__(self, baseobj, facility, plugin_type):
75 (whitelist, config) = Store().get_plugins_config(facility)
76 if cherrypy.config.get('debug', False):
77 cherrypy.log('[%s] %s: %s' % (facility, whitelist, config))
84 (pathname, dummy) = os.path.split(inspect.getfile(baseobj))
87 'available': p.get_plugins(pathname, plugin_type),
88 'whitelist': whitelist,
92 def get_plugin_data(self):
96 class PluginInstaller(object):
97 def __init__(self, baseobj):
98 (pathname, dummy) = os.path.split(inspect.getfile(baseobj))
99 self._pathname = pathname
101 def get_plugins(self):
103 return p.get_plugins(self._pathname, 'Installer')
106 class PluginObject(Log):
114 def get_config_desc(self):
115 """ The configuration description is a dictionary that provides
116 A description of the supported configuration options, as well
117 as the default configuration option values.
118 The key is the option name, the value is an array of 3 elements:
125 def set_config(self, config):
126 self._config = config
128 def get_config_value(self, name):
131 value = self._config.get(name, None)
134 opt = self._options.get(name, None)
138 if cherrypy.config.get('debug', False):
139 cherrypy.log('[%s] %s: %s' % (self.name, name, value))
143 def set_config_value(self, option, value):
145 self._config = dict()
146 self._config[option] = value
148 def get_plugin_config(self, facility):
149 return self._data.get_plugin_config(facility, self.name)
151 def save_plugin_config(self, facility):
152 self._data.save_plugin_config(facility, self.name, self._config)
154 def get_data(self, idval=None, name=None, value=None):
155 return self._data.get_data(self.name, idval=idval, name=name,
158 def save_data(self, data):
159 self._data.save_data(self.name, data)
161 def new_datum(self, datum):
162 self._data.new_datum(self.name, datum)
164 def del_datum(self, idval):
165 self._data.del_datum(self.name, idval)
167 def wipe_config_values(self, facility):
168 self._data.wipe_plugin_config(facility, self.name)
171 self._data.wipe_data(self.name)