Skip to content

Commit

Permalink
adhering (closer) to pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Spelchak committed Feb 1, 2018
1 parent d315dfc commit 6fc2d4d
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions lib/galaxy/jobs/dynamic_tool_destination.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import print_function

__version__ = '1.0.0'

from yaml import load

import argparse
Expand All @@ -14,18 +12,21 @@
from functools import reduce
from xml.etree import ElementTree as ET

__version__ = '1.0.0'

# log to galaxy's logger
log = logging.getLogger(__name__)

# does a lot more logging when set to true
verbose = True

"""
list of all valid priorities, inferred from the global
list of all valid priorities, inferred from the global
default_desinations section of the config
"""
priority_list = []


class MalformedYMLException(Exception):
pass

Expand Down Expand Up @@ -398,10 +399,10 @@ def __validate_destination(cls, valid_rule, return_bool, rule, tool, counter):
valid_rule = False
elif isinstance(rule["destination"], dict):
if ("priority" in rule["destination"] and isinstance(rule["destination"]["priority"], dict)):

#### new code
valid_destinations = get_valid_destinations_from_config()

for priority in rule["destination"]["priority"]:
if priority not in priority_list:
error = "Invalid priority: "
Expand All @@ -411,7 +412,7 @@ def __validate_destination(cls, valid_rule, return_bool, rule, tool, counter):
if verbose:
log.debug(error)
valid_rule = False

elif not isinstance(rule["destination"]["priority"][priority], str):
error = "Cannot parse tool destination '"
error += str(rule["destination"]["priority"][priority])
Expand All @@ -422,9 +423,9 @@ def __validate_destination(cls, valid_rule, return_bool, rule, tool, counter):
if verbose:
log.debug(error)
valid_rule = False

elif rule["destination"]["priority"][priority] not in valid_destinations:
error = "destination for '" + str(tool) + "', rule "
error = "destination for '" + str(tool) + "', rule "
error += str(counter) + ": '"
error += str(rule["destination"]["priority"][priority])
error += "' does not exist in job configuration."
Expand All @@ -433,7 +434,7 @@ def __validate_destination(cls, valid_rule, return_bool, rule, tool, counter):
if verbose:
log.debug(error)
valid_rule = False

#### new code ends here
else:
error = "No destination specified for rule " + str(counter)
Expand Down Expand Up @@ -769,26 +770,26 @@ def infinite_defaultdict():
elif isinstance(obj['default_destination'], dict):
if ('priority' in obj['default_destination'] and
isinstance(obj['default_destination']['priority'], dict)):

####My new code

for priority in obj['default_destination']['priority']:

if isinstance(obj['default_destination']['priority'][priority],
str):
str):
if priority not in priority_list:
priority_list.append(priority)

new_config['default']['priority'][priority] = obj[
'default_destination']['priority'][priority]

else:
error = ("Invalid default priority destination '" +
str(priority) + "' found in config!")
if verbose:
log.debug(error)
valid_config = False

if len(priority_list) < 1:
error = ("No valid default priorities found!")
if verbose:
Expand Down Expand Up @@ -820,15 +821,15 @@ def infinite_defaultdict():
if 'priority' in curr and isinstance(curr['priority'], str):
"""
#### my new code
if curr['priority'] in priority_list:
new_config['users'][user]['priority'] = curr['priority']
"""
if curr['priority'] in ['low', 'med', 'high']: ###TODO: ask about user priorities
if curr['priority'] in ['low', 'med', 'high']: ###TODO: ask about user priorities
new_config['users'][user]['priority'] = curr['priority']
else:
error = ("User '" + user + "', priority '"
+ str(curr['priority']) + "' is not valid")
error = ("User '" + user + "', priority '" +
str(curr['priority']) + "' is not valid")
if verbose:
log.debug(error)
valid_config = False
Expand Down Expand Up @@ -866,19 +867,19 @@ def infinite_defaultdict():
new_config['tools'][tool]['default_destination'] = (curr['default_destination'])
tool_has_default = True
elif isinstance(curr['default_destination'], dict):

if ('priority' in curr['default_destination'] and isinstance(curr['default_destination']['priority'], dict)):
####new code
###May not be necessary check if something has all priorities specified as default
for priority in priority_list:
if priority not in curr['default_destination']['priority']:
error = ("No default for destination for priority "
+ str(priority) + " in tool " + str(tool))
if verbose:
log.debug(error)
valid_config = False
## if all_mandatory_classes_defined == True:
error = ("No default for destination for priority " +
str(priority) + " in tool " + str(tool))
if verbose:
log.debug(error)
valid_config = False

# if all_mandatory_classes_defined == True:
for priority in curr['default_destination']['priority']:
destination = curr['default_destination']['priority'][priority]
if priority in priority_list:
Expand All @@ -888,16 +889,16 @@ def infinite_defaultdict():
tool_has_default = True
else:
error = ("No default '" + str(priority) +
"' priority destination for tool "
+ str(tool) + " in config!")
"' priority destination for tool " +
str(tool) + " in config!")
if verbose:
log.debug(error)
valid_config = False

else:
error = ("Invalid default priority '" +
str(priority) + "' for " + str(tool)
+ " found in config!")
str(priority) + "' for " + str(tool) +
" found in config!")
if verbose:
log.debug(error)
valid_config = False
Expand Down Expand Up @@ -1336,7 +1337,7 @@ def map_tool_to_destination(
matched = True
# check if the args in the config file are available
for arg in rule["arguments"]:
arg_dict = {arg : rule["arguments"][arg]}
arg_dict = {arg: rule["arguments"][arg]}
arg_keys_list = []
get_keys_from_dict(arg_dict, arg_keys_list)
try:
Expand Down Expand Up @@ -1417,43 +1418,43 @@ def map_tool_to_destination(
log.debug(output)

return destination
#### new method


def get_valid_destinations_from_config(config_location='/config/job_conf.xml'):
"""
returns A list of all destination IDs declared in the job configuration
@type config_location: str
@param config_location: The location of the job config file relative
to the galaxy root directory. Defaults to
to the galaxy root directory. Defaults to
galaxy/config/job_conf.xml
@rtype: list
@return: A list of all of the destination IDs declared in the job
configuration file.
"""
valid_destinations = []

# os.path.realpath gets the path of DynamicToolDestination.py
# and then os.path.join is used to go back four directories
config_directory = os.path.join(
os.path.dirname(os.path.realpath(__file__)), '../../..')

job_conf_file = config_directory + config_location
job_conf = ET.parse(job_conf_file)

for destination in job_conf.getroot().iter("destination"):
if isinstance(destination.get("id"), str):
valid_destinations.append(destination.get("id"))

else:
error = "Destination ID '" + str(destination)
error += "' in job configuration file cannot be"
error += " parsed. Things may not work as expected!"
log.debug(error)

return valid_destinations


if __name__ == '__main__':
"""
Expand Down

0 comments on commit 6fc2d4d

Please sign in to comment.