Make it possible to use PluginLoader without store
[cascardo/ipsilon.git] / ipsilon / util / plugin.py
index be6dd2f..87ab1ae 100644 (file)
@@ -1,25 +1,11 @@
-# Copyright (C) 2013  Simo Sorce <simo@redhat.com>
-#
-# 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 <http://www.gnu.org/licenses/>.
+# Copyright (C) 2013 Ipsilon project Contributors, for license see COPYING
 
 import os
 import imp
 import cherrypy
 import inspect
 
 import os
 import imp
 import cherrypy
 import inspect
-from ipsilon.util.data import AdminStore
+import logging
+from ipsilon.util.data import AdminStore, Store
 from ipsilon.util.log import Log
 
 
 from ipsilon.util.log import Log
 
 
@@ -30,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,
 
     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':
         name, ext = os.path.splitext(os.path.split(file_name)[-1])
         try:
             if ext.lower() == '.py':
@@ -40,21 +26,24 @@ class Plugins(object):
             else:
                 return
         except Exception, e:  # pylint: disable=broad-except
             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
             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
 
     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:
             return
 
         for name in files:
@@ -69,19 +58,23 @@ class Plugins(object):
 
 class PluginLoader(Log):
 
 
 class PluginLoader(Log):
 
-    def __init__(self, baseobj, facility, plugin_type):
+    def __init__(self, baseobj, facility, plugin_type, uses_store=True):
         self._pathname, _ = os.path.split(inspect.getfile(baseobj))
         self.facility = facility
         self._plugin_type = plugin_type
         self.available = dict()
         self.enabled = list()
         self._pathname, _ = os.path.split(inspect.getfile(baseobj))
         self.facility = facility
         self._plugin_type = plugin_type
         self.available = dict()
         self.enabled = list()
-        self.__data = None
+        self.__data = False
+        self.uses_store = uses_store
 
     # Defer initialization or instantiating the store will fail at load
     # time when used with Installer plugins as the cherrypy config context
     # is created after all Installer plugins are loaded.
     @property
     def _data(self):
 
     # Defer initialization or instantiating the store will fail at load
     # time when used with Installer plugins as the cherrypy config context
     # is created after all Installer plugins are loaded.
     @property
     def _data(self):
+        if not self.uses_store:
+            raise Exception('Tried to get plugin data while ' +
+                            'uses_store=False (%s)' % self.facility)
         if not self.__data:
             self.__data = AdminStore()
         return self.__data
         if not self.__data:
             self.__data = AdminStore()
         return self.__data
@@ -103,7 +96,8 @@ class PluginLoader(Log):
 
     def get_plugin_data(self):
         self.available = self.get_plugins()
 
     def get_plugin_data(self):
         self.available = self.get_plugins()
-        self.refresh_enabled()
+        if self.uses_store:
+            self.refresh_enabled()
 
     def save_enabled(self, enabled):
         if enabled:
 
     def save_enabled(self, enabled):
         if enabled:
@@ -157,7 +151,15 @@ class PluginObject(Log):
             return
 
         self.refresh_plugin_config()
             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()
+            self._data.create_plugin_data_table(self.name)
+            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)
 
         self.is_enabled = True
         self.debug('Plugin enabled: %s' % self.name)
 
@@ -170,6 +172,9 @@ class PluginObject(Log):
         self.is_enabled = False
         self.debug('Plugin disabled: %s' % self.name)
 
         self.is_enabled = False
         self.debug('Plugin disabled: %s' % self.name)
 
+    def used_datastores(self):
+        return []
+
     def import_config(self, config):
         self._config = config
 
     def import_config(self, config):
         self._config = config