summaryrefslogtreecommitdiff
path: root/cinderclient/utils.py
diff options
context:
space:
mode:
authorFlaper Fesp <flaper87@gmail.com>2013-02-27 14:47:44 +0100
committerFlaper Fesp <flaper87@gmail.com>2013-03-29 13:50:48 +0100
commit03a4806d972ac150e717451566e9c4c35a141d8f (patch)
treeb5079ee0b75eb9d184449913ac71691ba7a4f6c0 /cinderclient/utils.py
parent7369310622224073ecbef4ab84a48c2a873a56b9 (diff)
downloadpython-cinderclient-1.0.3.tar.gz
Decodes input and encodes output1.0.3
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
Diffstat (limited to 'cinderclient/utils.py')
-rw-r--r--cinderclient/utils.py14
1 files changed, 5 insertions, 9 deletions
diff --git a/cinderclient/utils.py b/cinderclient/utils.py
index 0e67508..f9c6566 100644
--- a/cinderclient/utils.py
+++ b/cinderclient/utils.py
@@ -1,4 +1,3 @@
-import locale
import os
import re
import sys
@@ -7,6 +6,7 @@ import uuid
import prettytable
from cinderclient import exceptions
+from cinderclient.openstack.common import strutils
def arg(*args, **kwargs):
@@ -143,14 +143,14 @@ def print_list(objs, fields, formatters={}):
row.append(data)
pt.add_row(row)
- print pt.get_string(sortby=fields[0])
+ print strutils.safe_encode(pt.get_string(sortby=fields[0]))
def print_dict(d, property="Property"):
pt = prettytable.PrettyTable([property, 'Value'], caching=False)
pt.aligns = ['l', 'l']
[pt.add_row(list(r)) for r in d.iteritems()]
- print pt.get_string(sortby=property)
+ print strutils.safe_encode(pt.get_string(sortby=property))
def find_resource(manager, name_or_id):
@@ -164,7 +164,7 @@ def find_resource(manager, name_or_id):
# now try to get entity as uuid
try:
- uuid.UUID(str(name_or_id))
+ uuid.UUID(strutils.safe_decode(name_or_id))
return manager.get(name_or_id)
except (ValueError, exceptions.NotFound):
pass
@@ -180,11 +180,7 @@ def find_resource(manager, name_or_id):
return manager.find(name=name_or_id)
except exceptions.NotFound:
try:
- # For command-line arguments that are in Unicode
- encoding = (locale.getpreferredencoding() or
- sys.stdin.encoding or
- 'UTF-8')
- return manager.find(display_name=(name_or_id.decode(encoding)))
+ return manager.find(display_name=name_or_id)
except (UnicodeDecodeError, exceptions.NotFound):
try:
# Volumes does not have name, but display_name