Skip to content

Commit 23213e4

Browse files
committed
printrdf & printdot: add support for multi-document CWL files
1 parent 7934640 commit 23213e4

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

cwltool/cwlrdf.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Any, Union, Dict, IO
77

88
def makerdf(workflow, wf, ctx):
9-
# type: (Union[str, unicode], Dict[unicode, Any], Loader.ContextType) -> Graph
9+
# type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType) -> Graph
1010
prefixes = {}
1111
for k,v in ctx.iteritems():
1212
if isinstance(v, dict):
@@ -18,7 +18,11 @@ def makerdf(workflow, wf, ctx):
1818
p, _ = frg.split("/")
1919
prefixes[p] = u"%s#%s/" % (doc_url, p)
2020

21-
wf["@context"] = ctx
21+
if isinstance(wf, list):
22+
for entry in wf:
23+
entry["@context"] = ctx
24+
else:
25+
wf["@context"] = ctx
2226
g = Graph().parse(data=json.dumps(wf), format='json-ld', location=workflow)
2327

2428
# Bug in json-ld loader causes @id fields to be added to the graph
@@ -31,7 +35,7 @@ def makerdf(workflow, wf, ctx):
3135
return g
3236

3337
def printrdf(workflow, wf, ctx, sr, stdout):
34-
# type: (Union[str, unicode], Dict[unicode, Any], Loader.ContextType, str, IO[Any]) -> None
38+
# type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType, str, IO[Any]) -> None
3539
stdout.write(makerdf(workflow, wf, ctx).serialize(format=sr))
3640

3741
def lastpart(uri): # type: (Any) -> str
@@ -172,7 +176,7 @@ def dot_without_parameters(g, stdout): # type: (Graph, IO[Any]) -> None
172176

173177

174178
def printdot(workflow, wf, ctx, stdout, include_parameters=False):
175-
# type: (Union[str, unicode], Dict[unicode, Any], Loader.ContextType, Any, bool) -> None
179+
# type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType, Any, bool) -> None
176180
g = makerdf(workflow, wf, ctx)
177181

178182
stdout.write("digraph {")

cwltool/load_tool.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def fetch_document(argsworkflow):
4444

4545
def validate_document(document_loader, workflowobj, uri,
4646
enable_dev=False, strict=True, preprocess_only=False):
47-
# type: (Loader, Dict[unicode, Any], unicode, bool, bool, bool) -> Tuple[Loader, Names, Dict[unicode, Any], Dict[unicode, Any], unicode]
47+
# type: (Loader, Dict[unicode, Any], unicode, bool, bool, bool) -> Tuple[Loader, Names, Union[Dict[unicode, Any], List[Dict[unicode, Any]]], Dict[unicode, Any], unicode]
4848
"""Validate a CWL document."""
4949
jobobj = None
5050
if "cwl:tool" in workflowobj:
@@ -83,10 +83,13 @@ def validate_document(document_loader, workflowobj, uri,
8383

8484
workflowobj["id"] = fileuri
8585
processobj, metadata = document_loader.resolve_all(workflowobj, fileuri)
86-
if not isinstance(processobj, dict):
87-
raise validate.ValidationException("Workflow must be a dict.")
86+
if not isinstance(processobj, (dict, list)):
87+
raise validate.ValidationException("Workflow must be a dict or list.")
8888

8989
if not metadata:
90+
if not isinstance(processobj, dict):
91+
raise validate.ValidationException(
92+
"Draft-2 workflows must be a dict.")
9093
metadata = {"$namespaces": processobj.get("$namespaces", {}),
9194
"$schemas": processobj.get("$schemas", []),
9295
"cwlVersion": processobj["cwlVersion"]}
@@ -106,9 +109,8 @@ def validate_document(document_loader, workflowobj, uri,
106109
return document_loader, avsc_names, processobj, metadata, uri
107110

108111

109-
def make_tool(document_loader, avsc_names, processobj, metadata, uri, makeTool,
110-
kwargs):
111-
# type: (Loader, Names, Dict[unicode, Any], Dict[unicode, Any], unicode, Callable[..., Process], Dict[str, Any]) -> Process
112+
def make_tool(document_loader, avsc_names, metadata, uri, makeTool, kwargs):
113+
# type: (Loader, Names, Dict[unicode, Any], unicode, Callable[..., Process], Dict[str, Any]) -> Process
112114
"""Make a Python CWL object."""
113115
resolveduri = document_loader.resolve_ref(uri)[0]
114116

@@ -122,7 +124,7 @@ def make_tool(document_loader, avsc_names, processobj, metadata, uri, makeTool,
122124
urlparse.urldefrag(i["id"])[1] for i in resolveduri
123125
if "id" in i))
124126
else:
125-
processobj = cast(Dict[unicode, Any], resolveduri)
127+
processobj = resolveduri
126128

127129
kwargs = kwargs.copy()
128130
kwargs.update({
@@ -150,5 +152,5 @@ def load_tool(argsworkflow, makeTool, kwargs=None,
150152
document_loader, avsc_names, processobj, metadata, uri = validate_document(
151153
document_loader, workflowobj, uri, enable_dev=enable_dev,
152154
strict=strict)
153-
return make_tool(document_loader, avsc_names, processobj, metadata, uri,
155+
return make_tool(document_loader, avsc_names, metadata, uri,
154156
makeTool, kwargs if kwargs else {})

cwltool/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ def main(argsl=None,
593593
printdot(uri, processobj, document_loader.ctx, stdout)
594594
return 0
595595

596-
tool = make_tool(document_loader, avsc_names, processobj, metadata,
597-
uri, makeTool, {})
596+
tool = make_tool(document_loader, avsc_names, metadata, uri,
597+
makeTool, {})
598598
except (validate.ValidationException) as exc:
599599
_logger.error(u"Tool definition failed validation:\n%s", exc,
600600
exc_info=(exc if args.debug else False))

cwltool/update.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def identity(doc, loader, baseuri): # pylint: disable=unused-argument
386386
return (doc, doc["cwlVersion"])
387387

388388
def checkversion(doc, metadata, enable_dev):
389-
# type: (Union[List, Dict[unicode, Any]], Dict[unicode, Any], bool) -> Tuple[Dict[unicode, Any], unicode] # pylint: disable=line-too-long
389+
# type: (Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Dict[unicode, Any], bool) -> Tuple[Dict[unicode, Any], unicode] # pylint: disable=line-too-long
390390
"""Checks the validity of the version of the give CWL document.
391391
392392
Returns the document and the validated version string.
@@ -418,7 +418,7 @@ def checkversion(doc, metadata, enable_dev):
418418
return (cdoc, version)
419419

420420
def update(doc, loader, baseuri, enable_dev, metadata):
421-
# type: (Union[List, Dict[unicode, Any]], Loader, str, bool, Any) -> Dict[unicode, Any]
421+
# type: (Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader, str, bool, Any) -> Dict[unicode, Any]
422422

423423
(cdoc, version) = checkversion(doc, metadata, enable_dev)
424424

0 commit comments

Comments
 (0)