1212import json
1313import os
1414import 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
1717from cwlformat .formatter import stringify_dict
1818from ruamel .yaml .dumper import RoundTripDumper
1919from ruamel .yaml .main import YAML , dump
2020from ruamel .yaml .representer import RoundTripRepresenter
2121from schema_salad .sourceline import SourceLine , add_lc_filename
2222
23+ if TYPE_CHECKING :
24+ from _typeshed import StrPath
25+
2326
2427def arg_parser () -> argparse .ArgumentParser :
2528 """Build the argument parser."""
@@ -78,7 +81,11 @@ def run(args: List[str]) -> int:
7881
7982
8083def 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 ]
0 commit comments