10
10
11
11
import argparse
12
12
import json
13
+ import logging
13
14
import os
15
+ import re
14
16
import sys
15
17
from io import TextIOWrapper
16
18
from pathlib import Path
17
19
from typing import (
18
20
IO ,
19
21
TYPE_CHECKING ,
20
22
Any ,
21
- List ,
22
23
MutableMapping ,
24
+ Optional ,
23
25
Set ,
26
+ TextIO ,
24
27
Union ,
25
28
cast ,
26
- Optional , TextIO ,
27
29
)
28
- import logging
29
- import re
30
30
31
31
from cwlformat .formatter import stringify_dict
32
- from ruamel .yaml .comments import Format
33
32
from ruamel .yaml .main import YAML
34
33
from ruamel .yaml .representer import RoundTripRepresenter
35
34
from schema_salad .sourceline import SourceLine , add_lc_filename
@@ -103,11 +102,11 @@ def run(args: list[str]) -> int:
103
102
104
103
105
104
def 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 ,
111
110
) -> None :
112
111
"""Loop over the provided packed CWL document and split it up."""
113
112
yaml = YAML (typ = "rt" )
@@ -129,7 +128,7 @@ def graph_split(
129
128
os .mkdir (output_dir )
130
129
131
130
def my_represent_none (
132
- self : Any , data : Any
131
+ self : Any , data : Any
133
132
) -> Any : # pylint: disable=unused-argument
134
133
"""Force clean representation of 'null'."""
135
134
return self .represent_scalar ("tag:yaml.org,2002:null" , "null" )
@@ -156,7 +155,9 @@ def my_represent_none(
156
155
yaml_dump (entry , output_file , pretty )
157
156
158
157
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 ]:
160
161
"""Rewrite the given element from the CWL $graph."""
161
162
imports = set ()
162
163
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]
169
170
if key == "run" and isinstance (value , str ) and value [0 ] == "#" :
170
171
document [key ] = f"{ re .sub ('.cwl$' , '' , value [1 :])} .cwl"
171
172
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 :]
173
174
elif key == "out" and isinstance (value , list ):
174
175
175
176
def rewrite_id (entry : Any ) -> Union [MutableMapping [Any , Any ], str ]:
176
177
if isinstance (entry , MutableMapping ):
177
178
if entry ["id" ].startswith (this_id ):
178
179
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 :]
180
181
return entry
181
182
elif isinstance (entry , str ):
182
183
if this_id and entry .startswith (this_id ):
183
- return entry [len (this_id ) + 1 :]
184
+ return entry [len (this_id ) + 1 :]
184
185
return entry
185
186
raise Exception (f"{ entry } is neither a dictionary nor string." )
186
187
187
188
document [key ][:] = [rewrite_id (entry ) for entry in value ]
188
189
elif key in ("source" , "scatter" , "items" , "format" ):
189
190
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
193
194
):
194
195
referrant_file , sub = value [1 :].split ("/" , 1 )
195
196
if referrant_file == doc_id :
@@ -200,7 +201,7 @@ def rewrite_id(entry: Any) -> Union[MutableMapping[Any, Any], str]:
200
201
new_sources = list ()
201
202
for entry in value :
202
203
if entry .startswith ("#" + doc_id ):
203
- new_sources .append (entry [len (doc_id ) + 2 :])
204
+ new_sources .append (entry [len (doc_id ) + 2 :])
204
205
else :
205
206
new_sources .append (entry )
206
207
document [key ] = new_sources
@@ -231,7 +232,7 @@ def rewrite_types(field: Any, entry_file: str, sameself: bool) -> None:
231
232
if key == name :
232
233
if isinstance (value , str ) and value .startswith (entry_file ):
233
234
if sameself :
234
- field [key ] = value [len (entry_file ) + 1 :]
235
+ field [key ] = value [len (entry_file ) + 1 :]
235
236
else :
236
237
field [key ] = "{d[0]}#{d[1]}" .format (
237
238
d = value [1 :].split ("/" , 1 )
@@ -243,7 +244,9 @@ def rewrite_types(field: Any, entry_file: str, sameself: bool) -> None:
243
244
rewrite_types (entry , entry_file , sameself )
244
245
245
246
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 ]:
247
250
"""Dump the schemadefs to their own file."""
248
251
for entry in document ["types" ]:
249
252
if "$import" in entry :
@@ -281,7 +284,11 @@ def json_dump(entry: Any, output_file: str) -> None:
281
284
json .dump (entry , result_handle , indent = 4 )
282
285
283
286
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 :
285
292
"""Output object as YAML."""
286
293
yaml = YAML (typ = "rt" , pure = True )
287
294
yaml .default_flow_style = False
@@ -293,21 +300,16 @@ def yaml_dump(entry: Any, output_file_or_handle: Optional[Union[str, Path, TextI
293
300
if pretty :
294
301
result_handle .write (stringify_dict (entry ))
295
302
else :
296
- yaml .dump (
297
- entry ,
298
- result_handle
299
- )
303
+ yaml .dump (entry , result_handle )
300
304
elif isinstance (output_file_or_handle , (TextIOWrapper , TextIO )):
301
305
if pretty :
302
306
output_file_or_handle .write (stringify_dict (entry ))
303
307
else :
304
- yaml .dump (
305
- entry ,
306
- output_file_or_handle
307
- )
308
+ yaml .dump (entry , output_file_or_handle )
308
309
else :
309
310
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
+ )
311
313
312
314
313
315
if __name__ == "__main__" :
0 commit comments