4
4
"""CWL Expression refactoring tool for CWL v1.0 ."""
5
5
import copy
6
6
import hashlib
7
- from collections .abc import Mapping
8
- from typing import (
9
- Any ,
10
- Dict ,
11
- List ,
12
- MutableSequence ,
13
- Optional ,
14
- Sequence ,
15
- Tuple ,
16
- Type ,
17
- Union ,
18
- cast ,
19
- )
7
+ from collections .abc import Mapping , MutableSequence , Sequence
8
+ from typing import Any , Optional , Union , cast
20
9
21
10
from ruamel import yaml
22
11
from schema_salad .sourceline import SourceLine
@@ -142,7 +131,7 @@ def get_expression(
142
131
143
132
144
133
def etool_to_cltool (
145
- etool : cwl .ExpressionTool , expressionLib : Optional [List [str ]] = None
134
+ etool : cwl .ExpressionTool , expressionLib : Optional [list [str ]] = None
146
135
) -> cwl .CommandLineTool :
147
136
"""Convert a ExpressionTool to a CommandLineTool."""
148
137
inputs = yaml .comments .CommentedSeq () # preserve the order
@@ -216,7 +205,7 @@ def traverse(
216
205
inside : bool ,
217
206
skip_command_line1 : bool ,
218
207
skip_command_line2 : bool ,
219
- ) -> Tuple [Union [cwl .CommandLineTool , cwl .ExpressionTool , cwl .Workflow ], bool ]:
208
+ ) -> tuple [Union [cwl .CommandLineTool , cwl .ExpressionTool , cwl .Workflow ], bool ]:
220
209
"""Convert the given process and any subprocesses."""
221
210
if not inside and isinstance (process , cwl .CommandLineTool ):
222
211
process = expand_stream_shortcuts (process )
@@ -330,7 +319,7 @@ def generate_etool_from_expr(
330
319
Union [
331
320
cwl .InputParameter ,
332
321
cwl .CommandInputParameter ,
333
- List [Union [cwl .InputParameter , cwl .CommandInputParameter ]],
322
+ list [Union [cwl .InputParameter , cwl .CommandInputParameter ]],
334
323
]
335
324
] = None , # if the "self" input should be a different type than the "result" output
336
325
extra_processes : Optional [
@@ -344,7 +333,7 @@ def generate_etool_from_expr(
344
333
self_type = target
345
334
if isinstance (self_type , list ):
346
335
new_type : Union [
347
- List [Union [cwl .ArraySchema , cwl .InputRecordSchema ]],
336
+ list [Union [cwl .ArraySchema , cwl .InputRecordSchema ]],
348
337
Union [cwl .ArraySchema , cwl .InputRecordSchema ],
349
338
] = [clean_type_ids (t .type_ ) for t in self_type if t .type_ ]
350
339
elif self_type .type_ :
@@ -420,7 +409,7 @@ def get_input_for_id(
420
409
"""Determine the CommandInputParameter for the given input name."""
421
410
name = name .split ("/" )[- 1 ]
422
411
423
- for inp in cast (List [cwl .CommandInputParameter ], tool .inputs ):
412
+ for inp in cast (list [cwl .CommandInputParameter ], tool .inputs ):
424
413
if inp .id and inp .id .split ("#" )[- 1 ].split ("/" )[- 1 ] == name :
425
414
return inp
426
415
if isinstance (tool , cwl .Workflow ) and "/" in name :
@@ -437,7 +426,7 @@ def find_expressionLib(
437
426
processes : Sequence [
438
427
Union [cwl .CommandLineTool , cwl .Workflow , cwl .ExpressionTool , cwl .WorkflowStep ]
439
428
]
440
- ) -> Optional [List [str ]]:
429
+ ) -> Optional [list [str ]]:
441
430
"""
442
431
Return the expressionLib from the highest priority InlineJavascriptRequirement.
443
432
@@ -448,7 +437,7 @@ def find_expressionLib(
448
437
if process .requirements :
449
438
for req in process .requirements :
450
439
if isinstance (req , cwl .InlineJavascriptRequirement ):
451
- return cast (Optional [List [str ]], copy .deepcopy (req .expressionLib ))
440
+ return cast (Optional [list [str ]], copy .deepcopy (req .expressionLib ))
452
441
return None
453
442
454
443
@@ -457,15 +446,15 @@ def replace_expr_with_etool(
457
446
name : str ,
458
447
workflow : cwl .Workflow ,
459
448
target : Union [cwl .CommandInputParameter , cwl .InputParameter ],
460
- source : Optional [Union [str , List [Any ]]],
449
+ source : Optional [Union [str , list [Any ]]],
461
450
replace_etool : bool = False ,
462
451
extra_process : Optional [
463
452
Union [cwl .Workflow , cwl .WorkflowStep , cwl .CommandLineTool ]
464
453
] = None ,
465
454
source_type : Optional [cwl .CommandInputParameter ] = None ,
466
455
) -> None :
467
456
"""Modify the given workflow, replacing the expr with an standalone ExpressionTool."""
468
- extra_processes : List [
457
+ extra_processes : list [
469
458
Union [cwl .Workflow , cwl .WorkflowStep , cwl .CommandLineTool ]
470
459
] = [workflow ]
471
460
if extra_process :
@@ -474,7 +463,7 @@ def replace_expr_with_etool(
474
463
expr , target , source is None , source_type , extra_processes
475
464
)
476
465
if replace_etool :
477
- processes : List [Union [cwl .WorkflowStep , cwl .Workflow , cwl .CommandLineTool ]] = [
466
+ processes : list [Union [cwl .WorkflowStep , cwl .Workflow , cwl .CommandLineTool ]] = [
478
467
workflow
479
468
]
480
469
if extra_process :
@@ -528,7 +517,7 @@ def empty_inputs(
528
517
cwl .CommandLineTool , cwl .WorkflowStep , cwl .ExpressionTool , cwl .Workflow
529
518
],
530
519
parent : Optional [cwl .Workflow ] = None ,
531
- ) -> Dict [str , Any ]:
520
+ ) -> dict [str , Any ]:
532
521
"""Produce a mock input object for the given inputs."""
533
522
result = {}
534
523
if isinstance (process_or_step , cwl .Process ):
@@ -662,22 +651,22 @@ def process_workflow_reqs_and_hints(
662
651
# and connecting all workflow inputs to the generated step
663
652
modified = False
664
653
inputs = empty_inputs (workflow )
665
- generated_res_reqs : List [ Tuple [str , Union [int , str ]]] = []
666
- generated_iwdr_reqs : List [ Tuple [str , Union [int , str ]]] = []
667
- generated_envVar_reqs : List [ Tuple [str , Union [int , str ]]] = []
668
- prop_reqs : Tuple [
654
+ generated_res_reqs : list [ tuple [str , Union [int , str ]]] = []
655
+ generated_iwdr_reqs : list [ tuple [str , Union [int , str ]]] = []
656
+ generated_envVar_reqs : list [ tuple [str , Union [int , str ]]] = []
657
+ prop_reqs : tuple [
669
658
Union [
670
- Type [cwl .EnvVarRequirement ],
671
- Type [cwl .ResourceRequirement ],
672
- Type [cwl .InitialWorkDirRequirement ],
659
+ type [cwl .EnvVarRequirement ],
660
+ type [cwl .ResourceRequirement ],
661
+ type [cwl .InitialWorkDirRequirement ],
673
662
],
674
663
...,
675
664
] = ()
676
665
resourceReq : Optional [cwl .ResourceRequirement ] = None
677
666
envVarReq : Optional [cwl .EnvVarRequirement ] = None
678
667
iwdr : Optional [cwl .InitialWorkDirRequirement ] = None
679
668
if workflow .requirements is not None :
680
- for req in cast (List [cwl .ProcessRequirement ], workflow .requirements ):
669
+ for req in cast (list [cwl .ProcessRequirement ], workflow .requirements ):
681
670
if req and isinstance (req , cwl .EnvVarRequirement ):
682
671
if req .envDef :
683
672
for index , envDef in enumerate (req .envDef ):
@@ -994,9 +983,9 @@ def process_level_reqs(
994
983
modified = False
995
984
target_process = step .run
996
985
inputs = empty_inputs (process )
997
- generated_res_reqs : List [ Tuple [str , str ]] = []
998
- generated_iwdr_reqs : List [ Tuple [str , Union [int , str ], Any ]] = []
999
- generated_envVar_reqs : List [ Tuple [str , Union [int , str ]]] = []
986
+ generated_res_reqs : list [ tuple [str , str ]] = []
987
+ generated_iwdr_reqs : list [ tuple [str , Union [int , str ], Any ]] = []
988
+ generated_envVar_reqs : list [ tuple [str , Union [int , str ]]] = []
1000
989
if not step .id :
1001
990
return False
1002
991
step_name = step .id .split ("#" , 1 )[- 1 ]
@@ -1378,7 +1367,7 @@ def traverse_CommandLineTool(
1378
1367
}
1379
1368
]
1380
1369
if outp .outputBinding .loadContents :
1381
- cast (Dict [Any , Any ], self )[0 ]["contents" ] = "stuff"
1370
+ cast (dict [Any , Any ], self )[0 ]["contents" ] = "stuff"
1382
1371
expression = get_expression (outp .outputBinding .outputEval , inputs , self )
1383
1372
if expression :
1384
1373
modified = True
@@ -1635,7 +1624,7 @@ def replace_clt_hintreq_expr_with_etool(
1635
1624
1636
1625
def cltool_inputs_to_etool_inputs (
1637
1626
tool : cwl .CommandLineTool ,
1638
- ) -> List [cwl .InputParameter ]:
1627
+ ) -> list [cwl .InputParameter ]:
1639
1628
"""Copy CommandLineTool input objects into the equivalent ExpressionTool input objects."""
1640
1629
inputs = yaml .comments .CommentedSeq ()
1641
1630
if tool .inputs :
@@ -1661,7 +1650,7 @@ def cltool_inputs_to_etool_inputs(
1661
1650
1662
1651
def cltool_step_outputs_to_workflow_outputs (
1663
1652
cltool_step : cwl .WorkflowStep , etool_step_id : str , etool_out_id : str
1664
- ) -> List [cwl .OutputParameter ]:
1653
+ ) -> list [cwl .OutputParameter ]:
1665
1654
"""
1666
1655
Copy CommandLineTool outputs into the equivalent Workflow output parameters.
1667
1656
@@ -1731,7 +1720,7 @@ def generate_etool_from_expr2(
1731
1720
}"""
1732
1721
)
1733
1722
hints = None
1734
- procs : List [
1723
+ procs : list [
1735
1724
Union [cwl .CommandLineTool , cwl .ExpressionTool , cwl .Workflow , cwl .WorkflowStep ]
1736
1725
] = []
1737
1726
if process :
@@ -1820,12 +1809,12 @@ def traverse_step(
1820
1809
raise WorkflowException ("target not found" )
1821
1810
input_source_id = None
1822
1811
source_type : Optional [
1823
- Union [List [cwl .InputParameter ], cwl .InputParameter ]
1812
+ Union [list [cwl .InputParameter ], cwl .InputParameter ]
1824
1813
] = None
1825
1814
if inp .source :
1826
1815
if isinstance (inp .source , MutableSequence ):
1827
1816
input_source_id = []
1828
- source_types : List [cwl .InputParameter ] = []
1817
+ source_types : list [cwl .InputParameter ] = []
1829
1818
for source in inp .source :
1830
1819
source_id = source .split ("#" )[- 1 ]
1831
1820
input_source_id .append (source_id )
@@ -1851,7 +1840,7 @@ def traverse_step(
1851
1840
# target.id = target.id.split('#')[-1]
1852
1841
if isinstance (original_process , cwl .ExpressionTool ):
1853
1842
found_JSReq = False
1854
- reqs : List [cwl .ProcessRequirement ] = []
1843
+ reqs : list [cwl .ProcessRequirement ] = []
1855
1844
if original_process .hints :
1856
1845
reqs .extend (original_process .hints )
1857
1846
if original_process .requirements :
@@ -1907,8 +1896,8 @@ def traverse_step(
1907
1896
1908
1897
1909
1898
def workflow_step_to_InputParameters (
1910
- step_ins : List [cwl .WorkflowStepInput ], parent : cwl .Workflow , except_in_id : str
1911
- ) -> List [cwl .InputParameter ]:
1899
+ step_ins : list [cwl .WorkflowStepInput ], parent : cwl .Workflow , except_in_id : str
1900
+ ) -> list [cwl .InputParameter ]:
1912
1901
"""Create InputParameters to match the given WorkflowStep inputs."""
1913
1902
params = []
1914
1903
for inp in step_ins :
@@ -1947,10 +1936,10 @@ def replace_step_valueFrom_expr_with_etool(
1947
1936
step : cwl .WorkflowStep ,
1948
1937
step_inp : cwl .WorkflowStepInput ,
1949
1938
original_process : Union [cwl .CommandLineTool , cwl .ExpressionTool ],
1950
- original_step_ins : List [cwl .WorkflowStepInput ],
1951
- source : Optional [Union [str , List [str ]]],
1939
+ original_step_ins : list [cwl .WorkflowStepInput ],
1940
+ source : Optional [Union [str , list [str ]]],
1952
1941
replace_etool : bool ,
1953
- source_type : Optional [Union [cwl .InputParameter , List [cwl .InputParameter ]]] = None ,
1942
+ source_type : Optional [Union [cwl .InputParameter , list [cwl .InputParameter ]]] = None ,
1954
1943
) -> None :
1955
1944
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""
1956
1945
if not step_inp .id :
@@ -1972,12 +1961,16 @@ def replace_step_valueFrom_expr_with_etool(
1972
1961
[workflow , step ],
1973
1962
)
1974
1963
if replace_etool :
1975
- processes = [
1964
+ processes : list [
1965
+ Union [
1966
+ cwl .Workflow , cwl .CommandLineTool , cwl .ExpressionTool , cwl .WorkflowStep
1967
+ ]
1968
+ ] = [
1976
1969
workflow ,
1977
1970
step ,
1978
- ] # type: List[Union[cwl.Workflow, cwl.CommandLineTool, cwl.ExpressionTool, cwl.WorkflowStep]]
1971
+ ]
1979
1972
cltool = etool_to_cltool (temp_etool , find_expressionLib (processes ))
1980
- etool = cltool # type : Union[cwl.ExpressionTool, cwl.CommandLineTool]
1973
+ etool : Union [cwl .ExpressionTool , cwl .CommandLineTool ] = cltool
1981
1974
else :
1982
1975
etool = temp_etool
1983
1976
wf_step_inputs = copy .deepcopy (original_step_ins )
@@ -2026,7 +2019,7 @@ def traverse_workflow(
2026
2019
replace_etool : bool ,
2027
2020
skip_command_line1 : bool ,
2028
2021
skip_command_line2 : bool ,
2029
- ) -> Tuple [cwl .Workflow , bool ]:
2022
+ ) -> tuple [cwl .Workflow , bool ]:
2030
2023
"""Traverse a workflow, processing each step."""
2031
2024
modified = False
2032
2025
for index , step in enumerate (workflow .steps ):
0 commit comments