7
7
8
8
import os
9
9
import sys
10
+ import json
11
+ import argparse
10
12
from pathlib import Path
11
13
from typing import IO , Any , Dict , List , MutableMapping , Set , Text , Union , cast
12
14
13
15
from ruamel import yaml
14
16
from schema_salad .sourceline import SourceLine , add_lc_filename
17
+ from cwlformat .formatter import stringify_dict
15
18
16
19
17
- def main (args : List [ str ] ) -> None :
20
+ def main () -> None :
18
21
"""Split the packed CWL at the path of the first argument."""
19
- with open (args [0 ], "r" ) as source_handle :
20
- run (source_handle )
21
-
22
-
23
- def run (sourceIO : IO [str ]) -> None :
22
+ parser = argparse .ArgumentParser (description = "Split the packed CWL." )
23
+ parser .add_argument ("cwlfile" )
24
+ parser .add_argument (
25
+ "-m" ,
26
+ "--mainfile" ,
27
+ default = None ,
28
+ type = str ,
29
+ help = "Specify the name of the main document." ,
30
+ )
31
+ parser .add_argument (
32
+ "-f" ,
33
+ "--output-format" ,
34
+ choices = ["json" , "yaml" ],
35
+ type = str ,
36
+ default = "json" ,
37
+ help = "Specify the format of the output CWL files." ,
38
+ )
39
+ parser .add_argument (
40
+ "-p" ,
41
+ "--pretty" ,
42
+ action = "store_true" ,
43
+ default = False ,
44
+ help = "Beautify the output CWL document, only works with yaml format." ,
45
+ )
46
+ parser .add_argument (
47
+ "-C" ,
48
+ "--outdir" ,
49
+ type = str ,
50
+ default = os .getcwd (),
51
+ help = "Output folder for the unpacked CWL files." ,
52
+ )
53
+ options = parser .parse_args ()
54
+
55
+ with open (options .cwlfile , "r" ) as source_handle :
56
+ run (
57
+ source_handle ,
58
+ options .outdir ,
59
+ options .output_format ,
60
+ options .mainfile ,
61
+ options .pretty ,
62
+ )
63
+
64
+
65
+ def run (
66
+ sourceIO : IO [str ], output_dir : str , output_format : str , mainfile : str , pretty : bool
67
+ ) -> None :
24
68
"""Loop over the provided packed CWL document and split it up."""
25
69
source = yaml .main .round_trip_load (sourceIO , preserve_quotes = True )
26
70
add_lc_filename (source , sourceIO .name )
@@ -47,15 +91,16 @@ def my_represent_none(
47
91
for import_name in imports :
48
92
rewrite_types (entry , "#{}" .format (import_name ), False )
49
93
if entry_id == "main" :
50
- entry_id = "unpacked_{}" .format (os .path .basename (sourceIO .name ))
51
- with open (entry_id , "w" , encoding = "utf-8" ) as result_handle :
52
- yaml .main .round_trip_dump (
53
- entry ,
54
- result_handle ,
55
- default_flow_style = False ,
56
- indent = 4 ,
57
- block_seq_indent = 2 ,
58
- )
94
+ if mainfile is None :
95
+ entry_id = "unpacked_{}" .format (os .path .basename (sourceIO .name ))
96
+ else :
97
+ entry_id = mainfile
98
+
99
+ output_file = os .path .join (output_dir , entry_id )
100
+ if output_format == "json" :
101
+ json_dump (entry , output_file )
102
+ elif output_format == "yaml" :
103
+ yaml_dump (entry , output_file , pretty )
59
104
60
105
61
106
def rewrite (document : Any , doc_id : str ) -> Set [str ]:
@@ -177,5 +222,26 @@ def seen_import(entry: MutableMapping[str, Any]) -> bool:
177
222
return seen_imports
178
223
179
224
225
+ def json_dump (entry : Any , output_file : str ) -> None :
226
+ """Output object as JSON."""
227
+ with open (output_file , "w" , encoding = "utf-8" ) as result_handle :
228
+ json .dump (entry , result_handle , indent = 4 )
229
+
230
+
231
+ def yaml_dump (entry : Any , output_file : str , pretty : bool ) -> None :
232
+ """Output object as YAML."""
233
+ with open (output_file , "w" , encoding = "utf-8" ) as result_handle :
234
+ if pretty :
235
+ result_handle .write (stringify_dict (entry ))
236
+ else :
237
+ yaml .main .round_trip_dump (
238
+ entry ,
239
+ result_handle ,
240
+ default_flow_style = False ,
241
+ indent = 4 ,
242
+ block_seq_indent = 2 ,
243
+ )
244
+
245
+
180
246
if __name__ == "__main__" :
181
- main (sys . argv [ 1 :] )
247
+ main ()
0 commit comments