3939from schema_salad .exceptions import ValidationException
4040from schema_salad .ref_resolver import Loader , file_uri , uri_file_path
4141from 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+ )
4349
4450import ruamel .yaml
4551from ruamel .yaml .comments import CommentedMap , CommentedSeq
@@ -415,7 +421,7 @@ def init_job_order(
415421 args : argparse .Namespace ,
416422 process : Process ,
417423 loader : Loader ,
418- stdout : Union [ TextIO , StreamWriter ],
424+ stdout : IO [ str ],
419425 print_input_deps : bool = False ,
420426 relative_deps : str = "primary" ,
421427 make_fs_access : Callable [[str ], StdFsAccess ] = StdFsAccess ,
@@ -438,7 +444,7 @@ def init_job_order(
438444 file_uri (os .getcwd ()) + "/" ,
439445 )
440446 if args .tool_help :
441- toolparser .print_help (cast ( IO [ str ], stdout ) )
447+ toolparser .print_help (stdout )
442448 exit (0 )
443449 cmd_line = vars (toolparser .parse_args (args .job_order ))
444450 for record_name in records :
@@ -564,7 +570,7 @@ def make_relative(base: str, obj: CWLObjectType) -> None:
564570def printdeps (
565571 obj : CWLObjectType ,
566572 document_loader : Loader ,
567- stdout : Union [ TextIO , StreamWriter ],
573+ stdout : IO [ str ],
568574 relative_deps : str ,
569575 uri : str ,
570576 basedir : Optional [str ] = None ,
@@ -577,7 +583,7 @@ def printdeps(
577583 elif relative_deps == "cwd" :
578584 base = os .getcwd ()
579585 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 )
581587
582588
583589def prov_deps (
@@ -641,10 +647,10 @@ def print_pack(
641647 """Return a CWL serialization of the CWL document in JSON."""
642648 packed = pack (loadingContext , uri )
643649 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 )
648654
649655
650656def supported_cwl_versions (enable_dev : bool ) -> List [str ]:
@@ -763,9 +769,7 @@ def setup_loadingContext(
763769 return loadingContext
764770
765771
766- def make_template (
767- tool : Process ,
768- ) -> None :
772+ def make_template (tool : Process , target : IO [str ]) -> None :
769773 """Make a template CWL input object for the give Process."""
770774
771775 def my_represent_none (
@@ -783,7 +787,7 @@ def my_represent_none(
783787 yaml .block_seq_indent = 2
784788 yaml .dump (
785789 generate_input_template (tool ),
786- sys . stdout ,
790+ target ,
787791 )
788792
789793
@@ -945,7 +949,7 @@ def check_working_directories(
945949
946950def print_targets (
947951 tool : Process ,
948- stdout : Union [ TextIO , StreamWriter ],
952+ stdout : IO [ str ],
949953 loading_context : LoadingContext ,
950954 prefix : str = "" ,
951955) -> None :
@@ -982,7 +986,7 @@ def main(
982986 args : Optional [argparse .Namespace ] = None ,
983987 job_order_object : Optional [CWLObjectType ] = None ,
984988 stdin : IO [Any ] = sys .stdin ,
985- stdout : Optional [Union [ TextIO , StreamWriter ]] = None ,
989+ stdout : Optional [IO [ str ]] = None ,
986990 stderr : IO [Any ] = sys .stderr ,
987991 versionfunc : Callable [[], str ] = versionstring ,
988992 logger_handler : Optional [logging .Handler ] = None ,
@@ -992,17 +996,18 @@ def main(
992996 runtimeContext : Optional [RuntimeContext ] = None ,
993997 input_required : bool = True ,
994998) -> 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
9961000 if hasattr (sys .stdout , "encoding" ) and sys .stdout .encoding .upper () not in (
9971001 "UTF-8" ,
9981002 "UTF8" ,
9991003 ):
10001004 if hasattr (sys .stdout , "detach" ):
10011005 stdout = io .TextIOWrapper (sys .stdout .buffer , encoding = "utf-8" )
10021006 else :
1003- stdout = getwriter ("utf-8" )(sys .stdout ) # type: ignore
1007+ stdout = getwriter ("utf-8" )(sys .stdout ) # type: ignore[assignment,arg-type]
10041008 else :
10051009 stdout = sys .stdout
1010+ stdout = cast (IO [str ], stdout )
10061011
10071012 _logger .removeHandler (defaultStreamHandler )
10081013 stderr_handler = logger_handler
@@ -1151,15 +1156,13 @@ def main(
11511156 )
11521157
11531158 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 ,
11631166 )
11641167 return 0
11651168
@@ -1179,7 +1182,7 @@ def main(
11791182 raise main_missing_exc
11801183
11811184 if args .make_template :
1182- make_template (tool )
1185+ make_template (tool , stdout )
11831186 return 0
11841187
11851188 if args .validate :
@@ -1225,15 +1228,13 @@ def main(
12251228 if args .print_subgraph :
12261229 if "name" in tool .tool :
12271230 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 ,
12371238 )
12381239 return 0
12391240
@@ -1398,12 +1399,15 @@ def loc_to_path(obj: CWLObjectType) -> None:
13981399 # Unsetting the Generation from final output object
13991400 visit_class (out , ("File" ,), MutationManager ().unset_generation )
14001401
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 ()
14071411
14081412 if status != "success" :
14091413 _logger .warning ("Final process status is %s" , status )
0 commit comments