Merge "python3: Refactor dict for python2/python3 compat"

This commit is contained in:
Jenkins 2013-10-13 05:53:35 +00:00 committed by Gerrit Code Review
commit 0c2839414f
14 changed files with 43 additions and 44 deletions

@ -270,7 +270,7 @@ class Resource(object):
return self.__dict__[k] return self.__dict__[k]
def __repr__(self): def __repr__(self):
reprkeys = sorted(k for k in list(self.__dict__.keys()) if k[0] != '_' reprkeys = sorted(k for k in self.__dict__ if k[0] != '_'
and k != 'manager') and k != 'manager')
info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys) info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys)
return "<%s %s>" % (self.__class__.__name__, info) return "<%s %s>" % (self.__class__.__name__, info)

@ -402,7 +402,7 @@ def get_client_class(version):
client_path = version_map[str(version)] client_path = version_map[str(version)]
except (KeyError, ValueError): except (KeyError, ValueError):
msg = "Invalid client version '%s'. must be one of: %s" % ( msg = "Invalid client version '%s'. must be one of: %s" % (
(version, ', '.join(list(version_map.keys())))) (version, ', '.join(version_map)))
raise exceptions.UnsupportedVersion(msg) raise exceptions.UnsupportedVersion(msg)
return utils.import_class(client_path) return utils.import_class(client_path)

@ -166,7 +166,7 @@ def from_response(response, body):
message = "n/a" message = "n/a"
details = "n/a" details = "n/a"
if hasattr(body, 'keys'): if hasattr(body, 'keys'):
error = body[list(body.keys())[0]] error = body[list(body)[0]]
message = error.get('message', None) message = error.get('message', None)
details = error.get('details', None) details = error.get('details', None)
return cls(code=response.status_code, message=message, details=details, return cls(code=response.status_code, message=message, details=details,

@ -476,7 +476,7 @@ class OpenStackCinderShell(object):
options = set() options = set()
for sc_str, sc in list(self.subcommands.items()): for sc_str, sc in list(self.subcommands.items()):
commands.add(sc_str) commands.add(sc_str)
for option in list(sc._optionals._option_string_actions.keys()): for option in sc._optionals._option_string_actions:
options.add(option) options.add(option)
commands.remove('bash-completion') commands.remove('bash-completion')

@ -23,12 +23,11 @@ from __future__ import print_function
def assert_has_keys(dict, required=[], optional=[]): def assert_has_keys(dict, required=[], optional=[]):
keys = list(dict.keys())
for k in required: for k in required:
try: try:
assert k in keys assert k in dict
except AssertionError: except AssertionError:
extra_keys = set(keys).difference(set(required + optional)) extra_keys = set(dict).difference(set(required + optional))
raise AssertionError("found unexpected keys: %s" % raise AssertionError("found unexpected keys: %s" %
list(extra_keys)) list(extra_keys))

@ -268,8 +268,8 @@ class FakeHTTPClient(base_client.HTTPClient):
def post_snapshots_1234_action(self, body, **kw): def post_snapshots_1234_action(self, body, **kw):
_body = None _body = None
resp = 202 resp = 202
assert len(body.keys()) == 1 assert len(list(body)) == 1
action = body.keys()[0] action = list(body)[0]
if action == 'os-reset_status': if action == 'os-reset_status':
assert 'status' in body['os-reset_status'] assert 'status' in body['os-reset_status']
elif action == 'os-update_snapshot_status': elif action == 'os-update_snapshot_status':
@ -313,10 +313,10 @@ class FakeHTTPClient(base_client.HTTPClient):
def post_volumes_1234_action(self, body, **kw): def post_volumes_1234_action(self, body, **kw):
_body = None _body = None
resp = 202 resp = 202
assert len(list(body.keys())) == 1 assert len(list(body)) == 1
action = list(body.keys())[0] action = list(body)[0]
if action == 'os-attach': if action == 'os-attach':
assert list(body[action].keys()) == ['instance_uuid', 'mountpoint'] assert list(body[action]) == ['instance_uuid', 'mountpoint']
elif action == 'os-detach': elif action == 'os-detach':
assert body[action] is None assert body[action] is None
elif action == 'os-reserve': elif action == 'os-reserve':
@ -324,10 +324,10 @@ class FakeHTTPClient(base_client.HTTPClient):
elif action == 'os-unreserve': elif action == 'os-unreserve':
assert body[action] is None assert body[action] is None
elif action == 'os-initialize_connection': elif action == 'os-initialize_connection':
assert list(body[action].keys()) == ['connector'] assert list(body[action]) == ['connector']
return (202, {}, {'connection_info': 'foos'}) return (202, {}, {'connection_info': 'foos'})
elif action == 'os-terminate_connection': elif action == 'os-terminate_connection':
assert list(body[action].keys()) == ['connector'] assert list(body[action]) == ['connector']
elif action == 'os-begin_detaching': elif action == 'os-begin_detaching':
assert body[action] is None assert body[action] is None
elif action == 'os-roll_detaching': elif action == 'os-roll_detaching':
@ -335,7 +335,7 @@ class FakeHTTPClient(base_client.HTTPClient):
elif action == 'os-reset_status': elif action == 'os-reset_status':
assert 'status' in body[action] assert 'status' in body[action]
elif action == 'os-extend': elif action == 'os-extend':
assert body[action].keys() == ['new_size'] assert list(body[action]) == ['new_size']
elif action == 'os-migrate_volume': elif action == 'os-migrate_volume':
assert 'host' in body[action] assert 'host' in body[action]
assert 'force_host_copy' in body[action] assert 'force_host_copy' in body[action]
@ -370,7 +370,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_sets_test(self, body, **kw): def put_os_quota_sets_test(self, body, **kw):
assert list(body.keys()) == ['quota_set'] assert list(body) == ['quota_set']
fakes.assert_has_keys(body['quota_set'], fakes.assert_has_keys(body['quota_set'],
required=['tenant_id']) required=['tenant_id'])
return (200, {}, {'quota_set': { return (200, {}, {'quota_set': {
@ -393,7 +393,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_class_sets_test(self, body, **kw): def put_os_quota_class_sets_test(self, body, **kw):
assert list(body.keys()) == ['quota_class_set'] assert list(body) == ['quota_class_set']
fakes.assert_has_keys(body['quota_class_set'], fakes.assert_has_keys(body['quota_class_set'],
required=['class_name']) required=['class_name'])
return (200, {}, {'quota_class_set': { return (200, {}, {'quota_class_set': {
@ -431,7 +431,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'extra_specs': {}}}) 'extra_specs': {}}})
def post_types_1_extra_specs(self, body, **kw): def post_types_1_extra_specs(self, body, **kw):
assert list(body.keys()) == ['extra_specs'] assert list(body) == ['extra_specs']
return (200, {}, {'extra_specs': {'k': 'v'}}) return (200, {}, {'extra_specs': {'k': 'v'}})
def delete_types_1_extra_specs_k(self, **kw): def delete_types_1_extra_specs_k(self, **kw):

@ -275,8 +275,8 @@ class FakeHTTPClient(base_client.HTTPClient):
def post_snapshots_1234_action(self, body, **kw): def post_snapshots_1234_action(self, body, **kw):
_body = None _body = None
resp = 202 resp = 202
assert len(body.keys()) == 1 assert len(list(body)) == 1
action = body.keys()[0] action = list(body)[0]
if action == 'os-reset_status': if action == 'os-reset_status':
assert 'status' in body['os-reset_status'] assert 'status' in body['os-reset_status']
elif action == 'os-update_snapshot_status': elif action == 'os-update_snapshot_status':
@ -320,10 +320,10 @@ class FakeHTTPClient(base_client.HTTPClient):
def post_volumes_1234_action(self, body, **kw): def post_volumes_1234_action(self, body, **kw):
_body = None _body = None
resp = 202 resp = 202
assert len(list(body.keys())) == 1 assert len(list(body)) == 1
action = list(body.keys())[0] action = list(body)[0]
if action == 'os-attach': if action == 'os-attach':
assert list(body[action].keys()) == ['instance_uuid', 'mountpoint'] assert list(body[action]) == ['instance_uuid', 'mountpoint']
elif action == 'os-detach': elif action == 'os-detach':
assert body[action] is None assert body[action] is None
elif action == 'os-reserve': elif action == 'os-reserve':
@ -331,10 +331,10 @@ class FakeHTTPClient(base_client.HTTPClient):
elif action == 'os-unreserve': elif action == 'os-unreserve':
assert body[action] is None assert body[action] is None
elif action == 'os-initialize_connection': elif action == 'os-initialize_connection':
assert list(body[action].keys()) == ['connector'] assert list(body[action]) == ['connector']
return (202, {}, {'connection_info': 'foos'}) return (202, {}, {'connection_info': 'foos'})
elif action == 'os-terminate_connection': elif action == 'os-terminate_connection':
assert list(body[action].keys()) == ['connector'] assert list(body[action]) == ['connector']
elif action == 'os-begin_detaching': elif action == 'os-begin_detaching':
assert body[action] is None assert body[action] is None
elif action == 'os-roll_detaching': elif action == 'os-roll_detaching':
@ -342,7 +342,7 @@ class FakeHTTPClient(base_client.HTTPClient):
elif action == 'os-reset_status': elif action == 'os-reset_status':
assert 'status' in body[action] assert 'status' in body[action]
elif action == 'os-extend': elif action == 'os-extend':
assert body[action].keys() == ['new_size'] assert list(body[action]) == ['new_size']
elif action == 'os-migrate_volume': elif action == 'os-migrate_volume':
assert 'host' in body[action] assert 'host' in body[action]
assert 'force_host_copy' in body[action] assert 'force_host_copy' in body[action]
@ -377,7 +377,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_sets_test(self, body, **kw): def put_os_quota_sets_test(self, body, **kw):
assert list(body.keys()) == ['quota_set'] assert list(body) == ['quota_set']
fakes.assert_has_keys(body['quota_set'], fakes.assert_has_keys(body['quota_set'],
required=['tenant_id']) required=['tenant_id'])
return (200, {}, {'quota_set': { return (200, {}, {'quota_set': {
@ -400,7 +400,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'gigabytes': 1}}) 'gigabytes': 1}})
def put_os_quota_class_sets_test(self, body, **kw): def put_os_quota_class_sets_test(self, body, **kw):
assert list(body.keys()) == ['quota_class_set'] assert list(body) == ['quota_class_set']
fakes.assert_has_keys(body['quota_class_set'], fakes.assert_has_keys(body['quota_class_set'],
required=['class_name']) required=['class_name'])
return (200, {}, {'quota_class_set': { return (200, {}, {'quota_class_set': {
@ -438,7 +438,7 @@ class FakeHTTPClient(base_client.HTTPClient):
'extra_specs': {}}}) 'extra_specs': {}}})
def post_types_1_extra_specs(self, body, **kw): def post_types_1_extra_specs(self, body, **kw):
assert list(body.keys()) == ['extra_specs'] assert list(body) == ['extra_specs']
return (200, {}, {'extra_specs': {'k': 'v'}}) return (200, {}, {'extra_specs': {'k': 'v'}})
def delete_types_1_extra_specs_k(self, **kw): def delete_types_1_extra_specs_k(self, **kw):

@ -39,7 +39,7 @@ class QuotaClassSetManager(base.Manager):
def update(self, class_name, **updates): def update(self, class_name, **updates):
body = {'quota_class_set': {'class_name': class_name}} body = {'quota_class_set': {'class_name': class_name}}
for update in updates.keys(): for update in updates:
body['quota_class_set'][update] = updates[update] body['quota_class_set'][update] = updates[update]
self._update('/os-quota-class-sets/%s' % (class_name), body) self._update('/os-quota-class-sets/%s' % (class_name), body)

@ -40,7 +40,7 @@ class QuotaSetManager(base.Manager):
def update(self, tenant_id, **updates): def update(self, tenant_id, **updates):
body = {'quota_set': {'tenant_id': tenant_id}} body = {'quota_set': {'tenant_id': tenant_id}}
for update in updates.keys(): for update in updates:
body['quota_set'][update] = updates[update] body['quota_set'][update] = updates[update]
self._update('/os-quota-sets/%s' % (tenant_id), body) self._update('/os-quota-sets/%s' % (tenant_id), body)

@ -95,7 +95,7 @@ def _print_volume_image(image):
def _translate_keys(collection, convert): def _translate_keys(collection, convert):
for item in collection: for item in collection:
keys = list(item.__dict__.keys()) keys = item.__dict__
for from_key, to_key in convert: for from_key, to_key in convert:
if from_key in keys and to_key not in keys: if from_key in keys and to_key not in keys:
setattr(item, to_key, item._info[from_key]) setattr(item, to_key, item._info[from_key])
@ -352,7 +352,7 @@ def do_metadata(cs, args):
if args.action == 'set': if args.action == 'set':
cs.volumes.set_metadata(volume, metadata) cs.volumes.set_metadata(volume, metadata)
elif args.action == 'unset': elif args.action == 'unset':
cs.volumes.delete_metadata(volume, list(metadata.keys())) cs.volumes.delete_metadata(volume, list(metadata))
@utils.arg( @utils.arg(
@ -560,7 +560,7 @@ def do_type_key(cs, args):
if args.action == 'set': if args.action == 'set':
vtype.set_keys(keypair) vtype.set_keys(keypair)
elif args.action == 'unset': elif args.action == 'unset':
vtype.unset_keys(list(keypair.keys())) vtype.unset_keys(list(keypair))
def do_endpoints(cs, args): def do_endpoints(cs, args):
@ -582,7 +582,7 @@ _quota_resources = ['volumes', 'snapshots', 'gigabytes']
def _quota_show(quotas): def _quota_show(quotas):
quota_dict = {} quota_dict = {}
for resource in quotas._info.keys(): for resource in quotas._info:
good_name = False good_name = False
for name in _quota_resources: for name in _quota_resources:
if resource.startswith(name): if resource.startswith(name):
@ -1198,7 +1198,7 @@ def do_qos_key(cs, args):
if args.action == 'set': if args.action == 'set':
cs.qos_specs.set_keys(args.qos_specs, keypair) cs.qos_specs.set_keys(args.qos_specs, keypair)
elif args.action == 'unset': elif args.action == 'unset':
cs.qos_specs.unset_keys(args.qos_specs, list(keypair.keys())) cs.qos_specs.unset_keys(args.qos_specs, list(keypair))
@utils.arg('qos_specs', metavar='<qos_specs>', @utils.arg('qos_specs', metavar='<qos_specs>',

@ -37,7 +37,7 @@ class QuotaClassSetManager(base.Manager):
def update(self, class_name, **updates): def update(self, class_name, **updates):
body = {'quota_class_set': {'class_name': class_name}} body = {'quota_class_set': {'class_name': class_name}}
for update in updates.keys(): for update in updates:
body['quota_class_set'][update] = updates[update] body['quota_class_set'][update] = updates[update]
self._update('/os-quota-class-sets/%s' % (class_name), body) self._update('/os-quota-class-sets/%s' % (class_name), body)

@ -38,7 +38,7 @@ class QuotaSetManager(base.Manager):
def update(self, tenant_id, **updates): def update(self, tenant_id, **updates):
body = {'quota_set': {'tenant_id': tenant_id}} body = {'quota_set': {'tenant_id': tenant_id}}
for update in updates.keys(): for update in updates:
body['quota_set'][update] = updates[update] body['quota_set'][update] = updates[update]
self._update('/os-quota-sets/%s' % (tenant_id), body) self._update('/os-quota-sets/%s' % (tenant_id), body)

@ -89,7 +89,7 @@ def _print_volume_image(image):
def _translate_keys(collection, convert): def _translate_keys(collection, convert):
for item in collection: for item in collection:
keys = list(item.__dict__.keys()) keys = item.__dict__
for from_key, to_key in convert: for from_key, to_key in convert:
if from_key in keys and to_key not in keys: if from_key in keys and to_key not in keys:
setattr(item, to_key, item._info[from_key]) setattr(item, to_key, item._info[from_key])
@ -390,7 +390,7 @@ def do_metadata(cs, args):
if args.action == 'set': if args.action == 'set':
cs.volumes.set_metadata(volume, metadata) cs.volumes.set_metadata(volume, metadata)
elif args.action == 'unset': elif args.action == 'unset':
cs.volumes.delete_metadata(volume, list(metadata.keys())) cs.volumes.delete_metadata(volume, list(metadata))
@utils.arg('--all-tenants', @utils.arg('--all-tenants',
@ -616,7 +616,7 @@ def do_type_key(cs, args):
if args.action == 'set': if args.action == 'set':
vtype.set_keys(keypair) vtype.set_keys(keypair)
elif args.action == 'unset': elif args.action == 'unset':
vtype.unset_keys(list(keypair.keys())) vtype.unset_keys(list(keypair))
def do_endpoints(cs, args): def do_endpoints(cs, args):
@ -638,7 +638,7 @@ _quota_resources = ['volumes', 'snapshots', 'gigabytes']
def _quota_show(quotas): def _quota_show(quotas):
quota_dict = {} quota_dict = {}
for resource in quotas._info.keys(): for resource in quotas._info:
good_name = False good_name = False
for name in _quota_resources: for name in _quota_resources:
if resource.startswith(name): if resource.startswith(name):
@ -1273,7 +1273,7 @@ def do_qos_key(cs, args):
if args.action == 'set': if args.action == 'set':
cs.qos_specs.set_keys(args.qos_specs, keypair) cs.qos_specs.set_keys(args.qos_specs, keypair)
elif args.action == 'unset': elif args.action == 'unset':
cs.qos_specs.unset_keys(args.qos_specs, list(keypair.keys())) cs.qos_specs.unset_keys(args.qos_specs, list(keypair))
@utils.arg('qos_specs', metavar='<qos_specs>', @utils.arg('qos_specs', metavar='<qos_specs>',

@ -267,7 +267,7 @@ class NovaTestResult(testtools.TestResult):
if not self.last_written or (self._now() - time).total_seconds() > 2.0: if not self.last_written or (self._now() - time).total_seconds() > 2.0:
diff = 3.0 diff = 3.0
while diff > 2.0: while diff > 2.0:
classes =list(self.results.keys()) classes =list(self.results)
oldest = min(classes, key=lambda x: self.last_time[x]) oldest = min(classes, key=lambda x: self.last_time[x])
diff = (self._now() - self.last_time[oldest]).total_seconds() diff = (self._now() - self.last_time[oldest]).total_seconds()
self.writeTestCase(oldest) self.writeTestCase(oldest)