Skip to content

Commit 3f4684d

Browse files
GlassOfWhiskeymr-c
authored andcommitted
Fixed type_for_source logic
1 parent 52d9669 commit 3f4684d

13 files changed

+789
-234
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from schema_salad.utils import json_dumps
2424

2525
import cwl_utils.parser.cwl_v1_0 as cwl
26+
import cwl_utils.parser.cwl_v1_0_utils as utils
2627
from cwl_utils.errors import JavascriptException, WorkflowException
2728
from cwl_utils.expression import do_eval, interpolate
2829
from cwl_utils.types import CWLObjectType, CWLOutputType
@@ -537,7 +538,7 @@ def empty_inputs(
537538
else:
538539
try:
539540
result[param_id] = example_input(
540-
type_for_source(process_or_step.run, param.source, parent)
541+
utils.type_for_source(process_or_step.run, param.source, parent)
541542
)
542543
except WorkflowException:
543544
pass
@@ -582,71 +583,6 @@ def example_input(some_type: Any) -> Any:
582583
return None
583584

584585

585-
def type_for_source(
586-
process: Union[cwl.CommandLineTool, cwl.Workflow, cwl.ExpressionTool],
587-
sourcenames: Union[str, List[str]],
588-
parent: Optional[cwl.Workflow] = None,
589-
) -> Union[List[Any], Any]:
590-
"""Determine the type for the given sourcenames."""
591-
params = param_for_source_id(process, sourcenames, parent)
592-
if not isinstance(params, list):
593-
return params.type
594-
new_type: List[Any] = []
595-
for p in params:
596-
if isinstance(p, str) and p not in new_type:
597-
new_type.append(p)
598-
elif hasattr(p, "type") and p.type not in new_type:
599-
new_type.append(p.type)
600-
return new_type
601-
602-
603-
def param_for_source_id(
604-
process: Union[cwl.CommandLineTool, cwl.Workflow, cwl.ExpressionTool],
605-
sourcenames: Union[str, List[str]],
606-
parent: Optional[cwl.Workflow] = None,
607-
) -> Union[List[cwl.InputParameter], cwl.InputParameter]:
608-
"""Find the process input parameter that matches one of the given sourcenames."""
609-
if isinstance(sourcenames, str):
610-
sourcenames = [sourcenames]
611-
params: List[cwl.InputParameter] = []
612-
for sourcename in sourcenames:
613-
if not isinstance(process, cwl.Workflow):
614-
for param in process.inputs:
615-
if param.id.split("#")[-1] == sourcename.split("#")[-1]:
616-
params.append(param)
617-
targets = [process]
618-
if parent:
619-
targets.append(parent)
620-
for target in targets:
621-
if isinstance(target, cwl.Workflow):
622-
for inp in target.inputs:
623-
if inp.id.split("#")[-1] == sourcename.split("#")[-1]:
624-
params.append(inp)
625-
for step in target.steps:
626-
if sourcename.split("/")[0] == step.id.split("#")[-1] and step.out:
627-
for outp in step.out:
628-
outp_id = outp if isinstance(outp, str) else outp.id
629-
if outp_id.split("/")[-1] == sourcename.split("/", 1)[1]:
630-
if step.run and step.run.outputs:
631-
for output in step.run.outputs:
632-
if (
633-
output.id.split("#")[-1]
634-
== sourcename.split("/", 1)[1]
635-
):
636-
params.append(output)
637-
if len(params) == 1:
638-
return params[0]
639-
elif len(params) > 1:
640-
return params
641-
raise WorkflowException(
642-
"param {} not found in {}\n or\n {}.".format(
643-
sourcename,
644-
yaml.main.round_trip_dump(cwl.save(process)),
645-
yaml.main.round_trip_dump(cwl.save(parent)),
646-
)
647-
)
648-
649-
650586
EMPTY_FILE: CWLOutputType = {
651587
"class": "File",
652588
"basename": "em.pty",
@@ -1841,11 +1777,13 @@ def traverse_step(
18411777
if not step.scatter:
18421778
self.append(
18431779
example_input(
1844-
type_for_source(parent, source.split("#")[-1])
1780+
utils.type_for_source(parent, source.split("#")[-1])
18451781
)
18461782
)
18471783
else:
1848-
scattered_source_type = type_for_source(parent, source)
1784+
scattered_source_type = utils.type_for_source(
1785+
parent, source
1786+
)
18491787
if isinstance(scattered_source_type, list):
18501788
for stype in scattered_source_type:
18511789
self.append(example_input(stype.type))
@@ -1854,10 +1792,12 @@ def traverse_step(
18541792
else:
18551793
if not step.scatter:
18561794
self = example_input(
1857-
type_for_source(parent, inp.source.split("#")[-1])
1795+
utils.type_for_source(parent, inp.source.split("#")[-1])
18581796
)
18591797
else:
1860-
scattered_source_type2 = type_for_source(parent, inp.source)
1798+
scattered_source_type2 = utils.type_for_source(
1799+
parent, inp.source
1800+
)
18611801
if isinstance(scattered_source_type2, list):
18621802
self = example_input(scattered_source_type2[0].type)
18631803
else:
@@ -1880,7 +1820,9 @@ def traverse_step(
18801820
for source in inp.source:
18811821
source_id = source.split("#")[-1]
18821822
input_source_id.append(source_id)
1883-
temp_type = type_for_source(step.run, source_id, parent)
1823+
temp_type = utils.type_for_source(
1824+
step.run, source_id, parent
1825+
)
18841826
if isinstance(temp_type, list):
18851827
for ttype in temp_type:
18861828
if ttype not in source_types:
@@ -1894,7 +1836,7 @@ def traverse_step(
18941836
)
18951837
else:
18961838
input_source_id = inp.source.split("#")[-1]
1897-
source_type = param_for_source_id(
1839+
source_type = utils.param_for_source_id(
18981840
step.run, input_source_id, parent
18991841
)
19001842
# target.id = target.id.split('#')[-1]
@@ -1965,7 +1907,9 @@ def workflow_step_to_InputParameters(
19651907
continue
19661908
inp_id = inp.id.split("#")[-1].split("/")[-1]
19671909
if inp.source and inp_id != except_in_id:
1968-
param = copy.deepcopy(param_for_source_id(parent, sourcenames=inp.source))
1910+
param = copy.deepcopy(
1911+
utils.param_for_source_id(parent, sourcenames=inp.source)
1912+
)
19691913
if isinstance(param, list):
19701914
for p in param:
19711915
if not p.type:

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from schema_salad.utils import json_dumps
2424

2525
import cwl_utils.parser.cwl_v1_1 as cwl
26+
import cwl_utils.parser.cwl_v1_1_utils as utils
2627
from cwl_utils.errors import JavascriptException, WorkflowException
2728
from cwl_utils.expression import do_eval, interpolate
2829
from cwl_utils.types import CWLObjectType, CWLOutputType
@@ -535,7 +536,7 @@ def empty_inputs(
535536
else:
536537
try:
537538
result[param_id] = example_input(
538-
type_for_source(process_or_step.run, param.source, parent)
539+
utils.type_for_source(process_or_step.run, param.source, parent)
539540
)
540541
except WorkflowException:
541542
pass
@@ -580,71 +581,6 @@ def example_input(some_type: Any) -> Any:
580581
return None
581582

582583

583-
def type_for_source(
584-
process: Union[cwl.CommandLineTool, cwl.Workflow, cwl.ExpressionTool],
585-
sourcenames: Union[str, List[str]],
586-
parent: Optional[cwl.Workflow] = None,
587-
) -> Union[List[Any], Any]:
588-
"""Determine the type for the given sourcenames."""
589-
params = param_for_source_id(process, sourcenames, parent)
590-
if not isinstance(params, list):
591-
return params.type
592-
new_type: List[Any] = []
593-
for p in params:
594-
if isinstance(p, str) and p not in new_type:
595-
new_type.append(p)
596-
elif hasattr(p, "type") and p.type not in new_type:
597-
new_type.append(p.type)
598-
return new_type
599-
600-
601-
def param_for_source_id(
602-
process: Union[cwl.CommandLineTool, cwl.Workflow, cwl.ExpressionTool],
603-
sourcenames: Union[str, List[str]],
604-
parent: Optional[cwl.Workflow] = None,
605-
) -> Union[List[cwl.WorkflowInputParameter], cwl.WorkflowInputParameter]:
606-
"""Find the process input parameter that matches one of the given sourcenames."""
607-
if isinstance(sourcenames, str):
608-
sourcenames = [sourcenames]
609-
params: List[cwl.WorkflowInputParameter] = []
610-
for sourcename in sourcenames:
611-
if not isinstance(process, cwl.Workflow):
612-
for param in process.inputs:
613-
if param.id.split("#")[-1] == sourcename.split("#")[-1]:
614-
params.append(param)
615-
targets = [process]
616-
if parent:
617-
targets.append(parent)
618-
for target in targets:
619-
if isinstance(target, cwl.Workflow):
620-
for inp in target.inputs:
621-
if inp.id.split("#")[-1] == sourcename.split("#")[-1]:
622-
params.append(inp)
623-
for step in target.steps:
624-
if sourcename.split("/")[0] == step.id.split("#")[-1] and step.out:
625-
for outp in step.out:
626-
outp_id = outp if isinstance(outp, str) else outp.id
627-
if outp_id.split("/")[-1] == sourcename.split("/", 1)[1]:
628-
if step.run and step.run.outputs:
629-
for output in step.run.outputs:
630-
if (
631-
output.id.split("#")[-1]
632-
== sourcename.split("/", 1)[1]
633-
):
634-
params.append(output)
635-
if len(params) == 1:
636-
return params[0]
637-
elif len(params) > 1:
638-
return params
639-
raise WorkflowException(
640-
"param {} not found in {}\n or\n {}.".format(
641-
sourcename,
642-
yaml.main.round_trip_dump(cwl.save(process)),
643-
yaml.main.round_trip_dump(cwl.save(parent)),
644-
)
645-
)
646-
647-
648584
EMPTY_FILE: CWLOutputType = {
649585
"class": "File",
650586
"basename": "em.pty",
@@ -1841,11 +1777,13 @@ def traverse_step(
18411777
if not step.scatter:
18421778
self.append(
18431779
example_input(
1844-
type_for_source(parent, source.split("#")[-1])
1780+
utils.type_for_source(parent, source.split("#")[-1])
18451781
)
18461782
)
18471783
else:
1848-
scattered_source_type = type_for_source(parent, source)
1784+
scattered_source_type = utils.type_for_source(
1785+
parent, source
1786+
)
18491787
if isinstance(scattered_source_type, list):
18501788
for stype in scattered_source_type:
18511789
self.append(example_input(stype.type))
@@ -1854,10 +1792,12 @@ def traverse_step(
18541792
else:
18551793
if not step.scatter:
18561794
self = example_input(
1857-
type_for_source(parent, inp.source.split("#")[-1])
1795+
utils.type_for_source(parent, inp.source.split("#")[-1])
18581796
)
18591797
else:
1860-
scattered_source_type2 = type_for_source(parent, inp.source)
1798+
scattered_source_type2 = utils.type_for_source(
1799+
parent, inp.source
1800+
)
18611801
if isinstance(scattered_source_type2, list):
18621802
self = example_input(scattered_source_type2[0].type)
18631803
else:
@@ -1880,7 +1820,9 @@ def traverse_step(
18801820
for source in inp.source:
18811821
source_id = source.split("#")[-1]
18821822
input_source_id.append(source_id)
1883-
temp_type = type_for_source(step.run, source_id, parent)
1823+
temp_type = utils.type_for_source(
1824+
step.run, source_id, parent
1825+
)
18841826
if isinstance(temp_type, list):
18851827
for ttype in temp_type:
18861828
if ttype not in source_types:
@@ -1894,7 +1836,7 @@ def traverse_step(
18941836
)
18951837
else:
18961838
input_source_id = inp.source.split("#")[-1]
1897-
source_type = param_for_source_id(
1839+
source_type = utils.param_for_source_id(
18981840
step.run, input_source_id, parent
18991841
)
19001842
# target.id = target.id.split('#')[-1]
@@ -1965,7 +1907,9 @@ def workflow_step_to_WorkflowInputParameters(
19651907
continue
19661908
inp_id = inp.id.split("#")[-1].split("/")[-1]
19671909
if inp.source and inp_id != except_in_id:
1968-
param = copy.deepcopy(param_for_source_id(parent, sourcenames=inp.source))
1910+
param = copy.deepcopy(
1911+
utils.param_for_source_id(parent, sourcenames=inp.source)
1912+
)
19691913
if isinstance(param, list):
19701914
for p in param:
19711915
p.id = inp_id

0 commit comments

Comments
 (0)