summaryrefslogtreecommitdiff
path: root/cinderclient/utils.py
diff options
context:
space:
mode:
authorChuck Short <chuck.short@canonical.com>2013-06-21 07:15:11 -0500
committerChuck Short <chuck.short@canonical.com>2013-06-22 12:50:23 -0500
commit1197fb3701641663c86c678c27708b6bf71c7062 (patch)
tree49c9f20d684b76ba510ef39a7ba37cc26c945f6c /cinderclient/utils.py
parentbbf791a1f50a28f22a76cdd9ede85c913476a1fe (diff)
downloadpython-cinderclient-1197fb3701641663c86c678c27708b6bf71c7062.tar.gz
python3: Strutils is not needed
strutils is used to safely encode and decode unicode strings for python2.7. Since unicode strings are the default in python3, ignore the use of strutils when running with python3. Change-Id: I9a8e296b4f2153b1ef4302a7dcd797fbb4561c35 Signed-off-by: Chuck Short <chuck.short@canonical.com>
Diffstat (limited to 'cinderclient/utils.py')
-rw-r--r--cinderclient/utils.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/cinderclient/utils.py b/cinderclient/utils.py
index fe947a0..922f053 100644
--- a/cinderclient/utils.py
+++ b/cinderclient/utils.py
@@ -142,7 +142,14 @@ def pretty_choice_list(l):
return ', '.join("'%s'" % i for i in l)
-def print_list(objs, fields, formatters={}):
+def _print(pt, order):
+ if sys.version_info >= (3, 0):
+ print(pt.get_string(sortby=order))
+ else:
+ print(strutils.safe_encode(pt.get_string(sortby=order)))
+
+
+def print_list(objs, fields, formatters={}, order_by=None):
mixed_case_fields = ['serverId']
pt = prettytable.PrettyTable([f for f in fields], caching=False)
pt.aligns = ['l' for f in fields]
@@ -161,15 +168,16 @@ def print_list(objs, fields, formatters={}):
row.append(data)
pt.add_row(row)
- if len(pt._rows) > 0:
- print(strutils.safe_encode(pt.get_string(sortby=fields[0])))
+ if order_by is None:
+ order_by = fields[0]
+ _print(pt, order_by)
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 six.iteritems(d)]
- print(strutils.safe_encode(pt.get_string(sortby=property)))
+ _print(pt, property)
def find_resource(manager, name_or_id):
@@ -181,9 +189,12 @@ def find_resource(manager, name_or_id):
except exceptions.NotFound:
pass
+ if sys.version_info <= (3, 0):
+ name_or_id = strutils.safe_decode(name_or_id)
+
# now try to get entity as uuid
try:
- uuid.UUID(strutils.safe_decode(name_or_id))
+ uuid.UUID(name_or_id)
return manager.get(name_or_id)
except (ValueError, exceptions.NotFound):
pass
@@ -277,7 +288,7 @@ def slugify(value):
From Django's "django/template/defaultfilters.py".
"""
import unicodedata
- if not isinstance(value, unicode):
+ if not isinstance(value, six.text_type):
value = six.text_type(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = six.text_type(_slugify_strip_re.sub('', value).strip().lower())