Skip to content
Snippets Groups Projects
Commit ae2f6454 authored by Conor Finn's avatar Conor Finn
Browse files

RE #27777 Implement ADS observer pattern

parent dd0e9f8d
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,17 @@ class FittingDataPresenter(object): ...@@ -29,6 +29,17 @@ class FittingDataPresenter(object):
if ws_name in self.model.get_loaded_workspaces(): if ws_name in self.model.get_loaded_workspaces():
self.model.get_loaded_workspaces().pop(ws_name) self.model.get_loaded_workspaces().pop(ws_name)
def rename_workspace(self, old_name, new_name):
if old_name in self.model.get_loaded_workspaces():
self.model.get_loaded_workspaces()[new_name] = self.model.get_loaded_workspaces().pop(old_name)
def clear_workspaces(self):
self.model.get_loaded_workspaces().clear()
def replace_workspace(self, name, workspace):
if name in self.model.get_loaded_workspaces():
self.model.get_loaded_workspaces()[name] = workspace
def get_loaded_workspaces(self): def get_loaded_workspaces(self):
return self.model.get_loaded_workspaces() return self.model.get_loaded_workspaces()
......
...@@ -9,6 +9,8 @@ from Engineering.gui.engineering_diffraction.tabs.fitting.data_handling.data_mod ...@@ -9,6 +9,8 @@ from Engineering.gui.engineering_diffraction.tabs.fitting.data_handling.data_mod
from Engineering.gui.engineering_diffraction.tabs.fitting.data_handling.data_view import FittingDataView from Engineering.gui.engineering_diffraction.tabs.fitting.data_handling.data_view import FittingDataView
from Engineering.gui.engineering_diffraction.tabs.fitting.data_handling.data_presenter import FittingDataPresenter from Engineering.gui.engineering_diffraction.tabs.fitting.data_handling.data_presenter import FittingDataPresenter
from Engineering.gui.engineering_diffraction.tabs.fitting.fitting_ads_observer import FittingADSObserver
class FittingDataWidget(object): class FittingDataWidget(object):
def __init__(self, parent, view=None): def __init__(self, parent, view=None):
...@@ -20,8 +22,20 @@ class FittingDataWidget(object): ...@@ -20,8 +22,20 @@ class FittingDataWidget(object):
self.model = FittingDataModel() self.model = FittingDataModel()
self.presenter = FittingDataPresenter(self.model, self.view) self.presenter = FittingDataPresenter(self.model, self.view)
self.ads_observer = FittingADSObserver(self.remove_workspace, self.clear_workspaces,
self.replace_workspace, self.rename_workspace)
def get_loaded_workspaces(self): def get_loaded_workspaces(self):
return self.presenter.get_loaded_workspaces() return self.presenter.get_loaded_workspaces()
def remove_workspace(self, workspace): def remove_workspace(self, workspace):
self.presenter.remove_workspace(workspace) self.presenter.remove_workspace(workspace)
def rename_workspace(self, old_name, new_name):
self.presenter.rename_workspace(old_name, new_name)
def clear_workspaces(self):
self.presenter.clear_workspaces()
def replace_workspace(self, name, workspace):
self.presenter.replace_workspace(name, workspace)
\ No newline at end of file
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source
# & Institut Laue - Langevin
# SPDX - License - Identifier: GPL - 3.0 +
from mantid.api import AnalysisDataServiceObserver
from functools import wraps
import sys
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 FittingADSObserver(AnalysisDataServiceObserver):
def __init__(self, delete_callback, clear_callback, replace_callback, rename_callback):
super(FittingADSObserver, self).__init__()
self.delete_callback = delete_callback
self.clear_callback = clear_callback
self.replace_callback = replace_callback
self.rename_callback = rename_callback
self.observeDelete(True)
self.observeRename(True)
self.observeReplace(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()
@_catch_exceptions
def replaceHandle(self, name, workspace):
"""
Called when the ADS has replaced a workspace with one of the same name.
Updates the workspace stored in the dict.
:param name: The name of the workspace.
:param workspace: A reference to the new workspace
"""
self.replace_callback(name, workspace)
def unsubscribe(self):
self.observeDelete(False)
self.observeRename(False)
self.observeClear(False)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment