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

Mark Wu wudxw at linux.vnet.ibm.com
Thu Jan 30 03:30:09 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     | 20 +++++++++++++-------
 src/kimchi/server.py   |  4 ++--
 src/kimchi/template.py |  5 ++---
 3 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/src/kimchi/root.py b/src/kimchi/root.py
index 6e107bc..c3d471e 100644
--- a/src/kimchi/root.py
+++ b/src/kimchi/root.py
@@ -36,7 +36,7 @@ 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',
@@ -49,11 +49,6 @@ class Root(Resource):
             self._cp_config = dict([(key, self.error_development_handler)
                                     for key in self._handled_error])
 
-        for ident, node in sub_nodes.items():
-            setattr(self, ident, node(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}
         res = template.render('error.html', data)
@@ -70,7 +65,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):
@@ -84,6 +79,17 @@ 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'
+        for ident, node in sub_nodes.items():
+            setattr(self, ident, node(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 9884249..6b5925c 100644
--- a/src/kimchi/server.py
+++ b/src/kimchi/server.py
@@ -35,7 +35,7 @@ from kimchi import mockmodel
 from kimchi import vnc
 from kimchi.config import paths, PluginPaths
 from kimchi.control import sub_nodes
-from kimchi.root import Root
+from kimchi.root import KimchiRoot
 from kimchi.utils import get_enabled_plugins, import_class
 
 
@@ -192,7 +192,7 @@ class Server(object):
             if node.url_auth:
                 self.configObj["/%s" % ident] = {'tools.kimchiauth.on': True}
 
-        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 37676b6..173e7c6 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)]
 
@@ -92,6 +90,7 @@ def render(resource, data):
         cherrypy.response.headers['Content-Type'] = 'application/json;charset=utf-8'
         return json.dumps(data, indent=2, separators=(',', ':'))
     elif can_accept_html():
+        paths = cherrypy.request.app.root.paths
         filename = paths.get_template_path(resource)
         try:
             params = {'data': data}
-- 
1.8.4.2




More information about the Kimchi-devel mailing list