[Kimchi-devel] [PATCH 1/2] add to new function to encode the unicode passed to cherrpy.

Aline Manera alinefm at linux.vnet.ibm.com
Tue Apr 1 19:16:08 UTC 2014


On 04/01/2014 06:57 AM, shaohef at linux.vnet.ibm.com wrote:
> From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
>
> Then others can make use of it.
>
> The cherrpy low version, cherrpy HTTPError accept unicode.
> But the high level version, cherrpy refuse to accept unicode.
>
> Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
> Signed-off-by: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
> ---
>   src/kimchi/root.py  | 11 +++--------
>   src/kimchi/utils.py |  8 ++++++++
>   2 files changed, 11 insertions(+), 8 deletions(-)
>
> diff --git a/src/kimchi/root.py b/src/kimchi/root.py
> index 9bae34a..aabae68 100644
> --- a/src/kimchi/root.py
> +++ b/src/kimchi/root.py
> @@ -31,6 +31,7 @@ from kimchi.control import sub_nodes
>   from kimchi.control.base import Resource
>   from kimchi.control.utils import parse_request
>   from kimchi.exception import MissingParameter, OperationFailed
> +from kimchi.utils import safe_encode
>
>
>   class Root(Resource):
> @@ -50,19 +51,13 @@ class Root(Resource):
>       def error_production_handler(self, status, message, traceback, version):
>           data = {'code': status, 'reason': message}
>           res = template.render('error.html', data)
> -        if (type(res) is unicode and
> -                LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')):
> -            res = res.encode("utf-8")
> -        return res
> +        return safe_encode(res)
>
>       def error_development_handler(self, status, message, traceback, version):
>           data = {'code': status, 'reason': message,
>                   'call_stack': cherrypy._cperror.format_exc()}
>           res = template.render('error.html', data)
> -        if (type(res) is unicode and
> -                LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')):
> -            res = res.encode("utf-8")
> -        return res
> +        return safe_encode(res)
>
>       def get(self):
>           return self.default(self.default_page)
> diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
> index 6c29e0e..bfad775 100644
> --- a/src/kimchi/utils.py
> +++ b/src/kimchi/utils.py
> @@ -31,6 +31,7 @@ from multiprocessing import Process, Queue
>   from threading import Timer
>
>   from cherrypy.lib.reprconf import Parser
> +from distutils.version import LooseVersion
>
>   from kimchi.asynctask import AsyncTask
>   from kimchi.config import paths, PluginPaths
> @@ -41,6 +42,13 @@ kimchi_log = cherrypy.log.error_log
>   task_id = 0
>

> +def safe_encode(string):
> +    if (type(string) is unicode and
> +       LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')):
> +        return string.encode("utf-8")
> +    return string
> +
> +

As it is related to cherrypy, ie, controller, I suggest put it into 
src/kimchi/control/utils.py

>   def _uri_to_name(collection, uri):
>       expr = '/%s/(.*?)/?$' % collection
>       m = re.match(expr, uri)




More information about the Kimchi-devel mailing list