Skip to content

Commit

Permalink
check 5k prefix for class for checking potential yaml and cwl tools
Browse files Browse the repository at this point in the history
  • Loading branch information
bernt-matthias committed Mar 23, 2022
1 parent 25592e3 commit 3357036
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions lib/galaxy/tool_util/loader_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,41 @@ def looks_like_a_data_manager_xml(path):
return looks_like_xml(path=path, regex=DATA_MANAGER_REGEX)


def is_a_yaml_with_class(path, classes):
"""Determine if a file is a valid YAML with a supplied ``class`` entry."""
if not _has_extension(path, YAML_EXTENSIONS):
return False
def as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes):
"""
get a dict from yaml file if it contains `class: CLASS`, where CLASS is
any string given in CLASSES. must appear in the first 5k and also load
properly in total.
"""
with open(path, encoding="utf-8") as f:
try:
start_contents = f.read(5 * 1024)
except UnicodeDecodeError:
return None
if re.search(rf"^class:\s+({'|'.join(classes)})\s*$", start_contents) is None:
return None

with open(path) as f:
try:
as_dict = yaml.safe_load(f)
except Exception:
return False
return None

if not isinstance(as_dict, dict):
return False
return None

file_class = as_dict.get("class", None)
return file_class in classes
if file_class not in classes:
return None

return as_dict


def is_a_yaml_with_class(path, classes):
"""Determine if a file is a valid YAML with a supplied ``class`` entry."""
if not _has_extension(path, YAML_EXTENSIONS):
return False
return as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes) is not None


def looks_like_a_tool_yaml(path):
Expand All @@ -216,17 +235,8 @@ def looks_like_a_cwl_artifact(path, classes=None):
if not _has_extension(path, CWL_EXTENSIONS):
return False

with open(path) as f:
try:
as_dict = yaml.safe_load(f)
except Exception:
return False

if not isinstance(as_dict, dict):
return False

file_class = as_dict.get("class", None)
if classes is not None and file_class not in classes:
as_dict = as_dict_if_looks_like_yaml_or_cwl_with_class(path, classes)
if as_dict is None:
return False

file_cwl_version = as_dict.get("cwlVersion", None)
Expand Down

0 comments on commit 3357036

Please sign in to comment.