[Kimchi-devel] [PATCH 1/8] refactor exception: Create a common Exception to translate error messages

Aline Manera alinefm at linux.vnet.ibm.com
Tue Feb 11 17:52:39 UTC 2014


From: Aline Manera <alinefm at br.ibm.com>

KimchiException will be the base for all other Exception types in Kimchi
It will translate the error message before raising it.
Then UI can use the message directly. That way we avoid duplicating messages
on UI and backend.
Also add i18n.py which will contain all the backend messages to be translated.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 plugins/sample/Makefile.am |    1 +
 plugins/sample/__init__.py |    7 ++++++-
 plugins/sample/i18n.py     |   29 +++++++++++++++++++++++++++++
 src/kimchi/Makefile.am     |    1 +
 src/kimchi/exception.py    |   28 ++++++++++++++++++++++++++++
 src/kimchi/i18n.py         |   29 +++++++++++++++++++++++++++++
 src/kimchi/root.py         |    3 +++
 7 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 plugins/sample/i18n.py
 create mode 100644 src/kimchi/i18n.py

diff --git a/plugins/sample/Makefile.am b/plugins/sample/Makefile.am
index 0cb5723..afab703 100644
--- a/plugins/sample/Makefile.am
+++ b/plugins/sample/Makefile.am
@@ -26,4 +26,5 @@ EXTRA_DIST = \
 	__init__.py \
 	API.json \
 	model.py \
+	i18n.py \
 	sample.conf
diff --git a/plugins/sample/__init__.py b/plugins/sample/__init__.py
index a1fe44e..fff9aa7 100644
--- a/plugins/sample/__init__.py
+++ b/plugins/sample/__init__.py
@@ -27,8 +27,10 @@ import os
 from cherrypy import expose
 
 
+from kimchi.config import PluginPaths
 from kimchi.control.base import Collection, Resource
-from model import Model
+from plugins.sample.i18n import messages
+from plugins.sample.model import Model
 
 
 model = Model()
@@ -40,6 +42,9 @@ class Drawings(Resource):
         self.description = Description(model)
         self.rectangles = Rectangles(model)
         self.circles = Circles(model)
+        self.paths = PluginPaths('sample')
+        self.domain = 'sample'
+        self.messages = messages
         self.api_schema = json.load(open(os.path.join(os.path.dirname(
                                     os.path.abspath(__file__)), 'API.json')))
 
diff --git a/plugins/sample/i18n.py b/plugins/sample/i18n.py
new file mode 100644
index 0000000..204460c
--- /dev/null
+++ b/plugins/sample/i18n.py
@@ -0,0 +1,29 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2013
+#
+# Authors:
+#  Aline Manera <alinefm at linux.vnet.ibm.com>
+#
+# 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
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# 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 gettext
+
+_ = gettext.gettext
+
+
+messages = {
+}
diff --git a/src/kimchi/Makefile.am b/src/kimchi/Makefile.am
index 1653c0c..51a496e 100644
--- a/src/kimchi/Makefile.am
+++ b/src/kimchi/Makefile.am
@@ -32,6 +32,7 @@ kimchi_PYTHON = \
 	distroloader.py    \
 	exception.py       \
 	featuretests.py    \
+	i18n.py            \
 	iscsi.py           \
 	isoinfo.py         \
 	mockmodel.py       \
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py
index a37015b..7fcce54 100644
--- a/src/kimchi/exception.py
+++ b/src/kimchi/exception.py
@@ -20,6 +20,34 @@
 # 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
+import gettext
+
+
+from kimchi.template import get_lang, validate_language
+
+
+class KimchiException(Exception):
+    def __init__(self, code='', 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)
+
+        try:
+            translation = gettext.translation(domain, paths.mo_dir, [lang])
+        except:
+            translation = gettext
+
+        for key, value in args.iteritems():
+            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)
+
 
 class NotFoundError(Exception):
     pass
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
new file mode 100644
index 0000000..204460c
--- /dev/null
+++ b/src/kimchi/i18n.py
@@ -0,0 +1,29 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2013
+#
+# Authors:
+#  Aline Manera <alinefm at linux.vnet.ibm.com>
+#
+# 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
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# 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 gettext
+
+_ = gettext.gettext
+
+
+messages = {
+}
diff --git a/src/kimchi/root.py b/src/kimchi/root.py
index ce4a49c..2b5c4b8 100644
--- a/src/kimchi/root.py
+++ b/src/kimchi/root.py
@@ -28,6 +28,7 @@ import os
 
 from kimchi import auth
 from kimchi import template
+from kimchi.i18n import messages
 from kimchi.config import paths
 from kimchi.control import sub_nodes
 from kimchi.control.base import Resource
@@ -95,6 +96,8 @@ class KimchiRoot(Root):
         self.api_schema = json.load(open(os.path.join(paths.src_dir,
                                                       'API.json')))
         self.paths = paths
+        self.domain = 'kimchi'
+        self.messages = messages
 
     @cherrypy.expose
     def login(self, *args):
-- 
1.7.10.4




More information about the Kimchi-devel mailing list