[Kimchi-devel] [PATCH V2] [Wok] Implement WokMessage class
Aline Manera
alinefm at linux.vnet.ibm.com
Tue Mar 29 14:39:26 UTC 2016
On 03/24/2016 11:35 AM, Lucio Correia wrote:
> WokMessage is a generic message implementation, which looks
> up and translates error messages for all plugins and Wok
> itself.
>
> Lookup and translation code was adapted from WokException
> class, which now just uses WokMessage.
>
> WokMessage is intended to be used by other modules dealing
> with messages, such as User Request Log and future feature
> Asynchronous Notifications.
>
> Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
> ---
> src/wok/exception.py | 45 +++---------------------------
> src/wok/message.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 83 insertions(+), 41 deletions(-)
> create mode 100644 src/wok/message.py
>
> diff --git a/src/wok/exception.py b/src/wok/exception.py
> index 023334b..52f007e 100644
> --- a/src/wok/exception.py
> +++ b/src/wok/exception.py
> @@ -20,53 +20,16 @@
> # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>
> import cherrypy
> -import gettext
>
> -from wok.i18n import messages as _messages
> -from wok.template import get_lang, validate_language
> +from wok.message import WokMessage
>
>
> class WokException(Exception):
> def __init__(self, code='', args={}):
> self.code = code
> -
> - for key, value in args.iteritems():
> - if isinstance(value, unicode):
> - continue
> -
> - # value is not unicode: convert it
> - try:
> - # In case the value formats itself to an ascii string.
> - args[key] = unicode(str(value), 'utf-8')
> - except UnicodeEncodeError:
> - # In case the value is a WokException or it formats
> - # itself to a unicode string.
> - args[key] = unicode(value)
> -
> - # First, check if it is a Wok error message, then search in plugin
> - # error messages list
> - msg = _messages.get(code, code)
> - if (msg == code) and (cherrypy.request.app):
> - msg = self._get_translation()
> -
> - msg = unicode(msg, 'utf-8') % args
> - pattern = "%s: %s" % (code, msg)
> - cherrypy.log.error_log.error(pattern)
> - Exception.__init__(self, pattern)
> -
> - def _get_translation(self):
> - 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(self.code, self.code)
> -
> - try:
> - translation = gettext.translation(domain, paths.mo_dir, [lang])
> - except:
> - translation = gettext
> -
> - return translation.gettext(text)
> + msg = WokMessage(code, args).get_text()
> + cherrypy.log.error_log.error(msg)
> + Exception.__init__(self, msg)
>
>
> class NotFoundError(WokException):
> diff --git a/src/wok/message.py b/src/wok/message.py
> new file mode 100644
> index 0000000..3167f1f
> --- /dev/null
> +++ b/src/wok/message.py
> @@ -0,0 +1,79 @@
> +#
> +# Project Wok
> +#
> +# 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
> +# 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 cherrypy
> +import gettext
> +
> +from wok.template import get_lang, validate_language
> +
> +
> +class WokMessage(object):
> + def __init__(self, code='', args={}, plugin=None):
> + # make all args unicode
> + for key, value in args.iteritems():
> + if isinstance(value, unicode):
> + continue
> +
> + try:
> + # In case the value formats itself to an ascii string.
> + args[key] = unicode(str(value), 'utf-8')
> + except UnicodeEncodeError:
> + # In case the value is a WokException or it formats
> + # itself to a unicode string.
> + args[key] = unicode(value)
> +
> + self.code = code
> + self.args = args
> + self.plugin = plugin
> +
> + def _get_translation(self):
> + # get app from plugin path if specified
> + if self.plugin:
> + app = cherrypy.tree.apps[self.plugin]
> + # if on request, try to get app from it
> + elif cherrypy.request.app:
> + app = cherrypy.request.app
> + # fallback: get root app (WokRoot)
> + else:
> + app = cherrypy.tree.apps['']
> +
> + # setup translation
> + domain = app.root.domain
> + paths = app.root.paths
> + lang = validate_language(get_lang())
> +
> + try:
> + translation = gettext.translation(domain, paths.mo_dir, [lang])
> + except:
> + translation = gettext
> +
> + # fallback to Wok message in case plugins raise Wok exceptions
> + text = app.root.messages.get(self.code, None)
> + if text is None:
> + wok_messages = cherrypy.tree.apps[''].root.messages
> + text = wok_messages.get(self.code, self.code)
> +
Even though you get the Wok message you are using a different domain to
get the translation.
That way, when a plugin uses a Wok message it will not be translated.
You need to do something like:
wok_app = cherrypy.tree.apps['']
text = app.root.messages.get(self.code, None)
if text is None:
text = wok_app.root.messages(self.code, self.code)
app = wok_app
# do translation
domain = app.root.domain
paths = app.root.paths
lang = validate_language(get_lang())
try:
translation = ...
except:
translation = ...
return translation.gettext(text)
> + return translation.gettext(text)
> +
> + def get_text(self):
> + msg = self._get_translation()
> + msg = unicode(msg, 'utf-8') % self.args
> + return "%s: %s" % (self.code, msg)
More information about the Kimchi-devel
mailing list