12
12
import json
13
13
import os
14
14
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
16
16
17
17
from cwlformat .formatter import stringify_dict
18
18
from ruamel .yaml .dumper import RoundTripDumper
19
19
from ruamel .yaml .main import YAML , dump
20
20
from ruamel .yaml .representer import RoundTripRepresenter
21
21
from schema_salad .sourceline import SourceLine , add_lc_filename
22
22
23
+ if TYPE_CHECKING :
24
+ from _typeshed import StrPath
25
+
23
26
24
27
def arg_parser () -> argparse .ArgumentParser :
25
28
"""Build the argument parser."""
@@ -78,7 +81,11 @@ def run(args: List[str]) -> int:
78
81
79
82
80
83
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 ,
82
89
) -> None :
83
90
"""Loop over the provided packed CWL document and split it up."""
84
91
yaml = YAML (typ = "rt" )
@@ -101,7 +108,7 @@ def my_represent_none(
101
108
RoundTripRepresenter .add_representer (type (None ), my_represent_none )
102
109
103
110
for entry in source ["$graph" ]:
104
- entry_id = entry .pop ("id" )[ 1 :]
111
+ entry_id = entry .pop ("id" ). lstrip ( "#" )
105
112
entry ["cwlVersion" ] = version
106
113
imports = rewrite (entry , entry_id )
107
114
if imports :
@@ -113,7 +120,7 @@ def my_represent_none(
113
120
else :
114
121
entry_id = mainfile
115
122
116
- output_file = os .path .join (output_dir , entry_id )
123
+ output_file = os .path .join (output_dir , entry_id + ".cwl" )
117
124
if output_format == "json" :
118
125
json_dump (entry , output_file )
119
126
elif output_format == "yaml" :
@@ -131,19 +138,20 @@ def rewrite(document: Any, doc_id: str) -> Set[str]:
131
138
for key , value in document .items ():
132
139
with SourceLine (document , key , Exception ):
133
140
if key == "run" and isinstance (value , str ) and value [0 ] == "#" :
134
- document [key ] = value [1 :]
141
+ document [key ] = f" { value [1 :]} .cwl"
135
142
elif key in ("id" , "outputSource" ) and value .startswith ("#" + doc_id ):
136
143
document [key ] = value [len (doc_id ) + 2 :]
137
- elif key == "out" :
144
+ elif key == "out" and isinstance ( value , list ) :
138
145
139
146
def rewrite_id (entry : Any ) -> Union [MutableMapping [Any , Any ], str ]:
140
147
if isinstance (entry , MutableMapping ):
141
148
if entry ["id" ].startswith (this_id ):
142
149
entry ["id" ] = cast (str , entry ["id" ])[len (this_id ) + 1 :]
143
150
return entry
144
151
elif isinstance (entry , str ):
145
- if entry .startswith (this_id ):
152
+ if this_id and entry .startswith (this_id ):
146
153
return entry [len (this_id ) + 1 :]
154
+ return entry
147
155
raise Exception (f"{ entry } is neither a dictionary nor string." )
148
156
149
157
document [key ][:] = [rewrite_id (entry ) for entry in value ]
0 commit comments