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>
This commit is contained in:
Chuck Short
2013-06-21 07:15:11 -05:00
parent bbf791a1f5
commit 1197fb3701
3 changed files with 28 additions and 9 deletions

View File

@@ -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)

View File

@@ -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())

View File

@@ -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: