Skip to content

Commit a38de2e

Browse files
GlassOfWhiskeymr-c
andauthored
Migrate format checking routines from cwltool (#141)
* GA: fix pydocstyle test Co-authored-by: Michael R. Crusoe <[email protected]>
1 parent 6fbab67 commit a38de2e

File tree

9 files changed

+51515
-8
lines changed

9 files changed

+51515
-8
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ include cwl_utils/cwlNodeEngineWithContext.js
99
include testdata/*.cwl
1010
include testdata/*.yaml
1111
include testdata/*.input
12+
include testdata/*.ttl
13+
include testdata/*.owl
1214
include cwl_utils/py.typed
1315
global-exclude *~
1416
global-exclude *.pyc

cwl_utils/file_formats.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
CWL file formats utilities.
3+
4+
For more information, please visit https://www.commonwl.org/user_guide/16-file-formats/
5+
"""
6+
7+
from typing import List, Optional, Set, Union
8+
9+
from rdflib import OWL, RDFS, Graph, URIRef
10+
from schema_salad.exceptions import ValidationException
11+
from schema_salad.utils import aslist, json_dumps
12+
13+
from cwl_utils.types import CWLObjectType
14+
15+
16+
def formatSubclassOf(
17+
fmt: str, cls: str, ontology: Optional[Graph], visited: Set[str]
18+
) -> bool:
19+
"""Determine if `fmt` is a subclass of `cls`."""
20+
if URIRef(fmt) == URIRef(cls):
21+
return True
22+
23+
if ontology is None:
24+
return False
25+
26+
if fmt in visited:
27+
return False
28+
29+
visited.add(fmt)
30+
31+
uriRefFmt = URIRef(fmt)
32+
33+
for _s, _p, o in ontology.triples((uriRefFmt, RDFS.subClassOf, None)):
34+
# Find parent classes of `fmt` and search upward
35+
if formatSubclassOf(o, cls, ontology, visited):
36+
return True
37+
38+
for _s, _p, o in ontology.triples((uriRefFmt, OWL.equivalentClass, None)):
39+
# Find equivalent classes of `fmt` and search horizontally
40+
if formatSubclassOf(o, cls, ontology, visited):
41+
return True
42+
43+
for s, _p, _o in ontology.triples((None, OWL.equivalentClass, uriRefFmt)):
44+
# Find equivalent classes of `fmt` and search horizontally
45+
if formatSubclassOf(s, cls, ontology, visited):
46+
return True
47+
48+
return False
49+
50+
51+
def check_format(
52+
actual_file: Union[CWLObjectType, List[CWLObjectType]],
53+
input_formats: Union[List[str], str],
54+
ontology: Optional[Graph],
55+
) -> None:
56+
"""Confirm that the format present is valid for the allowed formats."""
57+
for afile in aslist(actual_file):
58+
if not afile:
59+
continue
60+
if "format" not in afile:
61+
raise ValidationException(
62+
f"File has no 'format' defined: {json_dumps(afile, indent=4)}"
63+
)
64+
for inpf in aslist(input_formats):
65+
if afile["format"] == inpf or formatSubclassOf(
66+
afile["format"], inpf, ontology, set()
67+
):
68+
return
69+
raise ValidationException(
70+
f"File has an incompatible format: {json_dumps(afile, indent=4)}"
71+
)

cwl_utils/graph_split.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from typing import IO, Any, MutableMapping, Set, Union, cast
1515

1616
from cwlformat.formatter import stringify_dict
17+
from ruamel.yaml.dumper import RoundTripDumper
1718
from ruamel.yaml.main import YAML, dump
1819
from ruamel.yaml.representer import RoundTripRepresenter
19-
from ruamel.yaml.dumper import RoundTripDumper
2020
from schema_salad.sourceline import SourceLine, add_lc_filename
2121

2222

cwl_utils/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
import urllib.parse
77
import urllib.request
88
from copy import deepcopy
9-
10-
from ruamel.yaml.main import YAML
11-
from ruamel.yaml.parser import ParserError
12-
from ruamel.yaml.scanner import ScannerError
139
from typing import (
1410
Any,
1511
Dict,
@@ -21,6 +17,10 @@
2117
Union,
2218
)
2319

20+
from ruamel.yaml.main import YAML
21+
from ruamel.yaml.parser import ParserError
22+
from ruamel.yaml.scanner import ScannerError
23+
2424
from cwl_utils.errors import MissingKeyField
2525
from cwl_utils.loghandler import _logger
2626

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
cwl-upgrader >= 1.2.3
22
packaging
3+
rdflib
34
requests
5+
CacheControl
46
schema-salad >= 8.2, < 9

0 commit comments

Comments
 (0)