Skip to content

Commit

Permalink
Import check_format from cwl-utils (#1717)
Browse files Browse the repository at this point in the history
  • Loading branch information
GlassOfWhiskey authored Aug 19, 2022
1 parent 49fc1c3 commit 0e2ced5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 64 deletions.
63 changes: 2 additions & 61 deletions cwltool/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
MutableMapping,
MutableSequence,
Optional,
Set,
Union,
cast,
)

from cwl_utils import expression
from rdflib import Graph, URIRef
from rdflib.namespace import OWL, RDFS
from cwl_utils.file_formats import check_format
from rdflib import Graph
from ruamel.yaml.comments import CommentedMap
from schema_salad.avro.schema import Names, Schema, make_avsc_object
from schema_salad.exceptions import ValidationException
Expand Down Expand Up @@ -76,64 +75,6 @@ def substitute(value, replace): # type: (str, str) -> str
return value + replace


def formatSubclassOf(
fmt: str, cls: str, ontology: Optional[Graph], visited: Set[str]
) -> bool:
"""Determine if `fmt` is a subclass of `cls`."""
if URIRef(fmt) == URIRef(cls):
return True

if ontology is None:
return False

if fmt in visited:
return False

visited.add(fmt)

uriRefFmt = URIRef(fmt)

for _s, _p, o in ontology.triples((uriRefFmt, RDFS.subClassOf, None)):
# Find parent classes of `fmt` and search upward
if formatSubclassOf(o, cls, ontology, visited):
return True

for _s, _p, o in ontology.triples((uriRefFmt, OWL.equivalentClass, None)):
# Find equivalent classes of `fmt` and search horizontally
if formatSubclassOf(o, cls, ontology, visited):
return True

for s, _p, _o in ontology.triples((None, OWL.equivalentClass, uriRefFmt)):
# Find equivalent classes of `fmt` and search horizontally
if formatSubclassOf(s, cls, ontology, visited):
return True

return False


def check_format(
actual_file: Union[CWLObjectType, List[CWLObjectType]],
input_formats: Union[List[str], str],
ontology: Optional[Graph],
) -> None:
"""Confirm that the format present is valid for the allowed formats."""
for afile in aslist(actual_file):
if not afile:
continue
if "format" not in afile:
raise ValidationException(
f"File has no 'format' defined: {json_dumps(afile, indent=4)}"
)
for inpf in aslist(input_formats):
if afile["format"] == inpf or formatSubclassOf(
afile["format"], inpf, ontology, set()
):
return
raise ValidationException(
f"File has an incompatible format: {json_dumps(afile, indent=4)}"
)


class Builder(HasReqsHints):
def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion mypy-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mypy==0.971
ruamel.yaml>=0.16.0,<0.17.22
schema-salad>=8.2.20211104054942,<9
cwl-utils>=0.14
cwl-utils>=0.15
types-requests
types-setuptools
types-psutil
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pydot>=1.4.1
argcomplete>=1.12.0
pyparsing != 3.0.2 # breaks --print-dot (pydot) https://github.com/pyparsing/pyparsing/issues/319
pyparsing < 3;python_version<='3.6' # breaks --print-dot
cwl-utils>=0.14
cwl-utils>=0.15
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"pyparsing != 3.0.2", # breaks --print-dot (pydot) https://github.com/pyparsing/pyparsing/issues/319
"pyparsing < 3 ;python_version<='3.6'", # breaks --print-dot (pydot)
"argcomplete",
"cwl-utils >= 0.14",
"cwl-utils >= 0.15",
],
extras_require={
"deps": ["galaxy-tool-util >= 21.1.0"],
Expand Down

0 comments on commit 0e2ced5

Please sign in to comment.