1 # Copyright (C) 2013 Ipsilon project Contributors, for license see COPYING
8 from ipsilon.util.data import AdminStore, Store
9 from ipsilon.util.log import Log
12 class Plugins(object):
15 self._providers_tree = None
17 def _load_class(self, tree, class_type, file_name, *pargs):
18 cherrypy.log.error('Check module %s for class %s' % (file_name,
19 class_type), severity=logging.DEBUG)
20 name, ext = os.path.splitext(os.path.split(file_name)[-1])
22 if ext.lower() == '.py':
23 mod = imp.load_source(name, file_name)
24 # elif ext.lower() == '.pyc':
25 # mod = imp.load_compiled(name, file_name)
28 except Exception, e: # pylint: disable=broad-except
29 cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e),
30 severity=logging.ERROR)
33 if hasattr(mod, class_type):
34 instance = getattr(mod, class_type)(*pargs)
35 public_name = getattr(instance, 'name', name)
36 tree[public_name] = instance
37 cherrypy.log.error('Added module %s as %s' % (name, public_name),
38 severity=logging.DEBUG)
40 def _load_classes(self, tree, path, class_type, *pargs):
43 files = os.listdir(path)
44 except Exception, e: # pylint: disable=broad-except
45 cherrypy.log.error('No modules in %s: [%s]' % (path, e),
46 severity=logging.ERROR)
50 filename = os.path.join(path, name)
51 self._load_class(tree, class_type, filename, *pargs)
53 def get_plugins(self, path, class_type, *pargs):
55 self._load_classes(plugins, path, class_type, *pargs)
59 class PluginLoader(Log):
61 def __init__(self, baseobj, facility, plugin_type):
62 self._pathname, _ = os.path.split(inspect.getfile(baseobj))
63 self.facility = facility
64 self._plugin_type = plugin_type
65 self.available = dict()
69 # Defer initialization or instantiating the store will fail at load
70 # time when used with Installer plugins as the cherrypy config context
71 # is created after all Installer plugins are loaded.
75 self.__data = AdminStore()
79 def is_readonly(self):
80 return self._data.is_readonly
82 def get_plugins(self):
84 return p.get_plugins(self._pathname, self._plugin_type, self)
86 def refresh_enabled(self):
87 config = self._data.load_options(self.facility, name='global')
90 if 'enabled' in config:
91 self.enabled = config['enabled'].split(',')
93 def get_plugin_data(self):
94 self.available = self.get_plugins()
95 self.refresh_enabled()
97 def save_enabled(self, enabled):
99 self._data.save_options(self.facility, 'global',
100 {'enabled': ','.join(enabled)})
102 self._data.delete_options(self.facility, 'global',
104 self.debug('Plugin enabled state saved: %s' % enabled)
105 self.refresh_enabled()
108 class PluginInstaller(PluginLoader):
109 def __init__(self, baseobj, facility):
110 super(PluginInstaller, self).__init__(baseobj, facility, 'Installer')
113 class PluginObject(Log):
115 def __init__(self, plugins):
118 self._data = AdminStore()
119 self._plugins = plugins
120 self.is_enabled = False
123 def is_readonly(self):
124 return self._data.is_readonly
129 def on_disable(self):
132 def save_enabled_state(self):
134 self._plugins.refresh_enabled()
135 enabled.extend(self._plugins.enabled)
137 if self.name not in enabled:
138 enabled.append(self.name)
140 if self.name in enabled:
141 enabled.remove(self.name)
142 self._plugins.save_enabled(enabled)
148 self.refresh_plugin_config()
149 is_upgrade = Store._is_upgrade # pylint: disable=protected-access
151 Store._is_upgrade = True # pylint: disable=protected-access
153 for store in self.used_datastores():
154 store.upgrade_database()
156 Store._is_upgrade = is_upgrade # pylint: disable=protected-access
157 self.is_enabled = True
158 self.debug('Plugin enabled: %s' % self.name)
161 if not self.is_enabled:
166 self.is_enabled = False
167 self.debug('Plugin disabled: %s' % self.name)
169 def used_datastores(self):
172 def import_config(self, config):
173 self._config = config
175 def export_config(self):
178 def get_plugin_config(self):
179 return self._data.load_options(self._plugins.facility, self.name)
181 def refresh_plugin_config(self):
182 config = self.get_plugin_config()
185 self.import_config(config)
186 except Exception, e: # pylint: disable=broad-except
187 self.error('Failed to refresh config for %s (%s)' %
190 def save_plugin_config(self, config=None):
192 config = self.export_config()
194 self._data.save_options(self._plugins.facility, self.name, config)
196 def get_data(self, idval=None, name=None, value=None):
197 return self._data.get_data(self.name, idval=idval, name=name,
200 def save_data(self, data):
201 self._data.save_data(self.name, data)
203 def new_datum(self, datum):
204 self._data.new_datum(self.name, datum)
206 def del_datum(self, idval):
207 self._data.del_datum(self.name, idval)
209 def wipe_config_values(self):
210 self._data.delete_options(self._plugins.facility, self.name, None)
213 self._data.wipe_data(self.name)