[Kimchi-devel] [PATCH 3/3] Allow plugin use kimchi's ui handler

Mark Wu wudxw at linux.vnet.ibm.com
Wed Jan 22 05:48:29 UTC 2014


To make kimchi's error handler and tabs handler resued for plugin,
this patch moves the resource related code into a subclass. With this
change, the plugin can have error handler and tabs handler by
inheriting from this class.

Signed-off-by: Mark Wu <wudxw at linux.vnet.ibm.com>
---
 src/kimchi/root.py     | 37 ++++++++++++++++++++++---------------
 src/kimchi/server.py   |  4 ++--
 src/kimchi/template.py |  5 ++---
 3 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/src/kimchi/root.py b/src/kimchi/root.py
index ae9fd0e..4f30c0d 100644
--- a/src/kimchi/root.py
+++ b/src/kimchi/root.py
@@ -45,7 +45,8 @@ from kimchi.exception import OperationFailed
 
 
 class Root(Resource):
-    def __init__(self, model, dev_env):
+    def __init__(self, model, dev_env=False):
+        super(Root, self).__init__(model)
         self._handled_error = ['error_page.400', 'error_page.404',
                                'error_page.405', 'error_page.406',
                                'error_page.415', 'error_page.500']
@@ -57,19 +58,6 @@ class Root(Resource):
             self._cp_config = dict([(key, self.error_development_handler)
                                     for key in self._handled_error])
 
-        Resource.__init__(self, model)
-        self.vms = VMs(model)
-        self.templates = Templates(model)
-        self.storagepools = StoragePools(model)
-        self.interfaces = Interfaces(model)
-        self.networks = Networks(model)
-        self.tasks = Tasks(model)
-        self.config = Config(model)
-        self.host = Host(model)
-        self.debugreports = DebugReports(model)
-        self.plugins = Plugins(model)
-        self.api_schema = json.load(open(os.path.join(paths.src_dir,
-                                                      'API.json')))
 
     def error_production_handler(self, status, message, traceback, version):
         data = {'code': status, 'reason': message}
@@ -87,7 +75,7 @@ class Root(Resource):
         return res
 
     def get(self):
-        return self.default('kimchi-ui.html')
+        return self.default(self.default_page)
 
     @cherrypy.expose
     def default(self, page, **kwargs):
@@ -101,6 +89,25 @@ class Root(Resource):
             return template.render('tabs/' + page, None)
         raise cherrypy.HTTPError(404)
 
+
+class KimchiRoot(Root):
+    def __init__(self, model, dev_env):
+        super(KimchiRoot, self).__init__(model, dev_env)
+        self.default_page = 'kimchi-ui.html'
+        self.vms = VMs(model)
+        self.templates = Templates(model)
+        self.storagepools = StoragePools(model)
+        self.interfaces = Interfaces(model)
+        self.networks = Networks(model)
+        self.tasks = Tasks(model)
+        self.config = Config(model)
+        self.host = Host(model)
+        self.debugreports = DebugReports(model)
+        self.plugins = Plugins(model)
+        self.api_schema = json.load(open(os.path.join(paths.src_dir,
+                                                      'API.json')))
+        self.paths = paths
+
     @cherrypy.expose
     def login(self, *args):
         params = parse_request()
diff --git a/src/kimchi/server.py b/src/kimchi/server.py
index 58b082b..3c429d3 100644
--- a/src/kimchi/server.py
+++ b/src/kimchi/server.py
@@ -34,7 +34,7 @@ from kimchi import model
 from kimchi import mockmodel
 from kimchi import vnc
 from kimchi.config import paths, plugin_paths
-from kimchi.root import Root
+from kimchi.root import KimchiRoot
 from kimchi.utils import get_enabled_plugins, import_class
 
 
@@ -194,7 +194,7 @@ class Server(object):
             vnc_ws_proxy = vnc.new_ws_proxy()
             cherrypy.engine.subscribe('exit', vnc_ws_proxy.kill)
 
-        self.app = cherrypy.tree.mount(Root(model_instance, dev_env),
+        self.app = cherrypy.tree.mount(KimchiRoot(model_instance, dev_env),
                                        config=self.configObj)
         self._load_plugins()
 
diff --git a/src/kimchi/template.py b/src/kimchi/template.py
index 1740997..0a3b067 100644
--- a/src/kimchi/template.py
+++ b/src/kimchi/template.py
@@ -31,9 +31,6 @@ from Cheetah.Template import Template
 from glob import iglob
 
 
-from kimchi.config import paths
-
-
 def get_lang():
     cookie = cherrypy.request.cookie
     if "kimchiLang" in cookie.keys():
@@ -56,6 +53,7 @@ def get_lang():
 
 
 def get_support_languages():
+    paths = cherrypy.request.app.root.paths
     mopath = "%s/*" % paths.mo_dir
     return [path.rsplit('/', 1)[1] for path in iglob(mopath)]
 
@@ -88,6 +86,7 @@ def can_accept_html():
            can_accept('*/*')
 
 def render(resource, data):
+    paths = cherrypy.request.app.root.paths
     if can_accept('application/json'):
         cherrypy.response.headers['Content-Type'] = 'application/json;charset=utf-8'
         return json.dumps(data, indent=2,
-- 
1.8.4.2




More information about the Kimchi-devel mailing list