Skip to content

Migrate format checking routines from cwltool #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ include cwl_utils/cwlNodeEngineWithContext.js
include testdata/*.cwl
include testdata/*.yaml
include testdata/*.input
include testdata/*.ttl
include testdata/*.owl
include cwl_utils/py.typed
global-exclude *~
global-exclude *.pyc
71 changes: 71 additions & 0 deletions cwl_utils/file_formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
CWL file formats utilities.

For more information, please visit https://www.commonwl.org/user_guide/16-file-formats/
"""

from typing import List, Optional, Set, Union

from rdflib import OWL, RDFS, Graph, URIRef
from schema_salad.exceptions import ValidationException
from schema_salad.utils import aslist, json_dumps

from cwl_utils.types import CWLObjectType


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)}"
)
2 changes: 1 addition & 1 deletion cwl_utils/graph_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from typing import IO, Any, MutableMapping, Set, Union, cast

from cwlformat.formatter import stringify_dict
from ruamel.yaml.dumper import RoundTripDumper
from ruamel.yaml.main import YAML, dump
from ruamel.yaml.representer import RoundTripRepresenter
from ruamel.yaml.dumper import RoundTripDumper
from schema_salad.sourceline import SourceLine, add_lc_filename


Expand Down
8 changes: 4 additions & 4 deletions cwl_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import urllib.parse
import urllib.request
from copy import deepcopy

from ruamel.yaml.main import YAML
from ruamel.yaml.parser import ParserError
from ruamel.yaml.scanner import ScannerError
from typing import (
Any,
Dict,
Expand All @@ -21,6 +17,10 @@
Union,
)

from ruamel.yaml.main import YAML
from ruamel.yaml.parser import ParserError
from ruamel.yaml.scanner import ScannerError

from cwl_utils.errors import MissingKeyField
from cwl_utils.loghandler import _logger

Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cwl-upgrader >= 1.2.3
packaging
rdflib
requests
CacheControl
schema-salad >= 8.2, < 9
Loading