18
18
from pathlib import Path
19
19
from typing import (
20
20
IO ,
21
- TYPE_CHECKING ,
22
21
Any ,
23
22
MutableMapping ,
24
- Optional ,
25
23
Set ,
26
24
TextIO ,
27
25
Union ,
35
33
36
34
from cwl_utils .loghandler import _logger as _cwlutilslogger
37
35
38
- if TYPE_CHECKING :
39
- from _typeshed import StrPath
40
-
41
36
_logger = logging .getLogger ("cwl-graph-split" ) # pylint: disable=invalid-name
42
37
defaultStreamHandler = logging .StreamHandler () # pylint: disable=invalid-name
43
38
_logger .addHandler (defaultStreamHandler )
@@ -93,7 +88,7 @@ def run(args: list[str]) -> int:
93
88
with open (options .cwlfile ) as source_handle :
94
89
graph_split (
95
90
source_handle ,
96
- options .outdir ,
91
+ Path ( options .outdir ) ,
97
92
options .output_format ,
98
93
options .mainfile ,
99
94
options .pretty ,
@@ -103,7 +98,7 @@ def run(args: list[str]) -> int:
103
98
104
99
def graph_split (
105
100
sourceIO : IO [str ],
106
- output_dir : "StrPath" ,
101
+ output_dir : Path ,
107
102
output_format : str ,
108
103
mainfile : str ,
109
104
pretty : bool ,
@@ -121,11 +116,11 @@ def graph_split(
121
116
version = source .pop ("cwlVersion" )
122
117
123
118
# Check outdir parent exists
124
- if not Path ( output_dir ) .parent .is_dir ():
119
+ if not output_dir .parent .is_dir ():
125
120
raise NotADirectoryError (f"Parent directory of { output_dir } does not exist" )
126
121
# If output_dir is not a directory, create it
127
- if not Path ( output_dir ) .is_dir ():
128
- os .mkdir (output_dir )
122
+ if not output_dir .is_dir ():
123
+ output_dir .mkdir ()
129
124
130
125
def my_represent_none (
131
126
self : Any , data : Any
@@ -138,7 +133,7 @@ def my_represent_none(
138
133
for entry in source ["$graph" ]:
139
134
entry_id = entry .pop ("id" ).lstrip ("#" )
140
135
entry ["cwlVersion" ] = version
141
- imports = rewrite (entry , entry_id , Path ( output_dir ) )
136
+ imports = rewrite (entry , entry_id , output_dir )
142
137
if imports :
143
138
for import_name in imports :
144
139
rewrite_types (entry , f"#{ import_name } " , False )
@@ -148,15 +143,15 @@ def my_represent_none(
148
143
else :
149
144
entry_id = mainfile
150
145
151
- output_file = Path ( output_dir ) / (re .sub (".cwl$" , "" , entry_id ) + ".cwl" )
146
+ output_file = output_dir / (re .sub (".cwl$" , "" , entry_id ) + ".cwl" )
152
147
if output_format == "json" :
153
148
json_dump (entry , output_file )
154
149
elif output_format == "yaml" :
155
150
yaml_dump (entry , output_file , pretty )
156
151
157
152
158
153
def rewrite (
159
- document : Any , doc_id : str , output_dir : Path , pretty : Optional [ bool ] = False
154
+ document : Any , doc_id : str , output_dir : Path , pretty : bool = False
160
155
) -> Set [str ]:
161
156
"""Rewrite the given element from the CWL $graph."""
162
157
imports = set ()
@@ -245,7 +240,7 @@ def rewrite_types(field: Any, entry_file: str, sameself: bool) -> None:
245
240
246
241
247
242
def rewrite_schemadef (
248
- document : MutableMapping [str , Any ], output_dir : Path , pretty : Optional [ bool ] = False
243
+ document : MutableMapping [str , Any ], output_dir : Path , pretty : bool = False
249
244
) -> Set [str ]:
250
245
"""Dump the schemadefs to their own file."""
251
246
for entry in document ["types" ]:
@@ -256,7 +251,7 @@ def rewrite_schemadef(
256
251
for field in entry ["fields" ]:
257
252
field ["name" ] = field ["name" ].split ("/" )[2 ]
258
253
rewrite_types (field , entry_file , True )
259
- with open (output_dir / entry_file , "a" , encoding = "utf-8" ) as entry_handle :
254
+ with (output_dir / entry_file ). open ( "a" , encoding = "utf-8" ) as entry_handle :
260
255
yaml_dump (entry , entry_handle , pretty )
261
256
entry ["$import" ] = entry_file
262
257
del entry ["name" ]
@@ -278,15 +273,15 @@ def seen_import(entry: MutableMapping[str, Any]) -> bool:
278
273
return seen_imports
279
274
280
275
281
- def json_dump (entry : Any , output_file : str ) -> None :
276
+ def json_dump (entry : Any , output_file : Path ) -> None :
282
277
"""Output object as JSON."""
283
- with open (output_file , "w" , encoding = "utf-8" ) as result_handle :
278
+ with output_file . open ("w" , encoding = "utf-8" ) as result_handle :
284
279
json .dump (entry , result_handle , indent = 4 )
285
280
286
281
287
282
def yaml_dump (
288
283
entry : Any ,
289
- output_file_or_handle : Optional [ Union [str , Path , TextIOWrapper , TextIO ] ],
284
+ output_file_or_handle : Union [str , Path , TextIOWrapper , TextIO ],
290
285
pretty : bool ,
291
286
) -> None :
292
287
"""Output object as YAML."""
@@ -299,13 +294,15 @@ def yaml_dump(
299
294
with open (output_file_or_handle , "w" , encoding = "utf-8" ) as result_handle :
300
295
if pretty :
301
296
result_handle .write (stringify_dict (entry ))
302
- else :
303
- yaml .dump (entry , result_handle )
297
+ return
298
+ yaml .dump (entry , result_handle )
299
+ return
304
300
elif isinstance (output_file_or_handle , (TextIOWrapper , TextIO )):
305
301
if pretty :
306
302
output_file_or_handle .write (stringify_dict (entry ))
307
- else :
308
- yaml .dump (entry , output_file_or_handle )
303
+ return
304
+ yaml .dump (entry , output_file_or_handle )
305
+ return
309
306
else :
310
307
raise ValueError (
311
308
f"output_file_or_handle must be a string or a file handle but got { type (output_file_or_handle )} "
0 commit comments