X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=ipsilon%2Futil%2Fplugin.py;h=978e4701f143cb1c8569bea8cb34ec4a4484b3dc;hb=ba59365931a4e35226b3d9be216d867ff1549846;hp=063767c6163b825c0aee96737aa32d37cfc4e371;hpb=b7b80c5c0fc1895e85aae3acbfcbbc593a42697f;p=cascardo%2Fipsilon.git diff --git a/ipsilon/util/plugin.py b/ipsilon/util/plugin.py old mode 100755 new mode 100644 index 063767c..978e470 --- a/ipsilon/util/plugin.py +++ b/ipsilon/util/plugin.py @@ -1,28 +1,11 @@ -#!/usr/bin/python -# -# Copyright (C) 2013 Simo Sorce -# -# see file 'COPYING' for use and warranty information -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +# Copyright (C) 2013 Ipsilon project Contributors, for license see COPYING import os import imp import cherrypy import inspect -from ipsilon.util.config import Config -from ipsilon.util.data import AdminStore +import logging +from ipsilon.util.data import AdminStore, Store from ipsilon.util.log import Log @@ -33,7 +16,7 @@ class Plugins(object): def _load_class(self, tree, class_type, file_name, *pargs): cherrypy.log.error('Check module %s for class %s' % (file_name, - class_type)) + class_type), severity=logging.DEBUG) name, ext = os.path.splitext(os.path.split(file_name)[-1]) try: if ext.lower() == '.py': @@ -43,21 +26,24 @@ class Plugins(object): else: return except Exception, e: # pylint: disable=broad-except - cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e)) + cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e), + severity=logging.ERROR) return if hasattr(mod, class_type): instance = getattr(mod, class_type)(*pargs) public_name = getattr(instance, 'name', name) tree[public_name] = instance - cherrypy.log.error('Added module %s as %s' % (name, public_name)) + cherrypy.log.error('Added module %s as %s' % (name, public_name), + severity=logging.DEBUG) def _load_classes(self, tree, path, class_type, *pargs): files = None try: files = os.listdir(path) except Exception, e: # pylint: disable=broad-except - cherrypy.log.error('No modules in %s: [%s]' % (path, e)) + cherrypy.log.error('No modules in %s: [%s]' % (path, e), + severity=logging.ERROR) return for name in files: @@ -89,6 +75,10 @@ class PluginLoader(Log): self.__data = AdminStore() return self.__data + @property + def is_readonly(self): + return self._data.is_readonly + def get_plugins(self): p = Plugins() return p.get_plugins(self._pathname, self._plugin_type, self) @@ -129,6 +119,10 @@ class PluginObject(Log): self._plugins = plugins self.is_enabled = False + @property + def is_readonly(self): + return self._data.is_readonly + def on_enable(self): return @@ -152,7 +146,14 @@ class PluginObject(Log): return self.refresh_plugin_config() - self.on_enable() + is_upgrade = Store._is_upgrade # pylint: disable=protected-access + try: + Store._is_upgrade = True # pylint: disable=protected-access + self.on_enable() + for store in self.used_datastores(): + store.upgrade_database() + finally: + Store._is_upgrade = is_upgrade # pylint: disable=protected-access self.is_enabled = True self.debug('Plugin enabled: %s' % self.name) @@ -165,6 +166,9 @@ class PluginObject(Log): self.is_enabled = False self.debug('Plugin disabled: %s' % self.name) + def used_datastores(self): + return [] + def import_config(self, config): self._config = config @@ -177,7 +181,11 @@ class PluginObject(Log): def refresh_plugin_config(self): config = self.get_plugin_config() if config: - self.import_config(config) + try: + self.import_config(config) + except Exception, e: # pylint: disable=broad-except + self.error('Failed to refresh config for %s (%s)' % + (self.name, e)) def save_plugin_config(self, config=None): if config is None: @@ -203,41 +211,3 @@ 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)