Skip to content

Commit 8241eb5

Browse files
committed
Add abstraction for popping connection dictionary to model.
1 parent 5811bc5 commit 8241eb5

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

gxformat2/converter.py

+7-38
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
convert_dict_to_id_list_if_needed,
1515
ensure_step_position,
1616
inputs_as_native_steps,
17+
pop_connect_from_step_dict,
1718
with_step_ids,
1819
)
1920
from .yaml import ordered_load
@@ -89,13 +90,13 @@ def rename_arg(argument):
8990
return argument
9091

9192

92-
def clean_connection(value):
93+
def clean_connection(value: str) -> str:
9394
if value and "#" in value and SUPPORT_LEGACY_CONNECTIONS:
9495
# Hope these are just used by Galaxy testing workflows and such, and not in production workflows.
9596
log.warn(f"Legacy workflow syntax for connections [{value}] will not be supported in the future")
9697
value = value.replace("#", "/", 1)
97-
else:
98-
return value
98+
99+
return value
99100

100101

101102
class ImportOptions:
@@ -381,7 +382,7 @@ def transform_pause(context, step, default_name="Pause for dataset review"):
381382
"name": name
382383
}
383384

384-
connect = _init_connect_dict(step)
385+
connect = pop_connect_from_step_dict(step)
385386
_populate_input_connections(context, step, connect)
386387
_populate_tool_state(step, tool_state)
387388

@@ -398,7 +399,7 @@ def transform_subworkflow(context, step):
398399
tool_state = {
399400
}
400401

401-
connect = _init_connect_dict(step)
402+
connect = pop_connect_from_step_dict(step)
402403
_populate_input_connections(context, step, connect)
403404
_populate_tool_state(step, tool_state)
404405

@@ -428,7 +429,7 @@ def transform_tool(context, step):
428429
"__page__": 0,
429430
}
430431

431-
connect = _init_connect_dict(step)
432+
connect = pop_connect_from_step_dict(step)
432433

433434
def append_link(key, value):
434435
if key not in connect:
@@ -641,38 +642,6 @@ def _join_prefix(prefix, key):
641642
return new_key
642643

643644

644-
def _init_connect_dict(step):
645-
if "connect" not in step:
646-
step["connect"] = {}
647-
648-
connect = step["connect"]
649-
del step["connect"]
650-
651-
# handle CWL-style in dict connections.
652-
if "in" in step:
653-
step_in = step["in"]
654-
assert isinstance(step_in, dict)
655-
connection_keys = set()
656-
for key, value in step_in.items():
657-
# TODO: this can be a list right?
658-
if isinstance(value, dict) and 'source' in value:
659-
value = value["source"]
660-
elif isinstance(value, dict) and 'default' in value:
661-
continue
662-
elif isinstance(value, dict):
663-
raise KeyError(f'step input must define either source or default {value}')
664-
connect[key] = [value]
665-
connection_keys.add(key)
666-
667-
for key in connection_keys:
668-
del step_in[key]
669-
670-
if len(step_in) == 0:
671-
del step['in']
672-
673-
return connect
674-
675-
676645
def _populate_input_connections(context, step, connect):
677646
_ensure_inputs_connections(step)
678647
input_connections = step["input_connections"]

gxformat2/model.py

+32
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@
44
DictOrList = Union[Dict, List]
55

66

7+
def pop_connect_from_step_dict(step: dict) -> dict:
8+
if "connect" not in step:
9+
step["connect"] = {}
10+
11+
connect = step["connect"]
12+
del step["connect"]
13+
14+
# handle CWL-style in dict connections.
15+
if "in" in step:
16+
step_in = step["in"]
17+
assert isinstance(step_in, dict)
18+
connection_keys = set()
19+
for key, value in step_in.items():
20+
# TODO: this can be a list right?
21+
if isinstance(value, dict) and 'source' in value:
22+
value = value["source"]
23+
elif isinstance(value, dict) and 'default' in value:
24+
continue
25+
elif isinstance(value, dict):
26+
raise KeyError(f'step input must define either source or default {value}')
27+
connect[key] = [value]
28+
connection_keys.add(key)
29+
30+
for key in connection_keys:
31+
del step_in[key]
32+
33+
if len(step_in) == 0:
34+
del step['in']
35+
36+
return connect
37+
38+
739
def convert_dict_to_id_list_if_needed(
840
dict_or_list: DictOrList,
941
add_label: bool = False,

tests/test_model_helpers.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from gxformat2.model import pop_connect_from_dict
2+
3+
4+
def test_pop_connect():
5+
raw_step = {
6+
"in": {
7+
"bar": {
8+
"source": "foo/moo",
9+
},
10+
},
11+
}
12+
connect = pop_connect_from_dict(raw_step)
13+
assert connect["bar"] == ["foo/moo"]
14+
assert "in" not in raw_step
15+
16+
17+
def test_pop_connect_preserves_defaults():
18+
raw_step = {
19+
"in": {
20+
"bar": {
21+
"default": 7,
22+
},
23+
},
24+
}
25+
connect = pop_connect_from_dict(raw_step)
26+
assert "bar" not in connect
27+
assert "in" in raw_step

0 commit comments

Comments
 (0)