From 69fa91b4322395b125b8e50821ad88d0e0d2a313 Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Sun, 4 Aug 2013 16:12:23 +0200 Subject: [PATCH] Fix python 3.x related Hacking warnings Convert print operator usages to print functions. Fix one instance of outdated "except x,y:" syntactical construct. Remove usages of local() in string formatting alongway. Change-Id: Id0673a9183a6ea6bd9bf3f5c6d8e7c5f114ebf01 --- glanceclient/common/http.py | 3 +- glanceclient/common/utils.py | 8 ++- glanceclient/shell.py | 6 +- glanceclient/v1/legacy_shell.py | 115 ++++++++++++++++---------------- glanceclient/v1/shell.py | 22 +++--- 5 files changed, 82 insertions(+), 72 deletions(-) diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index 4cd66ec..e0fb9a4 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -215,7 +215,8 @@ class HTTPClient(object): raise exc.InvalidEndpoint(message=message) except (socket.error, socket.timeout) as e: endpoint = self.endpoint - message = "Error communicating with %(endpoint)s %(e)s" % locals() + message = ("Error communicating with %(endpoint)s %(e)s" % + {'endpoint': endpoint, 'e': e}) raise exc.CommunicationError(message=message) body_iter = ResponseBodyIterator(resp) diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 88f7621..cd4a9c9 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +from __future__ import print_function + import errno import os import sys @@ -54,14 +56,14 @@ def print_list(objs, fields, formatters={}): row.append(data) pt.add_row(row) - print strutils.safe_encode(pt.get_string()) + print(strutils.safe_encode(pt.get_string())) def print_dict(d): pt = prettytable.PrettyTable(['Property', 'Value'], caching=False) pt.align = 'l' [pt.add_row(list(r)) for r in d.iteritems()] - print strutils.safe_encode(pt.get_string(sortby='Property')) + print(strutils.safe_encode(pt.get_string(sortby='Property'))) def find_resource(manager, name_or_id): @@ -137,7 +139,7 @@ def import_versioned_module(version, submodule=None): def exit(msg=''): if msg: - print >> sys.stderr, strutils.safe_encode(msg) + print(strutils.safe_encode(msg), file=sys.stderr) sys.exit(1) diff --git a/glanceclient/shell.py b/glanceclient/shell.py index e0124e5..284a994 100644 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -17,6 +17,8 @@ Command-line interface to the OpenStack Images API. """ +from __future__ import print_function + import argparse import logging import re @@ -469,8 +471,8 @@ def main(): try: OpenStackImagesShell().main(map(strutils.safe_decode, sys.argv[1:])) except KeyboardInterrupt: - print >> sys.stderr, '... terminating glance client' + print('... terminating glance client', file=sys.stderr) sys.exit(1) except Exception as e: - print >> sys.stderr, utils.exception_to_str(e) + print(utils.exception_to_str(e), file=sys.stderr) sys.exit(1) diff --git a/glanceclient/v1/legacy_shell.py b/glanceclient/v1/legacy_shell.py index c9da52c..5800785 100644 --- a/glanceclient/v1/legacy_shell.py +++ b/glanceclient/v1/legacy_shell.py @@ -18,6 +18,8 @@ DEPRECATED functions that implement the same command line interface as the legacy glance client. """ +from __future__ import print_function + import argparse import sys import urlparse @@ -51,7 +53,7 @@ def get_image_filters_from_args(args): try: fields = get_image_fields_from_args(args) except RuntimeError as e: - print e + print(e) return FAILURE SUPPORTED_FILTERS = ['name', 'disk_format', 'container_format', 'status', @@ -78,27 +80,27 @@ def print_image_formatted(client, image): hostbase = "%s:%s" % (uri_parts.hostname, uri_parts.port) else: hostbase = uri_parts.hostname - print "URI: %s://%s/v1/images/%s" % (uri_parts.scheme, hostbase, image.id) - print "Id: %s" % image.id - print "Public: " + (image.is_public and "Yes" or "No") - print "Protected: " + (image.protected and "Yes" or "No") - print "Name: %s" % getattr(image, 'name', '') - print "Status: %s" % image.status - print "Size: %d" % int(image.size) - print "Disk format: %s" % getattr(image, 'disk_format', '') - print "Container format: %s" % getattr(image, 'container_format', '') - print "Minimum Ram Required (MB): %s" % image.min_ram - print "Minimum Disk Required (GB): %s" % image.min_disk + print("URI: %s://%s/v1/images/%s" % (uri_parts.scheme, hostbase, image.id)) + print("Id: %s" % image.id) + print("Public: " + (image.is_public and "Yes" or "No")) + print("Protected: " + (image.protected and "Yes" or "No")) + print("Name: %s" % getattr(image, 'name', '')) + print("Status: %s" % image.status) + print("Size: %d" % int(image.size)) + print("Disk format: %s" % getattr(image, 'disk_format', '')) + print("Container format: %s" % getattr(image, 'container_format', '')) + print("Minimum Ram Required (MB): %s" % image.min_ram) + print("Minimum Disk Required (GB): %s" % image.min_disk) if hasattr(image, 'owner'): - print "Owner: %s" % image.owner + print("Owner: %s" % image.owner) if len(image.properties) > 0: for k, v in image.properties.items(): - print "Property '%s': %s" % (k, v) - print "Created at: %s" % image.created_at + print("Property '%s': %s" % (k, v)) + print("Created at: %s" % image.created_at) if hasattr(image, 'deleted_at'): - print "Deleted at: %s" % image.deleted_at + print("Deleted at: %s" % image.deleted_at) if hasattr(image, 'updated_at'): - print "Updated at: %s" % image.updated_at + print("Updated at: %s" % image.updated_at) @utils.arg('--silent-upload', action="store_true", @@ -109,7 +111,7 @@ def do_add(gc, args): try: fields = get_image_fields_from_args(args.fields) except RuntimeError as e: - print e + print(e) return FAILURE image_meta = { @@ -131,7 +133,7 @@ def do_add(gc, args): unsupported_fields = ['status', 'size'] for field in unsupported_fields: if field in fields.keys(): - print 'Found non-settable field %s. Removing.' % field + print('Found non-settable field %s. Removing.' % field) fields.pop(field) # We need either a location or image data/stream to add... @@ -158,19 +160,19 @@ def do_add(gc, args): if not args.dry_run: image = gc.images.create(**image_meta) - print "Added new image with ID: %s" % image.id + print("Added new image with ID: %s" % image.id) if args.verbose: - print "Returned the following metadata for the new image:" + print("Returned the following metadata for the new image:") for k, v in sorted(image.to_dict().items()): - print " %(k)30s => %(v)s" % locals() + print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) else: - print "Dry run. We would have done the following:" + print("Dry run. We would have done the following:") def _dump(dict): for k, v in sorted(dict.items()): - print " %(k)30s => %(v)s" % locals() + print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) - print "Add new image with metadata:" + print("Add new image with metadata:") _dump(image_meta) return SUCCESS @@ -183,7 +185,7 @@ def do_update(gc, args): try: fields = get_image_fields_from_args(args.fields) except RuntimeError as e: - print e + print(e) return FAILURE image_meta = {} @@ -193,7 +195,7 @@ def do_update(gc, args): 'updated_at', 'size', 'status'] for field in nonmodifiable_fields: if field in fields.keys(): - print 'Found non-modifiable field %s. Removing.' % field + print('Found non-modifiable field %s. Removing.' % field) fields.pop(field) base_image_fields = ['disk_format', 'container_format', 'name', @@ -215,18 +217,18 @@ def do_update(gc, args): if not args.dry_run: image = gc.images.update(args.id, **image_meta) - print "Updated image %s" % args.id + print("Updated image %s" % args.id) if args.verbose: - print "Updated image metadata for image %s:" % args.id + print("Updated image metadata for image %s:" % args.id) print_image_formatted(gc, image) else: def _dump(dict): for k, v in sorted(dict.items()): - print " %(k)30s => %(v)s" % locals() + print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) - print "Dry run. We would have done the following:" - print "Update existing image with metadata:" + print("Dry run. We would have done the following:") + print("Update existing image with metadata:") _dump(image_meta) return SUCCESS @@ -237,7 +239,7 @@ def do_delete(gc, args): """DEPRECATED! Use image-delete instead.""" if not (args.force or user_confirm("Delete image %s?" % args.id, default=False)): - print 'Not deleting image %s' % args.id + print('Not deleting image %s' % args.id) return FAILURE gc.images.get(args.id).delete() @@ -289,14 +291,14 @@ def do_index(gc, args): pretty_table.add_column(20, label="Container Format") pretty_table.add_column(14, label="Size", just="r") - print pretty_table.make_header() + print(pretty_table.make_header()) for image in images: - print pretty_table.make_row(image.id, + print(pretty_table.make_row(image.id, image.name, image.disk_format, image.container_format, - image.size) + image.size)) @utils.arg('--limit', dest="limit", metavar="LIMIT", default=10, @@ -313,29 +315,30 @@ def do_details(gc, args): images = _get_images(gc, args) for i, image in enumerate(images): if i == 0: - print "=" * 80 + print("=" * 80) print_image_formatted(gc, image) - print "=" * 80 + print("=" * 80) def do_clear(gc, args): """DEPRECATED!""" if not (args.force or user_confirm("Delete all images?", default=False)): - print 'Not deleting any images' + print('Not deleting any images') return FAILURE images = gc.images.list() for image in images: if args.verbose: - print 'Deleting image %s "%s" ...' % (image.id, image.name), + print('Deleting image %s "%s" ...' % (image.id, image.name), + end=' ') try: image.delete() if args.verbose: - print 'done' + print('done') except Exception as e: - print 'Failed to delete image %s' % image.id - print e + print('Failed to delete image %s' % image.id) + print(e) return FAILURE return SUCCESS @@ -351,11 +354,11 @@ def do_image_members(gc, args): if memb.can_share: can_share = ' *' sharers += 1 - print "%s%s" % (memb.member_id, can_share) + print("%s%s" % (memb.member_id, can_share)) # Emit a footnote if sharers > 0: - print "\n(*: Can share image)" + print("\n(*: Can share image)") @utils.arg('--can-share', default=False, action="store_true", @@ -367,7 +370,7 @@ def do_member_images(gc, args): members = gc.image_members.list(member=args.member_id) if not len(members): - print "No images shared with member %s" % args.member_id + print("No images shared with member %s" % args.member_id) return SUCCESS sharers = 0 @@ -377,11 +380,11 @@ def do_member_images(gc, args): if memb.can_share: can_share = ' *' sharers += 1 - print "%s%s" % (memb.image_id, can_share) + print("%s%s" % (memb.image_id, can_share)) # Emit a footnote if sharers > 0: - print "\n(*: Can share image)" + print("\n(*: Can share image)") @utils.arg('--can-share', default=False, action="store_true", @@ -396,11 +399,11 @@ def do_members_replace(gc, args): gc.image_members.delete(args.image_id, member.member_id) gc.image_members.create(args.image_id, args.member_id, args.can_share) else: - print "Dry run. We would have done the following:" - print ('Replace members of image %s with "%s"' - % (args.image_id, args.member_id)) + print("Dry run. We would have done the following:") + print('Replace members of image %s with "%s"' + % (args.image_id, args.member_id)) if args.can_share: - print "New member would have been able to further share image." + print("New member would have been able to further share image.") @utils.arg('--can-share', default=False, action="store_true", @@ -413,11 +416,11 @@ def do_member_add(gc, args): if not args.dry_run: gc.image_members.create(args.image_id, args.member_id, args.can_share) else: - print "Dry run. We would have done the following:" - print ('Add "%s" to membership of image %s' % - (args.member_id, args.image_id)) + print("Dry run. We would have done the following:") + print('Add "%s" to membership of image %s' % + (args.member_id, args.image_id)) if args.can_share: - print "New member would have been able to further share image." + print("New member would have been able to further share image.") def user_confirm(prompt, default=False): diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py index 31c507b..d47a1b7 100644 --- a/glanceclient/v1/shell.py +++ b/glanceclient/v1/shell.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +from __future__ import print_function + import argparse import copy import os @@ -344,18 +346,18 @@ def do_image_delete(gc, args): image = utils.find_resource(gc.images, args_image) try: if args.verbose: - print 'Requesting image delete for %s ...' % \ - strutils.safe_encode(args_image), + print('Requesting image delete for %s ...' % + strutils.safe_encode(args_image), end=' ') gc.images.delete(image) if args.verbose: - print '[Done]' + print('[Done]') except exc.HTTPException as e: if args.verbose: - print '[Fail]' - print '%s: Unable to delete image %s' % (e, args_image) + print('[Fail]') + print('%s: Unable to delete image %s' % (e, args_image)) @utils.arg('--image-id', metavar='', @@ -365,14 +367,14 @@ def do_image_delete(gc, args): def do_member_list(gc, args): """Describe sharing permissions by image or tenant.""" if args.image_id and args.tenant_id: - print 'Unable to filter members by both --image-id and --tenant-id.' + print('Unable to filter members by both --image-id and --tenant-id.') sys.exit(1) elif args.image_id: kwargs = {'image': args.image_id} elif args.tenant_id: kwargs = {'member': args.tenant_id} else: - print 'Unable to list all members. Specify --image-id or --tenant-id' + print('Unable to list all members. Specify --image-id or --tenant-id') sys.exit(1) members = gc.image_members.list(**kwargs) @@ -402,6 +404,6 @@ def do_member_delete(gc, args): if not args.dry_run: gc.image_members.delete(image_id, args.tenant_id) else: - print "Dry run. We would have done the following:" - print ('Remove "%s" from the member list of image ' - '"%s"' % (args.tenant_id, args.image)) + print("Dry run. We would have done the following:") + print('Remove "%s" from the member list of image ' + '"%s"' % (args.tenant_id, args.image))