Merge "Fixes 'not in' operator usage"
This commit is contained in:
commit
77dbed11a6
12
HACKING.rst
12
HACKING.rst
@ -29,6 +29,18 @@ General
|
||||
mylist = Foo().list() # OKAY, does not shadow built-in
|
||||
|
||||
|
||||
- Use the "not in" operator for collection membership evaluation. Example::
|
||||
|
||||
if not X in Y: # BAD, hard to understand
|
||||
pass
|
||||
|
||||
if X not in Y: # OKAY, intuitive
|
||||
pass
|
||||
|
||||
if not (X in Y or X is Z): # OKAY, still better than all those 'not's
|
||||
pass
|
||||
|
||||
|
||||
Imports
|
||||
-------
|
||||
- Do not import objects, only modules (*)
|
||||
|
@ -280,7 +280,7 @@ class CloudController(object):
|
||||
host_services = {}
|
||||
for service in enabled_services:
|
||||
zone_hosts.setdefault(service['availability_zone'], [])
|
||||
if not service['host'] in zone_hosts[service['availability_zone']]:
|
||||
if service['host'] not in zone_hosts[service['availability_zone']]:
|
||||
zone_hosts[service['availability_zone']].append(
|
||||
service['host'])
|
||||
|
||||
@ -527,7 +527,7 @@ class CloudController(object):
|
||||
|
||||
def _rule_args_to_dict(self, context, kwargs):
|
||||
rules = []
|
||||
if not 'groups' in kwargs and not 'ip_ranges' in kwargs:
|
||||
if 'groups' not in kwargs and 'ip_ranges' not in kwargs:
|
||||
rule = self._rule_dict_last_step(context, **kwargs)
|
||||
if rule:
|
||||
rules.append(rule)
|
||||
@ -1549,11 +1549,11 @@ class CloudController(object):
|
||||
if attribute != 'launchPermission':
|
||||
raise exception.EC2APIError(_('attribute not supported: %s')
|
||||
% attribute)
|
||||
if not 'user_group' in kwargs:
|
||||
if 'user_group' not in kwargs:
|
||||
raise exception.EC2APIError(_('user or group not specified'))
|
||||
if len(kwargs['user_group']) != 1 and kwargs['user_group'][0] != 'all':
|
||||
raise exception.EC2APIError(_('only group "all" is supported'))
|
||||
if not operation_type in ['add', 'remove']:
|
||||
if operation_type not in ['add', 'remove']:
|
||||
msg = _('operation_type must be add or remove')
|
||||
raise exception.EC2APIError(msg)
|
||||
LOG.audit(_("Updating image %s publicity"), image_id, context=context)
|
||||
|
@ -102,7 +102,7 @@ class APIMapper(routes.Mapper):
|
||||
|
||||
class ProjectMapper(APIMapper):
|
||||
def resource(self, member_name, collection_name, **kwargs):
|
||||
if not ('parent_resource' in kwargs):
|
||||
if 'parent_resource' not in kwargs:
|
||||
kwargs['path_prefix'] = '{project_id}/'
|
||||
else:
|
||||
parent_resource = kwargs['parent_resource']
|
||||
|
@ -106,7 +106,7 @@ class AggregateController(object):
|
||||
raise exc.HTTPBadRequest
|
||||
|
||||
for key in updates.keys():
|
||||
if not key in ["name", "availability_zone"]:
|
||||
if key not in ["name", "availability_zone"]:
|
||||
raise exc.HTTPBadRequest
|
||||
|
||||
try:
|
||||
|
@ -110,7 +110,7 @@ class AvailabilityZoneController(wsgi.Controller):
|
||||
host_services = {}
|
||||
for service in enabled_services:
|
||||
zone_hosts.setdefault(service['availability_zone'], [])
|
||||
if not service['host'] in zone_hosts[service['availability_zone']]:
|
||||
if service['host'] not in zone_hosts[service['availability_zone']]:
|
||||
zone_hosts[service['availability_zone']].append(
|
||||
service['host'])
|
||||
|
||||
|
@ -84,7 +84,7 @@ class FlavorExtraSpecsController(object):
|
||||
context = req.environ['nova.context']
|
||||
authorize(context)
|
||||
self._check_body(body)
|
||||
if not id in body:
|
||||
if id not in body:
|
||||
expl = _('Request body and URI mismatch')
|
||||
raise exc.HTTPBadRequest(explanation=expl)
|
||||
if len(body) > 1:
|
||||
|
@ -80,13 +80,13 @@ class FloatingIPBulkController(object):
|
||||
context = req.environ['nova.context']
|
||||
authorize(context)
|
||||
|
||||
if not 'floating_ips_bulk_create' in body:
|
||||
if 'floating_ips_bulk_create' not in body:
|
||||
raise webob.exc.HTTPUnprocessableEntity()
|
||||
params = body['floating_ips_bulk_create']
|
||||
|
||||
LOG.debug(params)
|
||||
|
||||
if not 'ip_range' in params:
|
||||
if 'ip_range' not in params:
|
||||
raise webob.exc.HTTPUnprocessableEntity()
|
||||
ip_range = params['ip_range']
|
||||
|
||||
|
@ -159,7 +159,7 @@ class SimpleTenantUsageController(object):
|
||||
|
||||
info['uptime'] = delta.days * 24 * 3600 + delta.seconds
|
||||
|
||||
if not info['tenant_id'] in rval:
|
||||
if info['tenant_id'] not in rval:
|
||||
summary = {}
|
||||
summary['tenant_id'] = info['tenant_id']
|
||||
if detailed:
|
||||
|
@ -76,7 +76,7 @@ class Controller(object):
|
||||
expl = _('Incorrect request body format')
|
||||
raise exc.HTTPBadRequest(explanation=expl)
|
||||
|
||||
if not id in meta:
|
||||
if id not in meta:
|
||||
expl = _('Request body and URI mismatch')
|
||||
raise exc.HTTPBadRequest(explanation=expl)
|
||||
if len(meta) > 1:
|
||||
@ -105,7 +105,7 @@ class Controller(object):
|
||||
def delete(self, req, image_id, id):
|
||||
context = req.environ['nova.context']
|
||||
image = self._get_image(context, image_id)
|
||||
if not id in image['properties']:
|
||||
if id not in image['properties']:
|
||||
msg = _("Invalid metadata key")
|
||||
raise exc.HTTPNotFound(explanation=msg)
|
||||
image['properties'].pop(id)
|
||||
|
@ -751,7 +751,7 @@ class Controller(wsgi.Controller):
|
||||
server_dict = body['server']
|
||||
password = self._get_server_admin_password(server_dict)
|
||||
|
||||
if not 'name' in server_dict:
|
||||
if 'name' not in server_dict:
|
||||
msg = _("Server name is not defined")
|
||||
raise exc.HTTPBadRequest(explanation=msg)
|
||||
|
||||
|
@ -150,7 +150,7 @@ class Request(webob.Request):
|
||||
Does not do any body introspection, only checks header
|
||||
|
||||
"""
|
||||
if not "Content-Type" in self.headers:
|
||||
if "Content-Type" not in self.headers:
|
||||
return None
|
||||
|
||||
content_type = self.content_type
|
||||
|
@ -71,7 +71,7 @@ def get_availability_zones(context):
|
||||
available_zones = []
|
||||
for zone in [service['availability_zone'] for service
|
||||
in enabled_services]:
|
||||
if not zone in available_zones:
|
||||
if zone not in available_zones:
|
||||
available_zones.append(zone)
|
||||
|
||||
not_available_zones = []
|
||||
|
@ -52,7 +52,7 @@ def get_from_instance(instance):
|
||||
if mode == "hv":
|
||||
mode = HVM
|
||||
|
||||
if not mode in ALL:
|
||||
if mode not in ALL:
|
||||
raise exception.Invalid("Unknown vm mode '%s'" % mode)
|
||||
|
||||
return mode
|
||||
|
@ -269,7 +269,7 @@ class ComputeAttestationCache(object):
|
||||
|
||||
def get_host_attestation(self, host):
|
||||
"""Check host's trust level."""
|
||||
if not host in self.compute_nodes:
|
||||
if host not in self.compute_nodes:
|
||||
self._init_cache_entry(host)
|
||||
if not self._cache_valid(host):
|
||||
self._update_cache()
|
||||
|
@ -621,7 +621,7 @@ class WSGIService(object):
|
||||
|
||||
"""
|
||||
fl = '%s_manager' % self.name
|
||||
if not fl in CONF:
|
||||
if fl not in CONF:
|
||||
return None
|
||||
|
||||
manager_class_name = CONF.get(fl, None)
|
||||
|
@ -372,7 +372,7 @@ def create_info_cache(nw_cache):
|
||||
|
||||
|
||||
def get_fake_uuid(token=0):
|
||||
if not token in FAKE_UUIDS:
|
||||
if token not in FAKE_UUIDS:
|
||||
FAKE_UUIDS[token] = str(uuid.uuid4())
|
||||
return FAKE_UUIDS[token]
|
||||
|
||||
|
@ -391,7 +391,7 @@ class BaseTrackerTestCase(BaseTestCase):
|
||||
if tracker is None:
|
||||
tracker = self.tracker
|
||||
|
||||
if not field in tracker.compute_node:
|
||||
if field not in tracker.compute_node:
|
||||
raise test.TestingException(
|
||||
"'%(field)s' not in compute node." % locals())
|
||||
x = tracker.compute_node[field]
|
||||
|
@ -50,7 +50,7 @@ class GuestFS(object):
|
||||
self.mounts.append((options, device, mntpoint))
|
||||
|
||||
def mkdir_p(self, path):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
self.files[path] = {
|
||||
"isdir": True,
|
||||
"gid": 100,
|
||||
@ -59,7 +59,7 @@ class GuestFS(object):
|
||||
}
|
||||
|
||||
def read_file(self, path):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
self.files[path] = {
|
||||
"isdir": False,
|
||||
"content": "Hello World",
|
||||
@ -71,7 +71,7 @@ class GuestFS(object):
|
||||
return self.files[path]["content"]
|
||||
|
||||
def write(self, path, content):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
self.files[path] = {
|
||||
"isdir": False,
|
||||
"content": "Hello World",
|
||||
@ -83,7 +83,7 @@ class GuestFS(object):
|
||||
self.files[path]["content"] = content
|
||||
|
||||
def write_append(self, path, content):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
self.files[path] = {
|
||||
"isdir": False,
|
||||
"content": "Hello World",
|
||||
@ -95,13 +95,13 @@ class GuestFS(object):
|
||||
self.files[path]["content"] = self.files[path]["content"] + content
|
||||
|
||||
def stat(self, path):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
raise RuntimeError("No such file: " + path)
|
||||
|
||||
return self.files[path]["mode"]
|
||||
|
||||
def chown(self, uid, gid, path):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
raise RuntimeError("No such file: " + path)
|
||||
|
||||
if uid != -1:
|
||||
@ -110,7 +110,7 @@ class GuestFS(object):
|
||||
self.files[path]["gid"] = gid
|
||||
|
||||
def chmod(self, mode, path):
|
||||
if not path in self.files:
|
||||
if path not in self.files:
|
||||
raise RuntimeError("No such file: " + path)
|
||||
|
||||
self.files[path]["mode"] = mode
|
||||
|
@ -155,7 +155,7 @@ class TestOpenStackClient(object):
|
||||
LOG.debug(_("%(relative_uri)s => code %(http_status)s") % locals())
|
||||
|
||||
if check_response_status:
|
||||
if not http_status in check_response_status:
|
||||
if http_status not in check_response_status:
|
||||
if http_status == 404:
|
||||
raise OpenStackApiNotFoundException(response=response)
|
||||
elif http_status == 401:
|
||||
|
@ -58,7 +58,7 @@ def generate_new_element(items, prefix, numeric=False):
|
||||
candidate = prefix + generate_random_numeric(8)
|
||||
else:
|
||||
candidate = prefix + generate_random_alphanumeric(8)
|
||||
if not candidate in items:
|
||||
if candidate not in items:
|
||||
return candidate
|
||||
LOG.debug("Random collision on %s" % candidate)
|
||||
|
||||
|
@ -3848,7 +3848,7 @@ class IptablesFirewallTestCase(test.TestCase):
|
||||
in_rules = filter(lambda l: not l.startswith('#'),
|
||||
self.in_rules)
|
||||
for rule in in_rules:
|
||||
if not 'nova' in rule:
|
||||
if 'nova' not in rule:
|
||||
self.assertTrue(rule in self.out_rules,
|
||||
'Rule went missing: %s' % rule)
|
||||
|
||||
|
@ -46,7 +46,7 @@ def fake_execute(*args, **kwargs):
|
||||
elif args[0] == "chown":
|
||||
owner = args[1]
|
||||
path = args[2]
|
||||
if not path in files:
|
||||
if path not in files:
|
||||
raise Exception("No such file: " + path)
|
||||
|
||||
sep = owner.find(':')
|
||||
@ -72,7 +72,7 @@ def fake_execute(*args, **kwargs):
|
||||
elif args[0] == "chgrp":
|
||||
group = args[1]
|
||||
path = args[2]
|
||||
if not path in files:
|
||||
if path not in files:
|
||||
raise Exception("No such file: " + path)
|
||||
|
||||
if group == "users":
|
||||
@ -83,13 +83,13 @@ def fake_execute(*args, **kwargs):
|
||||
elif args[0] == "chmod":
|
||||
mode = args[1]
|
||||
path = args[2]
|
||||
if not path in files:
|
||||
if path not in files:
|
||||
raise Exception("No such file: " + path)
|
||||
|
||||
files[path]["mode"] = int(mode, 8)
|
||||
elif args[0] == "cat":
|
||||
path = args[1]
|
||||
if not path in files:
|
||||
if path not in files:
|
||||
files[path] = {
|
||||
"content": "Hello World",
|
||||
"gid": 100,
|
||||
@ -104,7 +104,7 @@ def fake_execute(*args, **kwargs):
|
||||
else:
|
||||
path = args[1]
|
||||
append = False
|
||||
if not path in files:
|
||||
if path not in files:
|
||||
files[path] = {
|
||||
"content": "Hello World",
|
||||
"gid": 100,
|
||||
|
@ -595,7 +595,7 @@ class XenAPIVMTestCase(stubs.XenAPITestBase):
|
||||
|
||||
def _check_vdis(self, start_list, end_list):
|
||||
for vdi_ref in end_list:
|
||||
if not vdi_ref in start_list:
|
||||
if vdi_ref not in start_list:
|
||||
vdi_rec = xenapi_fake.get_record('VDI', vdi_ref)
|
||||
# If the cache is turned on then the base disk will be
|
||||
# there even after the cleanup
|
||||
@ -1949,7 +1949,7 @@ class XenAPIDom0IptablesFirewallTestCase(stubs.XenAPITestBase):
|
||||
in_rules = filter(lambda l: not l.startswith('#'),
|
||||
self._in_rules)
|
||||
for rule in in_rules:
|
||||
if not 'nova' in rule:
|
||||
if 'nova' not in rule:
|
||||
self.assertTrue(rule in self._out_rules,
|
||||
'Rule went missing: %s' % rule)
|
||||
|
||||
|
@ -140,7 +140,7 @@ class BareMetalDriver(driver.ComputeDriver):
|
||||
keyval[0] = keyval[0].strip()
|
||||
keyval[1] = keyval[1].strip()
|
||||
extra_specs[keyval[0]] = keyval[1]
|
||||
if not 'cpu_arch' in extra_specs:
|
||||
if 'cpu_arch' not in extra_specs:
|
||||
LOG.warning(
|
||||
_('cpu_arch is not found in instance_type_extra_specs'))
|
||||
extra_specs['cpu_arch'] = ''
|
||||
|
@ -210,7 +210,7 @@ class LibvirtVolumeDriver(VolumeDriver):
|
||||
def _volume_driver_method(self, method_name, connection_info,
|
||||
*args, **kwargs):
|
||||
driver_type = connection_info.get('driver_volume_type')
|
||||
if not driver_type in self.volume_drivers:
|
||||
if driver_type not in self.volume_drivers:
|
||||
raise exception.VolumeDriverNotFound(driver_type=driver_type)
|
||||
driver = self.volume_drivers[driver_type]
|
||||
method = getattr(driver, method_name)
|
||||
|
@ -125,7 +125,7 @@ class FakeDriver(driver.ComputeDriver):
|
||||
self.instances[name] = fake_instance
|
||||
|
||||
def snapshot(self, context, instance, name, update_task_state):
|
||||
if not instance['name'] in self.instances:
|
||||
if instance['name'] not in self.instances:
|
||||
raise exception.InstanceNotRunning(instance_id=instance['uuid'])
|
||||
update_task_state(task_state=task_states.IMAGE_UPLOADING)
|
||||
|
||||
@ -209,7 +209,7 @@ class FakeDriver(driver.ComputeDriver):
|
||||
def attach_volume(self, connection_info, instance, mountpoint):
|
||||
"""Attach the disk to the instance at mountpoint using info."""
|
||||
instance_name = instance['name']
|
||||
if not instance_name in self._mounts:
|
||||
if instance_name not in self._mounts:
|
||||
self._mounts[instance_name] = {}
|
||||
self._mounts[instance_name][mountpoint] = connection_info
|
||||
return True
|
||||
|
@ -673,7 +673,7 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
def volume_driver_method(self, method_name, connection_info,
|
||||
*args, **kwargs):
|
||||
driver_type = connection_info.get('driver_volume_type')
|
||||
if not driver_type in self.volume_drivers:
|
||||
if driver_type not in self.volume_drivers:
|
||||
raise exception.VolumeDriverNotFound(driver_type=driver_type)
|
||||
driver = self.volume_drivers[driver_type]
|
||||
method = getattr(driver, method_name)
|
||||
|
@ -305,7 +305,7 @@ class ImageCacheManager(object):
|
||||
backing_path = os.path.join(CONF.instances_path,
|
||||
CONF.base_dir_name,
|
||||
backing_file)
|
||||
if not backing_path in inuse_images:
|
||||
if backing_path not in inuse_images:
|
||||
inuse_images.append(backing_path)
|
||||
|
||||
if backing_path in self.unexplained_images:
|
||||
@ -555,7 +555,7 @@ class ImageCacheManager(object):
|
||||
# Elements remaining in unexplained_images might be in use
|
||||
inuse_backing_images = self._list_backing_images()
|
||||
for backing_path in inuse_backing_images:
|
||||
if not backing_path in self.active_base_files:
|
||||
if backing_path not in self.active_base_files:
|
||||
self.active_base_files.append(backing_path)
|
||||
|
||||
# Anything left is an unknown base image
|
||||
|
@ -89,7 +89,7 @@ def reset():
|
||||
|
||||
|
||||
def reset_table(table):
|
||||
if not table in _CLASSES:
|
||||
if table not in _CLASSES:
|
||||
return
|
||||
_db_content[table] = {}
|
||||
|
||||
@ -417,7 +417,7 @@ class SessionBase(object):
|
||||
|
||||
def VBD_add_to_other_config(self, _1, vbd_ref, key, value):
|
||||
db_ref = _db_content['VBD'][vbd_ref]
|
||||
if not 'other_config' in db_ref:
|
||||
if 'other_config' not in db_ref:
|
||||
db_ref['other_config'] = {}
|
||||
if key in db_ref['other_config']:
|
||||
raise Failure(['MAP_DUPLICATE_KEY', 'VBD', 'other_config',
|
||||
@ -426,7 +426,7 @@ class SessionBase(object):
|
||||
|
||||
def VBD_get_other_config(self, _1, vbd_ref):
|
||||
db_ref = _db_content['VBD'][vbd_ref]
|
||||
if not 'other_config' in db_ref:
|
||||
if 'other_config' not in db_ref:
|
||||
return {}
|
||||
return db_ref['other_config']
|
||||
|
||||
@ -497,14 +497,14 @@ class SessionBase(object):
|
||||
|
||||
def VM_remove_from_xenstore_data(self, _1, vm_ref, key):
|
||||
db_ref = _db_content['VM'][vm_ref]
|
||||
if not 'xenstore_data' in db_ref:
|
||||
if 'xenstore_data' not in db_ref:
|
||||
return
|
||||
if key in db_ref['xenstore_data']:
|
||||
del db_ref['xenstore_data'][key]
|
||||
|
||||
def VM_add_to_xenstore_data(self, _1, vm_ref, key, value):
|
||||
db_ref = _db_content['VM'][vm_ref]
|
||||
if not 'xenstore_data' in db_ref:
|
||||
if 'xenstore_data' not in db_ref:
|
||||
db_ref['xenstore_data'] = {}
|
||||
db_ref['xenstore_data'][key] = value
|
||||
|
||||
@ -513,14 +513,14 @@ class SessionBase(object):
|
||||
|
||||
def VDI_remove_from_other_config(self, _1, vdi_ref, key):
|
||||
db_ref = _db_content['VDI'][vdi_ref]
|
||||
if not 'other_config' in db_ref:
|
||||
if 'other_config' not in db_ref:
|
||||
return
|
||||
if key in db_ref['other_config']:
|
||||
del db_ref['other_config'][key]
|
||||
|
||||
def VDI_add_to_other_config(self, _1, vdi_ref, key, value):
|
||||
db_ref = _db_content['VDI'][vdi_ref]
|
||||
if not 'other_config' in db_ref:
|
||||
if 'other_config' not in db_ref:
|
||||
db_ref['other_config'] = {}
|
||||
if key in db_ref['other_config']:
|
||||
raise Failure(['MAP_DUPLICATE_KEY', 'VDI', 'other_config',
|
||||
|
@ -1560,7 +1560,7 @@ def _find_iso_sr(session):
|
||||
if not sr_rec['content_type'] == 'iso':
|
||||
LOG.debug(_("ISO: not iso content"))
|
||||
continue
|
||||
if not 'i18n-key' in sr_rec['other_config']:
|
||||
if 'i18n-key' not in sr_rec['other_config']:
|
||||
LOG.debug(_("ISO: iso content_type, no 'i18n-key' key"))
|
||||
continue
|
||||
if not sr_rec['other_config']['i18n-key'] == 'local-storage-iso':
|
||||
|
Loading…
Reference in New Issue
Block a user