@@ -111,20 +111,21 @@ def create_wizard(self, model):
111
111
stages = self ._load_stages (model .get ('Stages' ), env )
112
112
return Wizard (start_stage , stages , env , self ._error_handler )
113
113
114
+ def _load_stage (self , stage , env ):
115
+ stage_attrs = {
116
+ 'name' : stage .get ('Name' ),
117
+ 'prompt' : stage .get ('Prompt' ),
118
+ 'retrieval' : stage .get ('Retrieval' ),
119
+ 'next_stage' : stage .get ('NextStage' ),
120
+ 'resolution' : stage .get ('Resolution' ),
121
+ 'interaction' : stage .get ('Interaction' ),
122
+ }
123
+ creator = self ._cached_creator
124
+ interaction = self ._interaction_loader
125
+ return Stage (env , creator , interaction , self , ** stage_attrs )
126
+
114
127
def _load_stages (self , stages , env ):
115
- def load_stage (stage ):
116
- stage_attrs = {
117
- 'name' : stage .get ('Name' ),
118
- 'prompt' : stage .get ('Prompt' ),
119
- 'retrieval' : stage .get ('Retrieval' ),
120
- 'next_stage' : stage .get ('NextStage' ),
121
- 'resolution' : stage .get ('Resolution' ),
122
- 'interaction' : stage .get ('Interaction' ),
123
- }
124
- creator = self ._cached_creator
125
- loader = self ._interaction_loader
126
- return Stage (env , creator , loader , ** stage_attrs )
127
- return [load_stage (stage ) for stage in stages ]
128
+ return [self ._load_stage (stage , env ) for stage in stages ]
128
129
129
130
130
131
class Wizard (object ):
@@ -177,8 +178,10 @@ def execute(self):
177
178
raise WizardException ('Stage not found: %s' % current_stage )
178
179
try :
179
180
self ._push_stage (stage )
180
- stage .execute ()
181
+ stage_data = stage .execute ()
181
182
current_stage = stage .get_next_stage ()
183
+ if current_stage is None :
184
+ return stage_data
182
185
except Exception as err :
183
186
stages = [s .name for (s , _ ) in self ._stage_history ]
184
187
recovery = self ._error_handler (err , stages )
@@ -199,9 +202,9 @@ def _pop_stages(self, stage_index):
199
202
class Stage (object ):
200
203
"""The Stage object. Contains logic to run all steps of the stage."""
201
204
202
- def __init__ (self , env , creator , interaction_loader , name = None ,
203
- prompt = None , retrieval = None , next_stage = None , resolution = None ,
204
- interaction = None ):
205
+ def __init__ (self , env , creator , interaction_loader , wizard_loader ,
206
+ name = None , prompt = None , retrieval = None , next_stage = None ,
207
+ resolution = None , interaction = None ):
205
208
"""Construct a new Stage object.
206
209
207
210
:type env: :class:`Environment`
@@ -235,6 +238,7 @@ def __init__(self, env, creator, interaction_loader, name=None,
235
238
"""
236
239
self ._env = env
237
240
self ._cached_creator = creator
241
+ self ._wizard_loader = wizard_loader
238
242
self ._interaction_loader = interaction_loader
239
243
self .name = name
240
244
self .prompt = prompt
@@ -270,6 +274,11 @@ def _handle_request_retrieval(self):
270
274
# execute operation passing all parameters
271
275
return operation (** parameters )
272
276
277
+ def _handle_wizard_delegation (self ):
278
+ wizard_name = self .retrieval ['Resource' ]
279
+ wizard = self ._wizard_loader .load_wizard (wizard_name )
280
+ return wizard .execute ()
281
+
273
282
def _handle_retrieval (self ):
274
283
# In case of no retrieval, empty dict
275
284
if not self .retrieval :
@@ -278,14 +287,15 @@ def _handle_retrieval(self):
278
287
data = self ._handle_static_retrieval ()
279
288
elif self .retrieval ['Type' ] == 'Request' :
280
289
data = self ._handle_request_retrieval ()
290
+ elif self .retrieval ['Type' ] == 'Wizard' :
291
+ data = self ._handle_wizard_delegation ()
281
292
# Apply JMESPath query if given
282
293
if self .retrieval .get ('Path' ):
283
294
data = jmespath .search (self .retrieval ['Path' ], data )
284
295
285
296
return data
286
297
287
298
def _handle_interaction (self , data ):
288
-
289
299
# if no interaction step, just forward data
290
300
if self .interaction is None :
291
301
return data
@@ -299,6 +309,7 @@ def _handle_resolution(self, data):
299
309
if self .resolution .get ('Path' ):
300
310
data = jmespath .search (self .resolution ['Path' ], data )
301
311
self ._env .store (self .resolution ['Key' ], data )
312
+ return data
302
313
303
314
def get_next_stage (self ):
304
315
"""Resolve the next stage name for the stage after this one.
@@ -322,7 +333,8 @@ def execute(self):
322
333
"""
323
334
retrieved_options = self ._handle_retrieval ()
324
335
selected_data = self ._handle_interaction (retrieved_options )
325
- self ._handle_resolution (selected_data )
336
+ resolved_data = self ._handle_resolution (selected_data )
337
+ return resolved_data
326
338
327
339
328
340
class Environment (object ):
0 commit comments