[Kimchi-devel] [PATCH v2][Wok 3/8] Implements control/model of new plugin API

Rodrigo Trujillo rodrigo.trujillo at linux.vnet.ibm.com
Mon Jun 6 19:13:31 UTC 2016


Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
---
 src/wok/control/plugins.py | 31 ++++++++++++++++++++++++++++---
 src/wok/i18n.py            |  3 +++
 src/wok/model/plugins.py   | 38 ++++++++++++++++++++++++++++++--------
 3 files changed, 61 insertions(+), 11 deletions(-)

diff --git a/src/wok/control/plugins.py b/src/wok/control/plugins.py
index 57dfa1b..5c125eb 100644
--- a/src/wok/control/plugins.py
+++ b/src/wok/control/plugins.py
@@ -19,11 +19,36 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
-from wok.control.base import SimpleCollection
+from wok.control.base import Collection, Resource, SimpleCollection
 from wok.control.utils import UrlSubNode
 
 
- at UrlSubNode("plugins")
-class Plugins(SimpleCollection):
+ at UrlSubNode("plugins", False)
+class PluginsSimple(SimpleCollection):
+    """
+    Returns only enabled plugins in order to built initial Wok UI
+    """
+    def __init__(self, model):
+        super(PluginsSimple, self).__init__(model)
+        pass
+
+
+ at UrlSubNode("pluginsmanager", True)
+class Plugins(Collection):
     def __init__(self, model):
         super(Plugins, self).__init__(model)
+        self.resource = Plugin
+        self.admin_methods = ['GET']
+
+
+class Plugin(Resource):
+    def __init__(self, model, id):
+        super(Plugin, self).__init__(model, id)
+        self.uri_fmt = "/pluginsmanager/%s"
+        self.enable = self.generate_action_handler('enable')
+        self.disable = self.generate_action_handler('disable')
+        self.admin_methods = ['GET', 'POST']
+
+    @property
+    def data(self):
+        return self.info
diff --git a/src/wok/i18n.py b/src/wok/i18n.py
index 4fc21f1..bda3949 100644
--- a/src/wok/i18n.py
+++ b/src/wok/i18n.py
@@ -60,4 +60,7 @@ messages = {
     "WOKRES0001L": _("Request made on resource"),
     "WOKROOT0001L": _("User '%(username)s' logged in"),
     "WOKROOT0002L": _("User '%(username)s' logged out"),
+
+    "WOKPLUG0001E": _("Plugin '%(plugin_name)s' not found."),
+    "WOKPLUG0002E": _("Error updating configuration file of plugin '%(plugin)s'."),
 }
diff --git a/src/wok/model/plugins.py b/src/wok/model/plugins.py
index 4beff44..b0ac366 100644
--- a/src/wok/model/plugins.py
+++ b/src/wok/model/plugins.py
@@ -3,8 +3,6 @@
 #
 # Copyright IBM Corp, 2015-2016
 #
-# Code derived from Project Kimchi
-#
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
@@ -19,9 +17,17 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
-import cherrypy
 
-from wok.utils import get_enabled_plugins
+from wok.exception import NotFoundError
+from wok.pluginsmanager import Plugins
+
+
+class PluginsSimpleModel(object):
+    def __init__(self, **kargs):
+        pass
+
+    def get_list(self):
+        return Plugins().get_enabled_plugins()
 
 
 class PluginsModel(object):
@@ -29,7 +35,23 @@ class PluginsModel(object):
         pass
 
     def get_list(self):
-        # Will only return plugins that were loaded correctly by WOK and are
-        # properly configured in cherrypy
-        return [plugin for (plugin, config) in get_enabled_plugins()
-                if config.get('wok').get('uri') in cherrypy.tree.apps.keys()]
+        return Plugins().get_all_plugins_names()
+
+
+class PluginModel(object):
+    def __init__(self, **kargs):
+        pass
+
+    def lookup(self, plugin):
+        info = Plugins().get_plugin_info(plugin)
+        if not info:
+            raise NotFoundError('WOKPLUG0001E', {'plugin_name': plugin})
+
+        return {'name': plugin,
+                'enabled': info['enabled']}
+
+    def enable(self, plugin_name):
+        Plugins().enable_plugin(plugin_name)
+
+    def disable(self, plugin_name):
+        Plugins().disable_plugin(plugin_name)
-- 
2.1.0




More information about the Kimchi-devel mailing list