From 1197fb3701641663c86c678c27708b6bf71c7062 Mon Sep 17 00:00:00 2001 From: Chuck Short Date: Fri, 21 Jun 2013 07:15:11 -0500 Subject: [PATCH] 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 --- cinderclient/shell.py | 10 ++++++++-- cinderclient/utils.py | 23 +++++++++++++++++------ cinderclient/v2/shell.py | 4 +++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/cinderclient/shell.py b/cinderclient/shell.py index 9750d2f79..d7377b196 100644 --- a/cinderclient/shell.py +++ b/cinderclient/shell.py @@ -29,6 +29,8 @@ import pkgutil import sys import logging +import six + from cinderclient import client from cinderclient import exceptions as exc import cinderclient.extension @@ -500,14 +502,18 @@ class OpenStackHelpFormatter(argparse.HelpFormatter): def main(): try: - OpenStackCinderShell().main(map(strutils.safe_decode, sys.argv[1:])) + if sys.version_info >= (3, 0): + OpenStackCinderShell().main(sys.argv[1:]) + else: + OpenStackCinderShell().main(map(strutils.safe_decode, + sys.argv[1:])) except KeyboardInterrupt: print("... terminating cinder client", file=sys.stderr) sys.exit(130) except Exception as e: logger.debug(e, exc_info=1) message = e.message - if not isinstance(message, basestring): + if not isinstance(message, six.string_types): message = str(message) print("ERROR: %s" % strutils.safe_encode(message), file=sys.stderr) sys.exit(1) diff --git a/cinderclient/utils.py b/cinderclient/utils.py index fe947a020..922f053e9 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()) diff --git a/cinderclient/v2/shell.py b/cinderclient/v2/shell.py index e96609d61..996447d3f 100644 --- a/cinderclient/v2/shell.py +++ b/cinderclient/v2/shell.py @@ -20,6 +20,8 @@ import os import sys import time +import six + from cinderclient import exceptions from cinderclient import utils @@ -253,7 +255,7 @@ def do_create(cs, args): # NOTE(vish): multiple copies of the same hint will # result in a list of values if key in hints: - if isinstance(hints[key], basestring): + if isinstance(hints[key], six.string_types): hints[key] = [hints[key]] hints[key] += [value] else: