-
-
Notifications
You must be signed in to change notification settings - Fork 17
Adding a post hook that can run a measurement - try 2 #151
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
base: main
Are you sure you want to change the base?
Changes from all commits
21b3546
b5ff715
422554d
f6cedd8
65c6461
2873797
5b49e01
2286a80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright 2015-2018 by Exopy Authors, see AUTHORS for more details. | ||
| # | ||
| # Distributed under the terms of the BSD license. | ||
| # | ||
| # The full license is in the file LICENCE, distributed with this software. | ||
| # ----------------------------------------------------------------------------- | ||
| """Implementaion of the AddTaskHook hook. | ||
|
|
||
| """ | ||
| from enaml.workbench.api import Workbench | ||
| from atom.api import Typed, Unicode, Tuple | ||
| from .base_hooks import BasePostExecutionHook | ||
| from ...tasks.api import RootTask | ||
| from ..engines.base_engine import ExecutionInfos, BaseEngine | ||
|
|
||
|
|
||
| class AddTasksHook(BasePostExecutionHook): | ||
| """Post-execusion hook to add a hierarchy of tasks. | ||
|
|
||
| """ | ||
| #: Reference to the root task at the base of the hierarchy | ||
| root_task = Typed(RootTask) | ||
|
|
||
| #: Reference to the measurement workbench | ||
|
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. application workbench The workbench is shared by the whole application and is not specific to the measurement plugin. |
||
| workbench = Typed(Workbench) | ||
|
|
||
| #: Reference to the measurement engine | ||
| engine = Typed(BaseEngine) | ||
|
|
||
| #: Reference to the hook root task path; | ||
| #: it is the same as the one of the measurement and will not be displayed | ||
| default_path = Unicode() | ||
|
|
||
| #: Reference to the build and runtime dependencies of the hook tasks | ||
| dependencies = Tuple() | ||
|
|
||
| def __init__(self, declaration, workbench): | ||
| self.root_task = RootTask() | ||
| self.workbench = workbench | ||
| super().__init__(declaration=declaration) | ||
|
|
||
| def check(self, workbench, **kwargs): | ||
| """ Check that the post-hook task can be executed | ||
|
|
||
| """ | ||
| # set the root_task default path to the one of the measure | ||
| self.root_task.default_path = self.measurement.root_task.default_path | ||
| res, traceback = self.root_task.check() | ||
| return res, traceback | ||
|
|
||
| def run(self, workbench, engine): | ||
| """ Execute the post-hook task | ||
|
|
||
| """ | ||
| # measure has collected the runtime dependencies given by list_runtimes | ||
| meas_deps = self.measurement.dependencies | ||
| runtime_deps = meas_deps.get_runtime_dependencies(self.declaration.id) | ||
| # on the other hand, we need to collect the build dependencies | ||
| build_deps = self.dependencies[0].dependencies | ||
| cmd = 'exopy.app.dependencies.collect' | ||
| core = workbench.get_plugin('enaml.workbench.core') | ||
| deps = core.invoke_command(cmd, dict(dependencies=build_deps, | ||
| kind='build')) | ||
| if deps.errors: | ||
| raise RuntimeError('Error when collecting the build dependencies') | ||
|
|
||
| infos = ExecutionInfos(id=self.measurement.id+'.posttask', | ||
| task=self.root_task, | ||
| build_deps=deps.dependencies, | ||
| runtime_deps=runtime_deps, | ||
| observed_entries=[], # no monitor for the hooks | ||
| checks=not self.measurement.forced_enqueued, | ||
| ) | ||
| execution_result = engine.perform(infos) | ||
| self.engine = engine | ||
| return execution_result | ||
|
|
||
| def pause(self): | ||
| """ Pause the task | ||
|
|
||
| """ | ||
| self.engine.pause() | ||
|
|
||
| def resume(self): | ||
| """ Resume the task | ||
|
|
||
| """ | ||
| self.engine.resume() | ||
|
|
||
| def stop(self, force=False): | ||
| """ Stop the task | ||
|
|
||
| """ | ||
| self.engine.stop(force) | ||
|
|
||
| def list_runtimes(self, workbench): | ||
| """ Returns the run_time dependencies | ||
|
|
||
| """ | ||
| cmd = 'exopy.app.dependencies.analyse' | ||
| core = workbench.get_plugin('enaml.workbench.core') | ||
| deps = core.invoke_command(cmd, | ||
| {'obj': self.root_task, | ||
| 'dependencies': ['build', 'runtime']}) | ||
| self.dependencies = deps | ||
| return deps[1] | ||
|
|
||
| def get_state(self): | ||
| """ Return the informations to save the post hook | ||
|
|
||
| """ | ||
| core = self.workbench.get_plugin('enaml.workbench.core') | ||
| cmd = 'exopy.tasks.save' | ||
| task_prefs = core.invoke_command(cmd, {'task': self.root_task}, self) | ||
| return task_prefs | ||
|
|
||
| def set_state(self, state): | ||
| """ Load the post hook | ||
|
|
||
| """ | ||
| cmd = 'exopy.tasks.build_root' | ||
| kwarg = {'mode': 'from config', 'config': state, | ||
| 'build_dep': self.workbench} | ||
| try: | ||
| core = self.workbench.get_plugin('enaml.workbench.core') | ||
| self.root_task = core.invoke_command(cmd, kwarg) | ||
| except Exception: | ||
| msg = 'Building %s, failed to restore post hook task : %s' | ||
| errors['post hook'] = msg % (state.get('name'), format_exc()) | ||
| return None, errors | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # ----------------------------------------------------------------------------- | ||
| # Copyright 2015-2018 by Exopy Authors, see AUTHORS for more details. | ||
| # | ||
| # Distributed under the terms of the BSD license. | ||
| # | ||
| # The full license is in the file LICENCE, distributed with this software. | ||
| # ----------------------------------------------------------------------------- | ||
| """Widget associated with the AddTaskHook. | ||
|
|
||
| """ | ||
| from atom.api import Typed | ||
| from enaml.widgets.api import Container, PushButton, Label | ||
| from ...tasks.tasks.base_views import RootTaskView | ||
|
|
||
|
|
||
| enamldef AddTasksView(Container): | ||
| """ Widget used for the AddTaskHook | ||
|
|
||
| """ | ||
| #: Reference to the hook edited with this view | ||
| attr hook | ||
|
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. Same those deserve a #: comment |
||
|
|
||
| #: Reference to the corresponding declaration | ||
| attr declaration | ||
|
|
||
| #: Reference to the corresponding measurement workbench | ||
|
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. Same here |
||
| attr workbench | ||
|
|
||
| hug_width = 'ignore' | ||
| hug_height = 'ignore' | ||
| RootTaskView: view: | ||
| core = workbench.get_plugin('enaml.workbench.core') | ||
| task = hook.root_task | ||
| show_path = False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ from ..instruments.api import InstrUser | |
|
|
||
| from .engines.process_engine import ProcessEngine | ||
| from .editors.api import Editor | ||
| from .hooks.api import PreExecutionHook | ||
| from .hooks.api import PreExecutionHook, PostExecutionHook | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -216,6 +216,25 @@ enamldef MeasureManifest(PluginManifest): manifest: | |
| from .hooks.internal_checks import InternalChecksHook | ||
| return InternalChecksHook(declaration=self) | ||
|
|
||
| Extension: | ||
| id = 'post-execution' | ||
| point = manifest.id + '.post-execution' | ||
| PostExecutionHook: | ||
| id = 'exopy.addtask_hook' | ||
|
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 must say I am not a big fan fan of this name... RunTaskHook perhaps ? |
||
| description = ('Run an additional task at the end of a measure,' | ||
| 'even if it is stopped.') | ||
|
|
||
| new => (workbench, default=False): | ||
| from .hooks.addtask_hook import AddTasksHook | ||
| return AddTasksHook(declaration=self, | ||
| workbench=workbench) | ||
|
|
||
| make_view => (workbench, hook): | ||
| with enaml.imports(): | ||
| from .hooks.addtask_view import AddTasksView | ||
| return AddTasksView(declaration=self, hook=hook, | ||
| workbench=workbench) | ||
|
|
||
| Extension: | ||
| id = 'preferences' | ||
| point = 'exopy.app.preferences.plugin' | ||
|
|
||
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.
All those attributes should be documented using #: comments.