From 5a894efffbd14df748ed744baf00c64d9917248c Mon Sep 17 00:00:00 2001 From: Stefano Borini <sborini@enthought.com> Date: Tue, 22 Aug 2017 11:19:03 +0100 Subject: [PATCH] Introduced UIHooksFactory --- .../base_notification_listener_factory.py | 4 +- .../base_ui_hook_manager.py | 18 -------- force_bdss/ui_hooks/__init__.py | 0 force_bdss/ui_hooks/base_ui_hook_manager.py | 44 +++++++++++++++++++ force_bdss/ui_hooks/base_ui_hooks_factory.py | 44 +++++++++++++++++++ force_bdss/ui_hooks/i_ui_hooks_factory.py | 18 ++++++++ 6 files changed, 109 insertions(+), 19 deletions(-) delete mode 100644 force_bdss/notification_listeners/base_ui_hook_manager.py create mode 100644 force_bdss/ui_hooks/__init__.py create mode 100644 force_bdss/ui_hooks/base_ui_hook_manager.py create mode 100644 force_bdss/ui_hooks/base_ui_hooks_factory.py create mode 100644 force_bdss/ui_hooks/i_ui_hooks_factory.py diff --git a/force_bdss/notification_listeners/base_notification_listener_factory.py b/force_bdss/notification_listeners/base_notification_listener_factory.py index 0dfff5b..5ebff3b 100644 --- a/force_bdss/notification_listeners/base_notification_listener_factory.py +++ b/force_bdss/notification_listeners/base_notification_listener_factory.py @@ -49,9 +49,11 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits): Data to use to fill the model. """ - @abc.abstractmethod def create_ui_hook_manager(self): """Creates an instance of the hook manager The hook manager contains a set of methods that are applicable in various moments of the UI application lifetime. + By default, it returns None, meaning that no hook managers are + installed, and no action will take place. """ + return None diff --git a/force_bdss/notification_listeners/base_ui_hook_manager.py b/force_bdss/notification_listeners/base_ui_hook_manager.py deleted file mode 100644 index 52e9c47..0000000 --- a/force_bdss/notification_listeners/base_ui_hook_manager.py +++ /dev/null @@ -1,18 +0,0 @@ -import abc -from traits.api import ABCHasStrictTraits - - -class BaseUIHookManager(ABCHasStrictTraits): - @abc.abstractmethod - def before_execution(self, application, model): - """Hook that is called before execution of a given model. - Gives a chance to alter the model before the temporary file is created - with its contents and the calculation invoked. - """ - - @abc.abstractmethod - def before_save(self, application, model): - """Hook that is called just before saving a given model to disk - in response to a user action. This does not apply to saving of - temporary files before execution. - """ diff --git a/force_bdss/ui_hooks/__init__.py b/force_bdss/ui_hooks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/force_bdss/ui_hooks/base_ui_hook_manager.py b/force_bdss/ui_hooks/base_ui_hook_manager.py new file mode 100644 index 0000000..5429fa7 --- /dev/null +++ b/force_bdss/ui_hooks/base_ui_hook_manager.py @@ -0,0 +1,44 @@ +import abc +from traits.api import ABCHasStrictTraits, Instance + +from .i_ui_hooks_factory import IUIHooksFactory + + +class BaseUIHookManager(ABCHasStrictTraits): + #: A reference to the factory + factory = Instance(IUIHooksFactory) + + def __init__(self, factory, *args, **kwargs): + """Initializes the notification listener. + + Parameters + ---------- + factory: BaseNotificationListener + The factory this Notification Listener belongs to + """ + self.factory = factory + super(BaseUIHookManager, self).__init__(*args, **kwargs) + + @abc.abstractmethod + def before_execution(self, task): + """Hook that is called before execution of a given evaluation. + Gives a chance to perform operations before the temporary file is + created with its contents and the calculation invoked. + + Parameters + ---------- + task: + The pyface envisage task. + """ + + @abc.abstractmethod + def before_save(self, task): + """Hook that is called just before saving a given model to disk + in response to a user action. This does not apply to saving of + temporary files before execution. + + Parameters + ---------- + task: + The pyface envisage task + """ diff --git a/force_bdss/ui_hooks/base_ui_hooks_factory.py b/force_bdss/ui_hooks/base_ui_hooks_factory.py new file mode 100644 index 0000000..e9b1295 --- /dev/null +++ b/force_bdss/ui_hooks/base_ui_hooks_factory.py @@ -0,0 +1,44 @@ +import abc + +from traits.api import ABCHasStrictTraits, Instance, String, provides +from envisage.plugin import Plugin + +from .i_ui_hooks_factory import IUIHooksFactory + + +@provides(IUIHooksFactory) +class BaseUIHooksFactory(ABCHasStrictTraits): + """Base class for notification listeners. + Notification listeners are extensions that receive event notifications + from the MCO and perform an associated action. + """ + #: identifier of the factory + id = String() + + #: Name of the factory. User friendly for UI + name = String() + + #: A reference to the containing plugin + plugin = Instance(Plugin) + + def __init__(self, plugin, *args, **kwargs): + """Initializes the instance. + + Parameters + ---------- + plugin: Plugin + The plugin that holds this factory. + """ + self.plugin = plugin + super(BaseUIHooksFactory, self).__init__(*args, **kwargs) + + @abc.abstractmethod + def create_ui_hook_manager(self): + """Creates an instance of the hook manager + The hook manager contains a set of methods that are applicable in + various moments of the UI application lifetime. + + Returns + ------- + BaseUIHookManager + """ diff --git a/force_bdss/ui_hooks/i_ui_hooks_factory.py b/force_bdss/ui_hooks/i_ui_hooks_factory.py new file mode 100644 index 0000000..dc60ae0 --- /dev/null +++ b/force_bdss/ui_hooks/i_ui_hooks_factory.py @@ -0,0 +1,18 @@ +from traits.api import Interface, String, Instance +from envisage.plugin import Plugin + + +class IUIHooksFactory(Interface): + """Envisage required interface for the BaseUIHooksFactory. + You should not need to use this directly. + + Refer to the BaseUIHooksFactory for documentation. + """ + id = String() + + name = String() + + plugin = Instance(Plugin) + + def create_hook_manager(self): + """""" -- GitLab