[Kimchi-devel] [PATCH] [Wok] Implement WokMessage class
Jose Ricardo Ziviani
joserz at linux.vnet.ibm.com
Wed Mar 23 20:10:57 UTC 2016
Reviewed by: Jose Ricardo Ziviani <joserz at linux.vnet.ibm.com>
On 23-03-2016 16:08, 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 | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 77 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..e982fba
> --- /dev/null
> +++ b/src/wok/message.py
> @@ -0,0 +1,73 @@
> +#
> +# 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['']
> +
> + domain = app.root.domain
> + paths = app.root.paths
> + text = app.root.messages.get(self.code, self.code)
> + lang = validate_language(get_lang())
> +
> + try:
> + translation = gettext.translation(domain, paths.mo_dir, [lang])
> + except:
> + translation = gettext
> +
> + 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)
>
--
Jose Ricardo Ziviani
-----------------------------
Software Engineer
Linux Technology Center - IBM
More information about the Kimchi-devel
mailing list