Skip to content

Commit d857e21

Browse files
committed
add offline graph-split test + fix
1 parent 02e49a3 commit d857e21

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

cwl_utils/graph_split.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
import json
1313
import os
1414
import sys
15-
from typing import IO, Any, List, MutableMapping, Set, Union, cast
15+
from typing import IO, TYPE_CHECKING, Any, List, MutableMapping, Set, Union, cast
1616

1717
from cwlformat.formatter import stringify_dict
1818
from ruamel.yaml.dumper import RoundTripDumper
1919
from ruamel.yaml.main import YAML, dump
2020
from ruamel.yaml.representer import RoundTripRepresenter
2121
from schema_salad.sourceline import SourceLine, add_lc_filename
2222

23+
if TYPE_CHECKING:
24+
from _typeshed import StrPath
25+
2326

2427
def arg_parser() -> argparse.ArgumentParser:
2528
"""Build the argument parser."""
@@ -78,7 +81,11 @@ def run(args: List[str]) -> int:
7881

7982

8083
def graph_split(
81-
sourceIO: IO[str], output_dir: str, output_format: str, mainfile: str, pretty: bool
84+
sourceIO: IO[str],
85+
output_dir: "StrPath",
86+
output_format: str,
87+
mainfile: str,
88+
pretty: bool,
8289
) -> None:
8390
"""Loop over the provided packed CWL document and split it up."""
8491
yaml = YAML(typ="rt")
@@ -101,7 +108,7 @@ def my_represent_none(
101108
RoundTripRepresenter.add_representer(type(None), my_represent_none)
102109

103110
for entry in source["$graph"]:
104-
entry_id = entry.pop("id")[1:]
111+
entry_id = entry.pop("id").lstrip("#")
105112
entry["cwlVersion"] = version
106113
imports = rewrite(entry, entry_id)
107114
if imports:
@@ -113,7 +120,7 @@ def my_represent_none(
113120
else:
114121
entry_id = mainfile
115122

116-
output_file = os.path.join(output_dir, entry_id)
123+
output_file = os.path.join(output_dir, entry_id + ".cwl")
117124
if output_format == "json":
118125
json_dump(entry, output_file)
119126
elif output_format == "yaml":
@@ -131,19 +138,20 @@ def rewrite(document: Any, doc_id: str) -> Set[str]:
131138
for key, value in document.items():
132139
with SourceLine(document, key, Exception):
133140
if key == "run" and isinstance(value, str) and value[0] == "#":
134-
document[key] = value[1:]
141+
document[key] = f"{value[1:]}.cwl"
135142
elif key in ("id", "outputSource") and value.startswith("#" + doc_id):
136143
document[key] = value[len(doc_id) + 2 :]
137-
elif key == "out":
144+
elif key == "out" and isinstance(value, list):
138145

139146
def rewrite_id(entry: Any) -> Union[MutableMapping[Any, Any], str]:
140147
if isinstance(entry, MutableMapping):
141148
if entry["id"].startswith(this_id):
142149
entry["id"] = cast(str, entry["id"])[len(this_id) + 1 :]
143150
return entry
144151
elif isinstance(entry, str):
145-
if entry.startswith(this_id):
152+
if this_id and entry.startswith(this_id):
146153
return entry[len(this_id) + 1 :]
154+
return entry
147155
raise Exception(f"{entry} is neither a dictionary nor string.")
148156

149157
document[key][:] = [rewrite_id(entry) for entry in value]

tests/test_graph_split.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88

99
from cwl_utils.graph_split import graph_split
1010

11+
from .util import get_data
12+
1113
URI = "https://gist.githubusercontent.com/altairwei/6a0097db95cad23de36f825ed3b9f4b0/raw/83f332931c3093ee73554cd7f60054ce17d03239/rhapsody_wta_1.8.packed.cwl"
1214

1315

1416
def test_graph_split(tmp_path: Path) -> None:
1517
"""Confirm that a user provided example produces no exception."""
16-
os.chdir(tmp_path)
1718
sourceIO = StringIO(requests.get(URI).text)
1819
sourceIO.name = URI
19-
graph_split(sourceIO, ".", "yaml", "main.cwl", True)
20+
graph_split(sourceIO, tmp_path, "yaml", "main.cwl", True)
21+
22+
23+
def test_graph_split_offline(tmp_path: Path) -> None:
24+
"""Confirm that a local provided example produces no exception."""
25+
with open(get_data("testdata/js-expr-req-wf.cwl")) as handle:
26+
graph_split(handle, tmp_path, "yaml", "main.cwl", True)

0 commit comments

Comments
 (0)