-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1041 from mantidproject/839_add_ads_observer
Added ADSObserver to MSlice
- Loading branch information
Showing
8 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from functools import wraps | ||
import sys | ||
|
||
from mantid.api import AnalysisDataServiceObserver | ||
|
||
|
||
def _catch_exceptions(func): | ||
""" | ||
Catch all exceptions in method and print a traceback to stderr | ||
""" | ||
|
||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
func(*args, **kwargs) | ||
except Exception: | ||
sys.stderr.write("Error occurred in handler:\n") | ||
import traceback | ||
|
||
traceback.print_exc() | ||
|
||
return wrapper | ||
|
||
|
||
class MSliceADSObserver(AnalysisDataServiceObserver): | ||
def __init__(self, delete_callback, clear_callback, rename_callback): | ||
super(MSliceADSObserver, self).__init__() | ||
self.delete_callback = delete_callback | ||
self.clear_callback = clear_callback | ||
self.rename_callback = rename_callback | ||
|
||
self.observeDelete(True) | ||
self.observeRename(True) | ||
self.observeClear(True) | ||
|
||
@_catch_exceptions | ||
def deleteHandle(self, workspace_name, workspace): | ||
""" | ||
Called when the ADS deletes a workspace, removes it from the dict of tracked workspaces. | ||
:param workspace_name: name of the workspace | ||
:param workspace: reference to the workspace (not used) | ||
""" | ||
self.delete_callback(workspace_name) | ||
|
||
@_catch_exceptions | ||
def renameHandle(self, old_workspace_name, new_workspace_name): | ||
""" | ||
Called when the ADS renames a workspace, updates the dict with the new name. | ||
:param old_workspace_name: original name of the workspace | ||
:param new_workspace_name: new name for the workspace | ||
""" | ||
self.rename_callback(old_workspace_name, new_workspace_name) | ||
|
||
@_catch_exceptions | ||
def clearHandle(self): | ||
""" | ||
Called when the ADS has been cleared, removes all data. | ||
""" | ||
self.clear_callback() |
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from __future__ import (absolute_import, division, print_function) | ||
import unittest | ||
|
||
from mock import MagicMock | ||
|
||
from mantid.api import AnalysisDataService | ||
from mantid.simpleapi import CreateSampleWorkspace, RenameWorkspace | ||
|
||
from mslice.models.mslice_ads_observer import MSliceADSObserver | ||
from mslice.presenters.workspace_manager_presenter import WorkspaceManagerPresenter | ||
|
||
|
||
class WorkspaceManagerPresenterTest(unittest.TestCase): | ||
|
||
def test_ensure_that_the_ads_observer_calls_delete_handle(self): | ||
presenter = WorkspaceManagerPresenter(MagicMock()) | ||
presenter.delete_handle = MagicMock() | ||
self.assertTrue(isinstance(presenter._ads_observer, MSliceADSObserver)) | ||
presenter._ads_observer = MSliceADSObserver( | ||
presenter.delete_handle, presenter.clear_handle, presenter.rename_handle | ||
) | ||
|
||
CreateSampleWorkspace(OutputWorkspace="ws", StoreInADS=True) | ||
AnalysisDataService.remove("ws") | ||
|
||
presenter.delete_handle.assert_called_once_with("ws") | ||
|
||
def test_ensure_that_the_ads_observer_calls_rename_handle(self): | ||
presenter = WorkspaceManagerPresenter(MagicMock()) | ||
presenter.rename_handle = MagicMock() | ||
self.assertTrue(isinstance(presenter._ads_observer, MSliceADSObserver)) | ||
presenter._ads_observer = MSliceADSObserver( | ||
presenter.delete_handle, presenter.clear_handle, presenter.rename_handle | ||
) | ||
|
||
CreateSampleWorkspace(OutputWorkspace="ws", StoreInADS=True) | ||
RenameWorkspace(InputWorkspace="ws", OutputWorkspace="ws1") | ||
|
||
presenter.rename_handle.assert_called_once_with("ws", "ws1") | ||
|
||
def test_ensure_that_the_ads_observer_calls_clear_handle(self): | ||
presenter = WorkspaceManagerPresenter(MagicMock()) | ||
presenter.clear_handle = MagicMock() | ||
self.assertTrue(isinstance(presenter._ads_observer, MSliceADSObserver)) | ||
presenter._ads_observer = MSliceADSObserver( | ||
presenter.delete_handle, presenter.clear_handle, presenter.rename_handle | ||
) | ||
|
||
CreateSampleWorkspace(OutputWorkspace="ws", StoreInADS=True) | ||
AnalysisDataService.clear(True) | ||
|
||
presenter.clear_handle.assert_called_once() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains 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