[Kimchi-devel] [PATCH][Wok 3/5] Changes server behavior to use pluginsmanager

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Tue May 31 17:51:36 UTC 2016


Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
---
 src/wok/server.py | 32 +++++++++++++++++---------------
 src/wok/utils.py  | 36 +++---------------------------------
 2 files changed, 20 insertions(+), 48 deletions(-)

diff --git a/src/wok/server.py b/src/wok/server.py
index 902d4bf..d9f6ec5 100644
--- a/src/wok/server.py
+++ b/src/wok/server.py
@@ -21,6 +21,7 @@
 #
 
 import cherrypy
+import copy
 import logging
 import logging.handlers
 import os
@@ -30,14 +31,15 @@ from string import Template
 from wok import auth
 from wok import config
 from wok.config import config as configParser
-from wok.config import paths, PluginConfig, WokConfig
+from wok.config import paths, WokConfig
 from wok.control import sub_nodes
 from wok.model import model
+from wok.pluginsmanager import Plugins
 from wok.proxy import start_proxy, terminate_proxy
 from wok.reqlogger import RequestLogger
 from wok.root import WokRoot
 from wok.safewatchedfilehandler import SafeWatchedFileHandler
-from wok.utils import get_enabled_plugins, import_class
+from wok.utils import import_class
 
 
 LOGGING_LEVEL = {"debug": logging.DEBUG,
@@ -171,6 +173,9 @@ class Server(object):
 
         self.app = cherrypy.tree.mount(WokRoot(model_instance, dev_env),
                                        config=self.configObj)
+
+        # Initiate and handles plugins
+        self.plugins = Plugins()
         self._load_plugins(options)
 
         # Terminate proxy when cherrypy server is terminated
@@ -179,18 +184,15 @@ class Server(object):
         cherrypy.lib.sessions.init()
 
     def _load_plugins(self, options):
-        for plugin_name, plugin_config in get_enabled_plugins():
-            try:
-                plugin_class = ('plugins.%s.%s' %
-                                (plugin_name,
-                                 plugin_name[0].upper() + plugin_name[1:]))
-                script_name = plugin_config['wok']['uri']
-                del plugin_config['wok']
-
-                plugin_config.update(PluginConfig(plugin_name))
-            except KeyError:
+        plugins_info = self.plugins.get_all_plugins_info()
+        for plugin_name in plugins_info:
+            if not plugins_info[plugin_name]['enabled']:
                 continue
 
+            plugin_class = plugins_info[plugin_name]['plugin_class']
+            script_name = plugins_info[plugin_name]['uri']
+            plugin_cfg = copy.deepcopy(plugins_info[plugin_name]['config'])
+
             try:
                 plugin_app = import_class(plugin_class)(options)
             except ImportError, e:
@@ -203,7 +205,7 @@ class Server(object):
             # dynamically extend plugin config with custom data, if provided
             get_custom_conf = getattr(plugin_app, "get_custom_conf", None)
             if get_custom_conf is not None:
-                plugin_config.update(get_custom_conf())
+                plugin_cfg.update(get_custom_conf())
 
             # dynamically add tools.wokauth.on = True to extra plugin APIs
             try:
@@ -216,7 +218,7 @@ class Server(object):
                         ident = "/%s" % ident
                         urlSubNodes[ident] = {'tools.wokauth.on': True}
 
-                    plugin_config.update(urlSubNodes)
+                    plugin_cfg.update(urlSubNodes)
 
             except ImportError, e:
                 cherrypy.log.error_log.error(
@@ -224,7 +226,7 @@ class Server(object):
                     "error: %s" % (plugin_class, e.message)
                 )
 
-            cherrypy.tree.mount(plugin_app, script_name, plugin_config)
+            cherrypy.tree.mount(plugin_app, script_name, plugin_cfg)
 
     def start(self):
         # Subscribe to SignalHandler plugin
diff --git a/src/wok/utils.py b/src/wok/utils.py
index 9a29084..e46e234 100644
--- a/src/wok/utils.py
+++ b/src/wok/utils.py
@@ -35,14 +35,14 @@ import traceback
 import xml.etree.ElementTree as ET
 import locale
 
-from cherrypy.lib.reprconf import Parser
 from datetime import datetime, timedelta
 from multiprocessing import Process, Queue
 from threading import Timer
 
 from wok.asynctask import AsyncTask
-from wok.config import paths, PluginPaths
+from wok.config import PluginPaths
 from wok.exception import InvalidParameter, TimeoutExpired
+from wok.pluginsmanager import Plugins
 
 
 wok_log = cherrypy.log.error_log
@@ -76,40 +76,10 @@ def is_digit(value):
         return False
 
 
-def _load_plugin_conf(name):
-    plugin_conf = PluginPaths(name).conf_file
-    if not os.path.exists(plugin_conf):
-        cherrypy.log.error_log.error("Plugin configuration file %s"
-                                     " doesn't exist." % plugin_conf)
-        return
-    try:
-        return Parser().dict_from_file(plugin_conf)
-    except ValueError as e:
-        cherrypy.log.error_log.error("Failed to load plugin "
-                                     "conf from %s: %s" %
-                                     (plugin_conf, e.message))
-
-
-def get_enabled_plugins():
-    plugin_dir = paths.plugins_dir
-    try:
-        dir_contents = os.listdir(plugin_dir)
-    except OSError:
-        return
-    for name in dir_contents:
-        if os.path.isdir(os.path.join(plugin_dir, name)):
-            plugin_config = _load_plugin_conf(name)
-            try:
-                if plugin_config['wok']['enable']:
-                    yield (name, plugin_config)
-            except (TypeError, KeyError):
-                continue
-
-
 def get_all_tabs():
     files = []
 
-    for plugin, _ in get_enabled_plugins():
+    for plugin in Plugins().get_enabled_plugins():
         files.append(os.path.join(PluginPaths(plugin).ui_dir,
                      'config/tab-ext.xml'))
 
-- 
2.1.0




More information about the Kimchi-devel mailing list