1010
1111import argparse
1212import json
13+ import logging
1314import os
15+ import re
1416import sys
1517from io import TextIOWrapper
1618from pathlib import Path
1719from typing import (
1820 IO ,
1921 TYPE_CHECKING ,
2022 Any ,
21- List ,
2223 MutableMapping ,
24+ Optional ,
2325 Set ,
26+ TextIO ,
2427 Union ,
2528 cast ,
26- Optional , TextIO ,
2729)
28- import logging
29- import re
3030
3131from cwlformat .formatter import stringify_dict
32- from ruamel .yaml .comments import Format
3332from ruamel .yaml .main import YAML
3433from ruamel .yaml .representer import RoundTripRepresenter
3534from schema_salad .sourceline import SourceLine , add_lc_filename
@@ -103,11 +102,11 @@ def run(args: list[str]) -> int:
103102
104103
105104def graph_split (
106- sourceIO : IO [str ],
107- output_dir : "StrPath" ,
108- output_format : str ,
109- mainfile : str ,
110- pretty : bool ,
105+ sourceIO : IO [str ],
106+ output_dir : "StrPath" ,
107+ output_format : str ,
108+ mainfile : str ,
109+ pretty : bool ,
111110) -> None :
112111 """Loop over the provided packed CWL document and split it up."""
113112 yaml = YAML (typ = "rt" )
@@ -129,7 +128,7 @@ def graph_split(
129128 os .mkdir (output_dir )
130129
131130 def my_represent_none (
132- self : Any , data : Any
131+ self : Any , data : Any
133132 ) -> Any : # pylint: disable=unused-argument
134133 """Force clean representation of 'null'."""
135134 return self .represent_scalar ("tag:yaml.org,2002:null" , "null" )
@@ -156,7 +155,9 @@ def my_represent_none(
156155 yaml_dump (entry , output_file , pretty )
157156
158157
159- def rewrite (document : Any , doc_id : str , output_dir : Path , pretty : Optional [bool ] = False ) -> Set [str ]:
158+ def rewrite (
159+ document : Any , doc_id : str , output_dir : Path , pretty : Optional [bool ] = False
160+ ) -> Set [str ]:
160161 """Rewrite the given element from the CWL $graph."""
161162 imports = set ()
162163 if isinstance (document , list ) and not isinstance (document , str ):
@@ -169,27 +170,27 @@ def rewrite(document: Any, doc_id: str, output_dir: Path, pretty: Optional[bool]
169170 if key == "run" and isinstance (value , str ) and value [0 ] == "#" :
170171 document [key ] = f"{ re .sub ('.cwl$' , '' , value [1 :])} .cwl"
171172 elif key in ("id" , "outputSource" ) and value .startswith ("#" + doc_id ):
172- document [key ] = value [len (doc_id ) + 2 :]
173+ document [key ] = value [len (doc_id ) + 2 :]
173174 elif key == "out" and isinstance (value , list ):
174175
175176 def rewrite_id (entry : Any ) -> Union [MutableMapping [Any , Any ], str ]:
176177 if isinstance (entry , MutableMapping ):
177178 if entry ["id" ].startswith (this_id ):
178179 assert isinstance (this_id , str ) # nosec B101
179- entry ["id" ] = cast (str , entry ["id" ])[len (this_id ) + 1 :]
180+ entry ["id" ] = cast (str , entry ["id" ])[len (this_id ) + 1 :]
180181 return entry
181182 elif isinstance (entry , str ):
182183 if this_id and entry .startswith (this_id ):
183- return entry [len (this_id ) + 1 :]
184+ return entry [len (this_id ) + 1 :]
184185 return entry
185186 raise Exception (f"{ entry } is neither a dictionary nor string." )
186187
187188 document [key ][:] = [rewrite_id (entry ) for entry in value ]
188189 elif key in ("source" , "scatter" , "items" , "format" ):
189190 if (
190- isinstance (value , str )
191- and value .startswith ("#" )
192- and "/" in value
191+ isinstance (value , str )
192+ and value .startswith ("#" )
193+ and "/" in value
193194 ):
194195 referrant_file , sub = value [1 :].split ("/" , 1 )
195196 if referrant_file == doc_id :
@@ -200,7 +201,7 @@ def rewrite_id(entry: Any) -> Union[MutableMapping[Any, Any], str]:
200201 new_sources = list ()
201202 for entry in value :
202203 if entry .startswith ("#" + doc_id ):
203- new_sources .append (entry [len (doc_id ) + 2 :])
204+ new_sources .append (entry [len (doc_id ) + 2 :])
204205 else :
205206 new_sources .append (entry )
206207 document [key ] = new_sources
@@ -231,7 +232,7 @@ def rewrite_types(field: Any, entry_file: str, sameself: bool) -> None:
231232 if key == name :
232233 if isinstance (value , str ) and value .startswith (entry_file ):
233234 if sameself :
234- field [key ] = value [len (entry_file ) + 1 :]
235+ field [key ] = value [len (entry_file ) + 1 :]
235236 else :
236237 field [key ] = "{d[0]}#{d[1]}" .format (
237238 d = value [1 :].split ("/" , 1 )
@@ -243,7 +244,9 @@ def rewrite_types(field: Any, entry_file: str, sameself: bool) -> None:
243244 rewrite_types (entry , entry_file , sameself )
244245
245246
246- def rewrite_schemadef (document : MutableMapping [str , Any ], output_dir : Path , pretty : Optional [bool ] = False ) -> Set [str ]:
247+ def rewrite_schemadef (
248+ document : MutableMapping [str , Any ], output_dir : Path , pretty : Optional [bool ] = False
249+ ) -> Set [str ]:
247250 """Dump the schemadefs to their own file."""
248251 for entry in document ["types" ]:
249252 if "$import" in entry :
@@ -281,7 +284,11 @@ def json_dump(entry: Any, output_file: str) -> None:
281284 json .dump (entry , result_handle , indent = 4 )
282285
283286
284- def yaml_dump (entry : Any , output_file_or_handle : Optional [Union [str , Path , TextIOWrapper , TextIO ]], pretty : bool ) -> None :
287+ def yaml_dump (
288+ entry : Any ,
289+ output_file_or_handle : Optional [Union [str , Path , TextIOWrapper , TextIO ]],
290+ pretty : bool ,
291+ ) -> None :
285292 """Output object as YAML."""
286293 yaml = YAML (typ = "rt" , pure = True )
287294 yaml .default_flow_style = False
@@ -293,21 +300,16 @@ def yaml_dump(entry: Any, output_file_or_handle: Optional[Union[str, Path, TextI
293300 if pretty :
294301 result_handle .write (stringify_dict (entry ))
295302 else :
296- yaml .dump (
297- entry ,
298- result_handle
299- )
303+ yaml .dump (entry , result_handle )
300304 elif isinstance (output_file_or_handle , (TextIOWrapper , TextIO )):
301305 if pretty :
302306 output_file_or_handle .write (stringify_dict (entry ))
303307 else :
304- yaml .dump (
305- entry ,
306- output_file_or_handle
307- )
308+ yaml .dump (entry , output_file_or_handle )
308309 else :
309310 raise ValueError (
310- f"output_file_or_handle must be a string or a file handle but got { type (output_file_or_handle )} " )
311+ f"output_file_or_handle must be a string or a file handle but got { type (output_file_or_handle )} "
312+ )
311313
312314
313315if __name__ == "__main__" :
0 commit comments