Skip to content

Commit

Permalink
Fix issues reported by flake8-bugbear
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoranzo committed Jul 4, 2020
1 parent cbb5ae5 commit 857549e
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bioblend/_tests/TestGalaxyInvocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def invocation_steps_by_order_index():
invocation = self.gi.invocations.show_invocation(invocation_id)
return dict((s["order_index"], s) for s in invocation["steps"])

for i in range(20):
for _ in range(20):
if 2 in invocation_steps_by_order_index():
break
time.sleep(.5)
Expand All @@ -64,7 +64,7 @@ def invocation_steps_by_order_index():
self.gi.invocations.show_invocation_step(invocation_id, pause_step["id"])["action"])
self.gi.invocations.run_invocation_step_action(invocation_id, pause_step["id"], action=True)
self.assertTrue(self.gi.invocations.show_invocation_step(invocation_id, pause_step["id"])["action"])
for i in range(20):
for _ in range(20):
invocation = self.gi.invocations.show_invocation(invocation_id)
if invocation["state"] == "scheduled":
break
Expand Down
4 changes: 2 additions & 2 deletions bioblend/_tests/TestGalaxyWorkflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def invocation_steps_by_order_index():
invocation = self.gi.workflows.show_invocation(workflow_id, invocation_id)
return dict((s["order_index"], s) for s in invocation["steps"])

for i in range(20):
for _ in range(20):
if 2 in invocation_steps_by_order_index():
break
time.sleep(.5)
Expand All @@ -48,7 +48,7 @@ def invocation_steps_by_order_index():
self.gi.workflows.show_invocation_step(workflow_id, invocation_id, pause_step["id"])["action"])
self.gi.workflows.run_invocation_step_action(workflow_id, invocation_id, pause_step["id"], action=True)
self.assertTrue(self.gi.workflows.show_invocation_step(workflow_id, invocation_id, pause_step["id"])["action"])
for i in range(20):
for _ in range(20):
invocation = self.gi.workflows.show_invocation(workflow_id, invocation_id)
if invocation["state"] == "scheduled":
break
Expand Down
13 changes: 9 additions & 4 deletions bioblend/cloudman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self,
galaxy_data_option='',
initial_storage_size=10,
key_name='cloudman_key_pair',
security_groups=['CloudMan'],
security_groups=None,
placement='',
kernel_id=None,
ramdisk_id=None,
Expand Down Expand Up @@ -160,6 +160,8 @@ def __init__(self,
extra parameters to the ``CloudManInstance.launch_instance``
method.
"""
if security_groups is None:
security_groups = ['CloudMan']
self.set_connection_parameters(access_key, secret_key, cloud_metadata)
self.set_pre_launch_parameters(
cluster_name, image_id, instance_type,
Expand All @@ -176,8 +178,9 @@ def set_connection_parameters(self, access_key, secret_key, cloud_metadata=None)
def set_pre_launch_parameters(
self, cluster_name, image_id, instance_type, password,
kernel_id=None, ramdisk_id=None, key_name='cloudman_key_pair',
security_groups=['CloudMan'], placement='',
block_until_ready=False):
security_groups=None, placement='', block_until_ready=False):
if security_groups is None:
security_groups = ['CloudMan']
self.cluster_name = cluster_name
self.image_id = image_id
self.instance_type = instance_type
Expand Down Expand Up @@ -711,14 +714,16 @@ def terminate(self, terminate_master_instance=True, delete_cluster=False):
timeout=15)
return result

def _make_get_request(self, url, parameters={}, timeout=None):
def _make_get_request(self, url, parameters=None, timeout=None):
"""
Private function that makes a GET request to the nominated ``url``,
with the provided GET ``parameters``. Optionally, set the ``timeout``
to stop waiting for a response after a given number of seconds. This is
particularly useful when terminating a cluster as it may terminate
before sending a response.
"""
if parameters is None:
parameters = {}
req_url = '/'.join((self.cloudman_url, 'root', url))
r = requests.get(req_url, params=parameters, auth=("", self.password), timeout=timeout)
try:
Expand Down
22 changes: 12 additions & 10 deletions bioblend/cloudman/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __repr__(self):

def launch(self, cluster_name, image_id, instance_type, password,
kernel_id=None, ramdisk_id=None, key_name='cloudman_key_pair',
security_groups=['CloudMan'], placement='', subnet_id=None,
security_groups=None, placement='', subnet_id=None,
ebs_optimized=False, **kwargs):
"""
Check all the prerequisites (key pair and security groups) for
Expand All @@ -139,6 +139,8 @@ def launch(self, cluster_name, image_id, instance_type, password,
``instance_id`` containing the ID of a started instance, and
``error`` containing an error message if there was one.
"""
if security_groups is None:
security_groups = ['CloudMan']
ret = {'sg_names': [],
'sg_ids': [],
'kp_name': '',
Expand Down Expand Up @@ -215,7 +217,7 @@ def launch(self, cluster_name, image_id, instance_type, password,
ret['rs'] = rs
except EC2ResponseError as e:
err_msg = "Problem launching an instance: {0} (code {1}; status {2})" \
.format(e.message, e.error_code, e.status)
.format(str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
ret['error'] = err_msg
return ret
Expand All @@ -228,7 +230,7 @@ def launch(self, cluster_name, image_id, instance_type, password,
except EC2ResponseError as e:
err_msg = "Problem with the launched instance object: {0} " \
"(code {1}; status {2})" \
.format(e.message, e.error_code, e.status)
.format(str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
ret['error'] = err_msg
else:
Expand Down Expand Up @@ -281,7 +283,7 @@ def create_cm_security_group(self, sg_name='CloudMan', vpc_id=None):
err_msg = ("Problem getting security groups. This could indicate a "
"problem with your account credentials or permissions: "
"{0} (code {1}; status {2})"
.format(e.message, e.error_code, e.status))
.format(str(e), e.error_code, e.status))
bioblend.log.exception(err_msg)
progress['error'] = err_msg
return progress
Expand All @@ -300,7 +302,7 @@ def create_cm_security_group(self, sg_name='CloudMan', vpc_id=None):
except EC2ResponseError as e:
err_msg = "Problem creating security group '{0}': {1} (code {2}; " \
"status {3})" \
.format(sg_name, e.message, e.error_code, e.status)
.format(sg_name, str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
progress['error'] = err_msg
if cmsg:
Expand All @@ -322,7 +324,7 @@ def create_cm_security_group(self, sg_name='CloudMan', vpc_id=None):
except EC2ResponseError as e:
err_msg = "A problem adding security group authorizations: {0} " \
"(code {1}; status {2})" \
.format(e.message, e.error_code, e.status)
.format(str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
progress['error'] = err_msg
# Add ICMP (i.e., ping) rule required by HTCondor
Expand All @@ -340,7 +342,7 @@ def create_cm_security_group(self, sg_name='CloudMan', vpc_id=None):
except EC2ResponseError as e:
err_msg = "A problem with security ICMP rule authorization: {0} " \
"(code {1}; status {2})" \
.format(e.message, e.error_code, e.status)
.format(str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
progress['err_msg'] = err_msg
# Add rule that allows communication between instances in the same
Expand All @@ -365,7 +367,7 @@ def create_cm_security_group(self, sg_name='CloudMan', vpc_id=None):
except EC2ResponseError as e:
err_msg = "A problem with security group group " \
"authorization: {0} (code {1}; status {2})" \
.format(e.message, e.error_code, e.status)
.format(str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
progress['err_msg'] = err_msg
bioblend.log.info("Done configuring '%s' security group", cmsg.name)
Expand Down Expand Up @@ -414,7 +416,7 @@ def create_key_pair(self, key_name='cloudman_key_pair'):
kps = self.ec2_conn.get_all_key_pairs()
except EC2ResponseError as e:
err_msg = "Problem getting key pairs: {0} (code {1}; status {2})" \
.format(e.message, e.error_code, e.status)
.format(str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
progress['error'] = err_msg
return progress
Expand All @@ -427,7 +429,7 @@ def create_key_pair(self, key_name='cloudman_key_pair'):
kp = self.ec2_conn.create_key_pair(key_name)
except EC2ResponseError as e:
err_msg = "Problem creating key pair '{0}': {1} (code {2}; status {3})" \
.format(key_name, e.message, e.error_code, e.status)
.format(key_name, str(e), e.error_code, e.status)
bioblend.log.exception(err_msg)
progress['error'] = err_msg
return progress
Expand Down
2 changes: 1 addition & 1 deletion bioblend/galaxy/dataset_collections/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HasElements(object):

def __init__(self, name, type="list", elements=[]):
def __init__(self, name, type="list", elements=None):
self.name = name
self.type = type
if isinstance(elements, dict):
Expand Down
12 changes: 10 additions & 2 deletions bioblend/galaxy/groups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def show_group(self, group_id):
"""
return self._get(id=group_id)

def create_group(self, group_name, user_ids=[], role_ids=[]):
def create_group(self, group_name, user_ids=None, role_ids=None):
"""
Create a new group.
Expand All @@ -71,14 +71,18 @@ def create_group(self, group_name, user_ids=[], role_ids=[]):
'name': 'My Group Name',
'url': '/api/groups/7c9636938c3e83bf'}]
"""
if user_ids is None:
user_ids = []
if role_ids is None:
role_ids = []
payload = {
'name': group_name,
'user_ids': user_ids,
'role_ids': role_ids
}
return self._post(payload)

def update_group(self, group_id, group_name=None, user_ids=[], role_ids=[]):
def update_group(self, group_id, group_name=None, user_ids=None, role_ids=None):
"""
Update a group.
Expand All @@ -100,6 +104,10 @@ def update_group(self, group_id, group_name=None, user_ids=[], role_ids=[]):
:rtype: None
:return: None
"""
if user_ids is None:
user_ids = []
if role_ids is None:
role_ids = []
payload = {
'name': group_name,
'user_ids': user_ids,
Expand Down
4 changes: 2 additions & 2 deletions bioblend/galaxy/quotas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def show_quota(self, quota_id, deleted=False):
return self._get(id=quota_id, deleted=deleted)

def create_quota(self, name, description, amount, operation,
default='no', in_users=[], in_groups=[]):
default='no', in_users=None, in_groups=None):
"""
Create a new quota
Expand Down Expand Up @@ -113,7 +113,7 @@ def create_quota(self, name, description, amount, operation,
return self._post(payload)

def update_quota(self, quota_id, name=None, description=None, amount=None, operation=None,
default='no', in_users=[], in_groups=[]):
default='no', in_users=None, in_groups=None):
"""
Update an existing quota
Expand Down
6 changes: 5 additions & 1 deletion bioblend/galaxy/roles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def show_role(self, role_id):
"""
return self._get(id=role_id)

def create_role(self, role_name, description, user_ids=[], group_ids=[]):
def create_role(self, role_name, description, user_ids=None, group_ids=None):
"""
Create a new role.
Expand All @@ -76,6 +76,10 @@ def create_role(self, role_name, description, user_ids=[], group_ids=[]):
'id': 'ebfb8f50c6abde6d',
'name': 'Foo'}]
"""
if user_ids is None:
user_ids = []
if group_ids is None:
group_ids = []
payload = {
'name': role_name,
'description': description,
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/objects/w5_galaxy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

# Select the blastn step

ws = [_ for _ in iw_details['steps'].itervalues()
ws = [_ for _ in iw_details['steps'].values()
if _['tool_id'] and 'blastn' in _['tool_id']]
assert len(ws) == 1
ws = ws[0]
Expand All @@ -66,7 +66,7 @@
# Get (a copy of) the parameters dict for the selected step

ws_parameters = ws['tool_inputs'].copy()
for k, v in ws_parameters.iteritems():
for k, v in ws_parameters.items():
ws_parameters[k] = json.loads(v)

# Run the workflow on a new history with the selected dataset
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ commands =
deps =
flake8: flake8
flake8: flake8-import-order>=0.9
flake8: flake8-bugbear
py{35,36,37,38}: pytest
passenv =
py{35,36,37,38}: BIOBLEND_GALAXY_API_KEY BIOBLEND_GALAXY_MASTER_API_KEY BIOBLEND_GALAXY_URL BIOBLEND_GALAXY_USER_EMAIL BIOBLEND_TEST_JOB_TIMEOUT GALAXY_VERSION

0 comments on commit 857549e

Please sign in to comment.