[Kimchi-devel] [PATCH] Fix exception for model test

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Mon Feb 17 07:44:54 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

We don't start server in model test so cherrypy.request.app is None
and exception translation cannot be used.
Just do translation when server started.

Signed-off-by: ShaoHe Feng<shaohef at linux.vnet.ibm.com>
Signed-off-by: Royce Lv<lvroyce at linux.vnet.ibm.com>
---
 src/kimchi/exception.py | 18 ++++++++++++++----
 src/kimchi/i18n.py      |  2 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py
index 952e243..2d974a8 100644
--- a/src/kimchi/exception.py
+++ b/src/kimchi/exception.py
@@ -24,16 +24,28 @@ import cherrypy
 import gettext
 
 
+from kimchi.i18n import messages as _messages
 from kimchi.template import get_lang, validate_language
 
 
 class KimchiException(Exception):
     def __init__(self, code='', args={}):
+        self.code = code
+
+        if cherrypy.request.app:
+            msg = self._get_translation(args)
+        else:
+            msg = _messages.get(code, code) % args
+
+        pattern = "%s: %s" % (code, msg)
+        Exception.__init__(self, pattern)
+
+    def _get_translation(self, args):
         lang = validate_language(get_lang())
         paths = cherrypy.request.app.root.paths
         domain = cherrypy.request.app.root.domain
         messages = cherrypy.request.app.root.messages
-        text = messages.get(code, code)
+        text = messages.get(self.code, self.code)
 
         try:
             translation = gettext.translation(domain, paths.mo_dir, [lang])
@@ -44,9 +56,7 @@ class KimchiException(Exception):
             if not isinstance(value, unicode):
                 args[key] = unicode(value, 'utf-8')
 
-        msg = unicode(translation.gettext(text), 'utf-8') % args
-        pattern = "%s: %s" % (code, msg)
-        Exception.__init__(self, pattern)
+        return unicode(translation.gettext(text), 'utf-8') % args
 
 
 class NotFoundError(KimchiException):
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index fd61d4e..e9c003e 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -57,7 +57,7 @@ messages = {
     "KCHISO0006E": _("Unexpected volume type for primary volume in ISO %(filename)s"),
     "KCHISO0007E": _("Bad format while reading volume descriptor in ISO %(filename)s"),
 
-    "KCHVM0001E": _("Virtual machine $(name)s already exists"),
+    "KCHVM0001E": _("Virtual machine %(name)s already exists"),
     "KCHVM0002E": _("Virtual machine %(name)s does not exist"),
     "KCHVM0003E": _("Unable to rename virtual machine %(name)s. The name %(new_name)s already exists or it is not powered off."),
     "KCHVM0004E": _("Unable to retrieve screenshot for stopped virtual machine %(name)s"),
-- 
1.8.1.2




More information about the Kimchi-devel mailing list