-
Notifications
You must be signed in to change notification settings - Fork 784
Delegation #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Delegation #145
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,20 +111,21 @@ def create_wizard(self, model): | |
| stages = self._load_stages(model.get('Stages'), env) | ||
| return Wizard(start_stage, stages, env, self._error_handler) | ||
|
|
||
| def _load_stage(self, stage, env): | ||
| stage_attrs = { | ||
| 'name': stage.get('Name'), | ||
| 'prompt': stage.get('Prompt'), | ||
| 'retrieval': stage.get('Retrieval'), | ||
| 'next_stage': stage.get('NextStage'), | ||
| 'resolution': stage.get('Resolution'), | ||
| 'interaction': stage.get('Interaction'), | ||
| } | ||
| creator = self._cached_creator | ||
| interaction = self._interaction_loader | ||
| return Stage(env, creator, interaction, self, **stage_attrs) | ||
|
|
||
| def _load_stages(self, stages, env): | ||
| def load_stage(stage): | ||
| stage_attrs = { | ||
| 'name': stage.get('Name'), | ||
| 'prompt': stage.get('Prompt'), | ||
| 'retrieval': stage.get('Retrieval'), | ||
| 'next_stage': stage.get('NextStage'), | ||
| 'resolution': stage.get('Resolution'), | ||
| 'interaction': stage.get('Interaction'), | ||
| } | ||
| creator = self._cached_creator | ||
| loader = self._interaction_loader | ||
| return Stage(env, creator, loader, **stage_attrs) | ||
| return [load_stage(stage) for stage in stages] | ||
| return [self._load_stage(stage, env) for stage in stages] | ||
|
|
||
|
|
||
| class Wizard(object): | ||
|
|
@@ -177,8 +178,10 @@ def execute(self): | |
| raise WizardException('Stage not found: %s' % current_stage) | ||
| try: | ||
| self._push_stage(stage) | ||
| stage.execute() | ||
| stage_data = stage.execute() | ||
| current_stage = stage.get_next_stage() | ||
| if current_stage is None: | ||
| return stage_data | ||
| except Exception as err: | ||
| stages = [s.name for (s, _) in self._stage_history] | ||
| recovery = self._error_handler(err, stages) | ||
|
|
@@ -199,9 +202,9 @@ def _pop_stages(self, stage_index): | |
| class Stage(object): | ||
| """The Stage object. Contains logic to run all steps of the stage.""" | ||
|
|
||
| def __init__(self, env, creator, interaction_loader, name=None, | ||
| prompt=None, retrieval=None, next_stage=None, resolution=None, | ||
| interaction=None): | ||
| def __init__(self, env, creator, interaction_loader, wizard_loader, | ||
| name=None, prompt=None, retrieval=None, next_stage=None, | ||
| resolution=None, interaction=None): | ||
| """Construct a new Stage object. | ||
|
|
||
| :type env: :class:`Environment` | ||
|
|
@@ -235,6 +238,7 @@ def __init__(self, env, creator, interaction_loader, name=None, | |
| """ | ||
| self._env = env | ||
| self._cached_creator = creator | ||
| self._wizard_loader = wizard_loader | ||
| self._interaction_loader = interaction_loader | ||
| self.name = name | ||
| self.prompt = prompt | ||
|
|
@@ -270,6 +274,11 @@ def _handle_request_retrieval(self): | |
| # execute operation passing all parameters | ||
| return operation(**parameters) | ||
|
|
||
| def _handle_wizard_delegation(self): | ||
| wizard_name = self.retrieval['Resource'] | ||
| wizard = self._wizard_loader.load_wizard(wizard_name) | ||
| return wizard.execute() | ||
|
|
||
| def _handle_retrieval(self): | ||
| # In case of no retrieval, empty dict | ||
| if not self.retrieval: | ||
|
|
@@ -278,14 +287,15 @@ def _handle_retrieval(self): | |
| data = self._handle_static_retrieval() | ||
| elif self.retrieval['Type'] == 'Request': | ||
| data = self._handle_request_retrieval() | ||
| elif self.retrieval['Type'] == 'Wizard': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am fine with how you have it, but here is a cool way that we often use to avoid having to update an if statement every time you add a new type: retrieval_type = self.retrieval.get('Type', 'default').lower()
data = getattr(self, '_handle_' + retrieval_type + '_retrieval', self._handle_default_retrieval)() |
||
| data = self._handle_wizard_delegation() | ||
| # Apply JMESPath query if given | ||
| if self.retrieval.get('Path'): | ||
| data = jmespath.search(self.retrieval['Path'], data) | ||
|
|
||
| return data | ||
|
|
||
| def _handle_interaction(self, data): | ||
|
|
||
| # if no interaction step, just forward data | ||
| if self.interaction is None: | ||
| return data | ||
|
|
@@ -299,6 +309,7 @@ def _handle_resolution(self, data): | |
| if self.resolution.get('Path'): | ||
| data = jmespath.search(self.resolution['Path'], data) | ||
| self._env.store(self.resolution['Key'], data) | ||
| return data | ||
|
|
||
| def get_next_stage(self): | ||
| """Resolve the next stage name for the stage after this one. | ||
|
|
@@ -322,7 +333,8 @@ def execute(self): | |
| """ | ||
| retrieved_options = self._handle_retrieval() | ||
| selected_data = self._handle_interaction(retrieved_options) | ||
| self._handle_resolution(selected_data) | ||
| resolved_data = self._handle_resolution(selected_data) | ||
| return resolved_data | ||
|
|
||
|
|
||
| class Environment(object): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad that you removed that. Usually it is not great practice to define a function inside a function, especially if it will be called more than once.