5
5
import numpy as np
6
6
from jobflow import job , Flow
7
7
8
- from python_workflow_definition .shared import get_dict , get_list , get_kwargs , get_source_handles , convert_nodes_list_to_dict
8
+ from python_workflow_definition .shared import (
9
+ get_dict ,
10
+ get_list ,
11
+ get_kwargs ,
12
+ get_source_handles ,
13
+ convert_nodes_list_to_dict ,
14
+ NODES_LABEL ,
15
+ EDGES_LABEL ,
16
+ SOURCE_LABEL ,
17
+ SOURCE_PORT_LABEL ,
18
+ TARGET_LABEL ,
19
+ TARGET_PORT_LABEL ,
20
+ )
9
21
10
22
11
23
def _get_function_dict (flow ):
@@ -26,9 +38,19 @@ def _get_nodes_dict(function_dict):
26
38
27
39
def _get_edge_from_dict (target , key , value_dict , nodes_mapping_dict ):
28
40
if len (value_dict ['attributes' ]) == 1 :
29
- return {"target" : target , "targetPort" : key , "source" : nodes_mapping_dict [value_dict ["uuid" ]], "sourcePort" : value_dict ["attributes" ][0 ][1 ]}
41
+ return {
42
+ TARGET_LABEL : target ,
43
+ TARGET_PORT_LABEL : key ,
44
+ SOURCE_LABEL : nodes_mapping_dict [value_dict ["uuid" ]],
45
+ SOURCE_PORT_LABEL : value_dict ["attributes" ][0 ][1 ],
46
+ }
30
47
else :
31
- return {"target" : target , "targetPort" : key , "source" : nodes_mapping_dict [value_dict ["uuid" ]], "sourcePort" : None }
48
+ return {
49
+ TARGET_LABEL : target ,
50
+ TARGET_PORT_LABEL : key ,
51
+ SOURCE_LABEL : nodes_mapping_dict [value_dict ["uuid" ]],
52
+ SOURCE_PORT_LABEL : None ,
53
+ }
32
54
33
55
34
56
def _get_edges_and_extend_nodes (flow_dict , nodes_mapping_dict , nodes_dict ):
@@ -59,8 +81,8 @@ def _get_edges_and_extend_nodes(flow_dict, nodes_mapping_dict, nodes_dict):
59
81
nodes_dict [node_index ] = vt
60
82
else :
61
83
node_index = {str (tv ): tk for tk , tv in nodes_dict .items ()}[str (vt )]
62
- edges_lst .append ({"target" : node_dict_index , "targetPort" : kt , "source" : node_index , "sourcePort" : None })
63
- edges_lst .append ({"target" : nodes_mapping_dict [job ["uuid" ]], "targetPort" : k , "source" : node_dict_index , "sourcePort" : None })
84
+ edges_lst .append ({TARGET_LABEL : node_dict_index , TARGET_PORT_LABEL : kt , SOURCE_LABEL : node_index , SOURCE_PORT_LABEL : None })
85
+ edges_lst .append ({TARGET_LABEL : nodes_mapping_dict [job ["uuid" ]], TARGET_PORT_LABEL : k , SOURCE_LABEL : node_dict_index , SOURCE_PORT_LABEL : None })
64
86
elif isinstance (v , list ) and any ([isinstance (el , dict ) and "@module" in el and "@class" in el and "@version" in el for el in v ]):
65
87
node_list_index = len (nodes_dict )
66
88
nodes_dict [node_list_index ] = get_list
@@ -78,15 +100,15 @@ def _get_edges_and_extend_nodes(flow_dict, nodes_mapping_dict, nodes_dict):
78
100
nodes_dict [node_index ] = vt
79
101
else :
80
102
node_index = {str (tv ): tk for tk , tv in nodes_dict .items ()}[str (vt )]
81
- edges_lst .append ({"target" : node_list_index , "targetPort" : kt , "source" : node_index , "sourcePort" : None })
82
- edges_lst .append ({"target" : nodes_mapping_dict [job ["uuid" ]], "targetPort" : k , "source" : node_list_index , "sourcePort" : None })
103
+ edges_lst .append ({TARGET_LABEL : node_list_index , TARGET_PORT_LABEL : kt , SOURCE_LABEL : node_index , SOURCE_PORT_LABEL : None })
104
+ edges_lst .append ({TARGET_LABEL : nodes_mapping_dict [job ["uuid" ]], TARGET_PORT_LABEL : k , SOURCE_LABEL : node_list_index , SOURCE_PORT_LABEL : None })
83
105
else :
84
106
if v not in nodes_dict .values ():
85
107
node_index = len (nodes_dict )
86
108
nodes_dict [node_index ] = v
87
109
else :
88
110
node_index = {tv : tk for tk , tv in nodes_dict .items ()}[v ]
89
- edges_lst .append ({"target" : nodes_mapping_dict [job ["uuid" ]], "targetPort" : k , "source" : node_index , "sourcePort" : None })
111
+ edges_lst .append ({TARGET_LABEL : nodes_mapping_dict [job ["uuid" ]], TARGET_PORT_LABEL : k , SOURCE_LABEL : node_index , SOURCE_PORT_LABEL : None })
90
112
return edges_lst , nodes_dict
91
113
92
114
@@ -99,7 +121,7 @@ def _resort_total_lst(total_dict, nodes_dict):
99
121
for ind in sorted (total_dict .keys ()):
100
122
connect = total_dict [ind ]
101
123
if ind not in ordered_lst :
102
- source_lst = [sd ["source" ] for sd in connect .values ()]
124
+ source_lst = [sd [SOURCE_LABEL ] for sd in connect .values ()]
103
125
if all ([s in ordered_lst or s in nodes_without_dep_lst for s in source_lst ]):
104
126
ordered_lst .append (ind )
105
127
total_new_dict [ind ] = connect
@@ -109,11 +131,11 @@ def _resort_total_lst(total_dict, nodes_dict):
109
131
def _group_edges (edges_lst ):
110
132
total_dict = {}
111
133
for ed_major in edges_lst :
112
- target_id = ed_major ["target" ]
134
+ target_id = ed_major [TARGET_LABEL ]
113
135
tmp_lst = []
114
136
if target_id not in total_dict .keys ():
115
137
for ed in edges_lst :
116
- if target_id == ed ["target" ]:
138
+ if target_id == ed [TARGET_LABEL ]:
117
139
tmp_lst .append (ed )
118
140
total_dict [target_id ] = get_kwargs (lst = tmp_lst )
119
141
return total_dict
@@ -139,8 +161,8 @@ def get_attr_helper(obj, source_handle):
139
161
else :
140
162
fn = job (method = v )
141
163
kwargs = {
142
- kw : input_dict [vw ["source" ]] if vw ["source" ] in input_dict else get_attr_helper (
143
- obj = memory_dict [vw ["source" ]], source_handle = vw ["sourcePort" ])
164
+ kw : input_dict [vw [SOURCE_LABEL ]] if vw [SOURCE_LABEL ] in input_dict else get_attr_helper (
165
+ obj = memory_dict [vw [SOURCE_LABEL ]], source_handle = vw [SOURCE_PORT_LABEL ])
144
166
for kw , vw in total_dict [k ].items ()
145
167
}
146
168
memory_dict [k ] = fn (** kwargs )
@@ -159,21 +181,21 @@ def load_workflow_json(file_name):
159
181
content = json .load (f )
160
182
161
183
edges_new_lst = []
162
- for edge in content ["edges" ]:
163
- if edge ["sourcePort" ] is None :
184
+ for edge in content [EDGES_LABEL ]:
185
+ if edge [SOURCE_PORT_LABEL ] is None :
164
186
edges_new_lst .append (edge )
165
187
else :
166
188
edges_new_lst .append (
167
189
{
168
- "target" : edge ["target" ],
169
- "targetPort" : edge ["targetPort" ],
170
- "source" : edge ["source" ],
171
- "sourcePort" : str (edge ["sourcePort" ]),
190
+ TARGET_LABEL : edge [TARGET_LABEL ],
191
+ TARGET_PORT_LABEL : edge [TARGET_PORT_LABEL ],
192
+ SOURCE_LABEL : edge [SOURCE_LABEL ],
193
+ SOURCE_PORT_LABEL : str (edge [SOURCE_PORT_LABEL ]),
172
194
}
173
195
)
174
196
175
197
nodes_new_dict = {}
176
- for k , v in convert_nodes_list_to_dict (nodes_list = content ["nodes" ]).items ():
198
+ for k , v in convert_nodes_list_to_dict (nodes_list = content [NODES_LABEL ]).items ():
177
199
if isinstance (v , str ) and "." in v :
178
200
p , m = v .rsplit ('.' , 1 )
179
201
mod = import_module (p )
@@ -214,4 +236,4 @@ def write_workflow_json(flow, file_name="workflow.json"):
214
236
nodes_store_lst .append ({"id" : k , "value" : v })
215
237
216
238
with open (file_name , "w" ) as f :
217
- json .dump ({"nodes" : nodes_store_lst , "edges" : edges_lst }, f )
239
+ json .dump ({NODES_LABEL : nodes_store_lst , EDGES_LABEL : edges_lst }, f )
0 commit comments