From 03a4806d972ac150e717451566e9c4c35a141d8f Mon Sep 17 00:00:00 2001 From: Flaper Fesp Date: Wed, 27 Feb 2013 14:47:44 +0100 Subject: Decodes input and encodes output Currently cinderclient doesn't handle properly incoming and outgoing encode / decode process. As a solution for this, this patch implements a decoding process for all data incoming from the user side and encodes everything going out of the client, i.e: http requests, prints, etc. This patch introduces a new module (strutils.py) taken from oslo-incubator in order to use 2 of the functions present in it: About safe_(decode|encode): Both functions try to encode / decode the incoming text using the stdin encoding, fallback to python's default encoding if that returns None or to UTF-8 as the last option. In both functions only basestring objects are accepted and they both raise TypeError if an object of another type is passed. About the general cinderclient changes: In order to better support non-ASCII characters, it is a good practice to use unicode interanlly and encode everything that has to go out. This patch aims to do that and introduces this behaviour in the client. Testing: A good test (besides using tox) is to use cinder client with and without setting any locale (export LANG=). Fixes bug: 1130572 Change-Id: Idb7d06954c29e003f68a0c4aa0b80ecc7017cbc9 --- cinderclient/shell.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'cinderclient/shell.py') diff --git a/cinderclient/shell.py b/cinderclient/shell.py index b163d70..c080564 100644 --- a/cinderclient/shell.py +++ b/cinderclient/shell.py @@ -30,6 +30,7 @@ import logging from cinderclient import client from cinderclient import exceptions as exc import cinderclient.extension +from cinderclient.openstack.common import strutils from cinderclient import utils from cinderclient.v1 import shell as shell_v1 from cinderclient.v2 import shell as shell_v2 @@ -486,13 +487,16 @@ class OpenStackHelpFormatter(argparse.HelpFormatter): def main(): try: - OpenStackCinderShell().main(sys.argv[1:]) + OpenStackCinderShell().main(map(strutils.safe_decode, sys.argv[1:])) except KeyboardInterrupt: print >> sys.stderr, "... terminating cinder client" sys.exit(130) except Exception, e: logger.debug(e, exc_info=1) - print >> sys.stderr, "ERROR: %s" % e.message + message = e.message + if not isinstance(message, basestring): + message = str(message) + print >> sys.stderr, "ERROR: %s" % strutils.safe_encode(message) sys.exit(1) -- cgit v1.2.1