39
39
from schema_salad .exceptions import ValidationException
40
40
from schema_salad .ref_resolver import Loader , file_uri , uri_file_path
41
41
from schema_salad .sourceline import cmap , strip_dup_lineno
42
- from schema_salad .utils import ContextType , FetcherCallableType , json_dumps , yaml_no_ts
42
+ from schema_salad .utils import (
43
+ ContextType ,
44
+ FetcherCallableType ,
45
+ json_dump ,
46
+ json_dumps ,
47
+ yaml_no_ts ,
48
+ )
43
49
44
50
import ruamel .yaml
45
51
from ruamel .yaml .comments import CommentedMap , CommentedSeq
@@ -415,7 +421,7 @@ def init_job_order(
415
421
args : argparse .Namespace ,
416
422
process : Process ,
417
423
loader : Loader ,
418
- stdout : Union [ TextIO , StreamWriter ],
424
+ stdout : IO [ str ],
419
425
print_input_deps : bool = False ,
420
426
relative_deps : str = "primary" ,
421
427
make_fs_access : Callable [[str ], StdFsAccess ] = StdFsAccess ,
@@ -438,7 +444,7 @@ def init_job_order(
438
444
file_uri (os .getcwd ()) + "/" ,
439
445
)
440
446
if args .tool_help :
441
- toolparser .print_help (cast ( IO [ str ], stdout ) )
447
+ toolparser .print_help (stdout )
442
448
exit (0 )
443
449
cmd_line = vars (toolparser .parse_args (args .job_order ))
444
450
for record_name in records :
@@ -564,7 +570,7 @@ def make_relative(base: str, obj: CWLObjectType) -> None:
564
570
def printdeps (
565
571
obj : CWLObjectType ,
566
572
document_loader : Loader ,
567
- stdout : Union [ TextIO , StreamWriter ],
573
+ stdout : IO [ str ],
568
574
relative_deps : str ,
569
575
uri : str ,
570
576
basedir : Optional [str ] = None ,
@@ -577,7 +583,7 @@ def printdeps(
577
583
elif relative_deps == "cwd" :
578
584
base = os .getcwd ()
579
585
visit_class (deps , ("File" , "Directory" ), functools .partial (make_relative , base ))
580
- print ( json_dumps ( deps , indent = 4 , default = str ), file = stdout )
586
+ json_dump ( deps , stdout , indent = 4 , default = str )
581
587
582
588
583
589
def prov_deps (
@@ -641,10 +647,10 @@ def print_pack(
641
647
"""Return a CWL serialization of the CWL document in JSON."""
642
648
packed = pack (loadingContext , uri )
643
649
if len (cast (Sized , packed ["$graph" ])) > 1 :
644
- return json_dumps ( packed , indent = 4 , default = str )
645
- return json_dumps (
646
- cast (MutableSequence [CWLObjectType ], packed ["$graph" ])[0 ], indent = 4 , default = str
647
- )
650
+ target = packed
651
+ else :
652
+ target = cast (MutableSequence [CWLObjectType ], packed ["$graph" ])[0 ]
653
+ return json_dumps ( target , indent = 4 , default = str )
648
654
649
655
650
656
def supported_cwl_versions (enable_dev : bool ) -> List [str ]:
@@ -763,9 +769,7 @@ def setup_loadingContext(
763
769
return loadingContext
764
770
765
771
766
- def make_template (
767
- tool : Process ,
768
- ) -> None :
772
+ def make_template (tool : Process , target : IO [str ]) -> None :
769
773
"""Make a template CWL input object for the give Process."""
770
774
771
775
def my_represent_none (
@@ -783,7 +787,7 @@ def my_represent_none(
783
787
yaml .block_seq_indent = 2
784
788
yaml .dump (
785
789
generate_input_template (tool ),
786
- sys . stdout ,
790
+ target ,
787
791
)
788
792
789
793
@@ -945,7 +949,7 @@ def check_working_directories(
945
949
946
950
def print_targets (
947
951
tool : Process ,
948
- stdout : Union [ TextIO , StreamWriter ],
952
+ stdout : IO [ str ],
949
953
loading_context : LoadingContext ,
950
954
prefix : str = "" ,
951
955
) -> None :
@@ -982,7 +986,7 @@ def main(
982
986
args : Optional [argparse .Namespace ] = None ,
983
987
job_order_object : Optional [CWLObjectType ] = None ,
984
988
stdin : IO [Any ] = sys .stdin ,
985
- stdout : Optional [Union [ TextIO , StreamWriter ]] = None ,
989
+ stdout : Optional [IO [ str ]] = None ,
986
990
stderr : IO [Any ] = sys .stderr ,
987
991
versionfunc : Callable [[], str ] = versionstring ,
988
992
logger_handler : Optional [logging .Handler ] = None ,
@@ -992,17 +996,18 @@ def main(
992
996
runtimeContext : Optional [RuntimeContext ] = None ,
993
997
input_required : bool = True ,
994
998
) -> int :
995
- if not stdout : # force UTF-8 even if the console is configured differently
999
+ if stdout is None : # force UTF-8 even if the console is configured differently
996
1000
if hasattr (sys .stdout , "encoding" ) and sys .stdout .encoding .upper () not in (
997
1001
"UTF-8" ,
998
1002
"UTF8" ,
999
1003
):
1000
1004
if hasattr (sys .stdout , "detach" ):
1001
1005
stdout = io .TextIOWrapper (sys .stdout .buffer , encoding = "utf-8" )
1002
1006
else :
1003
- stdout = getwriter ("utf-8" )(sys .stdout ) # type: ignore
1007
+ stdout = getwriter ("utf-8" )(sys .stdout ) # type: ignore[assignment,arg-type]
1004
1008
else :
1005
1009
stdout = sys .stdout
1010
+ stdout = cast (IO [str ], stdout )
1006
1011
1007
1012
_logger .removeHandler (defaultStreamHandler )
1008
1013
stderr_handler = logger_handler
@@ -1151,15 +1156,13 @@ def main(
1151
1156
)
1152
1157
1153
1158
if args .print_pre :
1154
- print (
1155
- json_dumps (
1156
- processobj ,
1157
- indent = 4 ,
1158
- sort_keys = True ,
1159
- separators = ("," , ": " ),
1160
- default = str ,
1161
- ),
1162
- file = stdout ,
1159
+ json_dump (
1160
+ processobj ,
1161
+ stdout ,
1162
+ indent = 4 ,
1163
+ sort_keys = True ,
1164
+ separators = ("," , ": " ),
1165
+ default = str ,
1163
1166
)
1164
1167
return 0
1165
1168
@@ -1179,7 +1182,7 @@ def main(
1179
1182
raise main_missing_exc
1180
1183
1181
1184
if args .make_template :
1182
- make_template (tool )
1185
+ make_template (tool , stdout )
1183
1186
return 0
1184
1187
1185
1188
if args .validate :
@@ -1225,15 +1228,13 @@ def main(
1225
1228
if args .print_subgraph :
1226
1229
if "name" in tool .tool :
1227
1230
del tool .tool ["name" ]
1228
- print (
1229
- json_dumps (
1230
- tool .tool ,
1231
- indent = 4 ,
1232
- sort_keys = True ,
1233
- separators = ("," , ": " ),
1234
- default = str ,
1235
- ),
1236
- file = stdout ,
1231
+ json_dump (
1232
+ tool .tool ,
1233
+ stdout ,
1234
+ indent = 4 ,
1235
+ sort_keys = True ,
1236
+ separators = ("," , ": " ),
1237
+ default = str ,
1237
1238
)
1238
1239
return 0
1239
1240
@@ -1398,12 +1399,15 @@ def loc_to_path(obj: CWLObjectType) -> None:
1398
1399
# Unsetting the Generation from final output object
1399
1400
visit_class (out , ("File" ,), MutationManager ().unset_generation )
1400
1401
1401
- print (
1402
- json_dumps (out , indent = 4 , ensure_ascii = False , default = str ),
1403
- file = stdout ,
1404
- )
1405
- if hasattr (stdout , "flush" ):
1406
- stdout .flush ()
1402
+ if args .write_summary :
1403
+ with open (args .write_summary , "w" ) as output_file :
1404
+ json_dump (
1405
+ out , output_file , indent = 4 , ensure_ascii = False , default = str
1406
+ )
1407
+ else :
1408
+ json_dump (out , stdout , indent = 4 , ensure_ascii = False , default = str )
1409
+ if hasattr (stdout , "flush" ):
1410
+ stdout .flush ()
1407
1411
1408
1412
if status != "success" :
1409
1413
_logger .warning ("Final process status is %s" , status )
0 commit comments