Skip to content

Commit 95c363e

Browse files
committed
Implemented stage history
1 parent 3ac28a6 commit 95c363e

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

awsshell/wizard.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import sys
2+
import copy
13
import json
24
import jmespath
35

46
import botocore.session
57
from botocore import xform_name
68

79
from awsshell.resource import index
8-
from awsshell.interaction import InteractionLoader
10+
from awsshell.interaction import InteractionLoader, SimpleSelect
11+
12+
from prompt_toolkit.shortcuts import confirm
913

1014

1115
class WizardException(Exception):
@@ -118,6 +122,13 @@ def _load_stages(self, stages):
118122
for stage in stages:
119123
self.stages[stage.name] = stage
120124

125+
def _select_stage_prompt(self):
126+
stages = [s.name for (s, _) in self.env._stage_history]
127+
selector = SimpleSelect({}, 'Select a Stage to return to: ')
128+
current_stage = selector.execute(stages)
129+
self.env.pop_stages(stages.index(current_stage))
130+
return current_stage
131+
121132
def execute(self):
122133
"""Run the wizard. Execute Stages until a final stage is reached.
123134
@@ -128,8 +139,18 @@ def execute(self):
128139
stage = self.stages.get(current_stage)
129140
if not stage:
130141
raise WizardException('Stage not found: %s' % current_stage)
131-
stage.execute()
132-
current_stage = stage.get_next_stage()
142+
try:
143+
stage.execute()
144+
current_stage = stage.get_next_stage()
145+
except EOFError:
146+
current_stage = self._select_stage_prompt()
147+
except Exception as e:
148+
sys.stdout.write('{0}\n'.format(e))
149+
sys.stdout.flush()
150+
if confirm('Select a previous stage? (y/n) '):
151+
current_stage = self._select_stage_prompt()
152+
else:
153+
raise
133154

134155

135156
class Stage(object):
@@ -245,6 +266,7 @@ def execute(self):
245266
2) Perform Interaction on retrieved data.
246267
3) Perform Resolution to store data in the environment.
247268
"""
269+
self._env.push_stage(self)
248270
retrieved_options = self._handle_retrieval()
249271
selected_data = self._handle_interaction(retrieved_options)
250272
self._handle_resolution(selected_data)
@@ -253,8 +275,9 @@ def execute(self):
253275
class Environment(object):
254276
"""Store vars into a dict and retrives them with JMESPath queries."""
255277

256-
def __init__(self):
278+
def __init__(self, stage_history=[]):
257279
self._variables = {}
280+
self._stage_history = stage_history
258281

259282
def __str__(self):
260283
return json.dumps(self._variables, indent=4, sort_keys=True)
@@ -291,6 +314,14 @@ def resolve_parameters(self, keys):
291314
:rtype: dict
292315
:return: The dict of with all of the paths resolved to their values.
293316
"""
317+
resolved_dict = {}
294318
for key in keys:
295-
keys[key] = self.retrieve(keys[key])
296-
return keys
319+
resolved_dict[key] = self.retrieve(keys[key])
320+
return resolved_dict
321+
322+
def push_stage(self, stage):
323+
self._stage_history.append((stage, copy.deepcopy(self._variables)))
324+
325+
def pop_stages(self, stage_index):
326+
self._variables = self._stage_history[stage_index][1]
327+
self._stage_history = self._stage_history[:stage_index]

0 commit comments

Comments
 (0)