Skip to content
Snippets Groups Projects
Commit c7dd4ec2 authored by Stefano Borini's avatar Stefano Borini Committed by GitHub
Browse files

Merge pull request #93 from force-h2020/ui-hook-manager

UI Hooks
parents f4495333 e10653ba
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ from .data_sources.i_data_source_factory import ( ...@@ -9,6 +9,7 @@ from .data_sources.i_data_source_factory import (
IDataSourceFactory) IDataSourceFactory)
from .kpi.i_kpi_calculator_factory import IKPICalculatorFactory from .kpi.i_kpi_calculator_factory import IKPICalculatorFactory
from .mco.i_mco_factory import IMCOFactory from .mco.i_mco_factory import IMCOFactory
from .ui_hooks.i_ui_hooks_factory import IUIHooksFactory
FACTORY_REGISTRY_PLUGIN_ID = "force.bdss.plugins.factory_registry" FACTORY_REGISTRY_PLUGIN_ID = "force.bdss.plugins.factory_registry"
...@@ -50,6 +51,14 @@ class FactoryRegistryPlugin(Plugin): ...@@ -50,6 +51,14 @@ class FactoryRegistryPlugin(Plugin):
id=ExtensionPointID.NOTIFICATION_LISTENER_FACTORIES id=ExtensionPointID.NOTIFICATION_LISTENER_FACTORIES
) )
#: UI Hooks are pluggable entities holding methods that are called
#: at specific moments in the UI application lifetime. They can be used
#: to inject special behaviors at those moments.
ui_hooks_factories = ExtensionPoint(
List(IUIHooksFactory),
id=ExtensionPointID.UI_HOOKS_FACTORIES
)
def data_source_factory_by_id(self, id): def data_source_factory_by_id(self, id):
"""Finds a given data source factory by means of its id. """Finds a given data source factory by means of its id.
The ID is as obtained by the function factory_id() in the The ID is as obtained by the function factory_id() in the
...@@ -140,13 +149,13 @@ class FactoryRegistryPlugin(Plugin): ...@@ -140,13 +149,13 @@ class FactoryRegistryPlugin(Plugin):
def notification_listener_factory_by_id(self, id): def notification_listener_factory_by_id(self, id):
"""Finds a given notification listener by means of its id. """Finds a given notification listener by means of its id.
The ID is as obtained by the function bundle_id() in the The ID is as obtained by the function factory_id() in the
plugin api. plugin api.
Parameters Parameters
---------- ----------
id: str id: str
The identifier returned by the bundle_id() function. The identifier returned by the factory_id() function.
Raises Raises
------ ------
......
...@@ -14,6 +14,7 @@ class ExtensionPointID: ...@@ -14,6 +14,7 @@ class ExtensionPointID:
KPI_CALCULATOR_FACTORIES = 'force.bdss.kpi_calculator.factories' KPI_CALCULATOR_FACTORIES = 'force.bdss.kpi_calculator.factories'
NOTIFICATION_LISTENER_FACTORIES = \ NOTIFICATION_LISTENER_FACTORIES = \
'force.bdss.notification_listener.factories' 'force.bdss.notification_listener.factories'
UI_HOOKS_FACTORIES = 'force.bdss.ui_hooks.factories'
def factory_id(producer, identifier): def factory_id(producer, identifier):
......
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 UIHooksFactory.
UI Hooks are extensions that perform actions associated to specific
moments of the UI lifetime.
"""
#: 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_hooks_manager(self):
"""Creates an instance of the hook manager.
The hooks manager contains a set of methods that are applicable in
various moments of the UI application lifetime.
Returns
-------
BaseUIHooksManager
"""
from traits.api import HasStrictTraits, Instance
from .i_ui_hooks_factory import IUIHooksFactory
class BaseUIHooksManager(HasStrictTraits):
#: A reference to the factory
factory = Instance(IUIHooksFactory)
def __init__(self, factory, *args, **kwargs):
"""Initializes the UI Hooks manager.
Parameters
----------
factory: BaseUIHooksFactory
The factory this UI Hooks manager belongs to
"""
self.factory = factory
super(BaseUIHooksManager, self).__init__(*args, **kwargs)
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.
"""
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
"""
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):
""""""
import unittest
try:
import mock
except ImportError:
from unittest import mock
from envisage.api import Plugin
from ..base_ui_hooks_factory import BaseUIHooksFactory
class NullUIHooksFactory(BaseUIHooksFactory):
def create_ui_hooks_manager(self):
return None
class TestBaseUIHooksFactory(unittest.TestCase):
def test_initialize(self):
mock_plugin = mock.Mock(spec=Plugin)
factory = NullUIHooksFactory(plugin=mock_plugin)
self.assertEqual(factory.plugin, mock_plugin)
import unittest
from ..base_ui_hooks_manager import BaseUIHooksManager
from ..base_ui_hooks_factory import BaseUIHooksFactory
try:
import mock
except ImportError:
from unittest import mock
class TestBaseUIHooksManager(unittest.TestCase):
def test_initialization(self):
mock_factory = mock.Mock(spec=BaseUIHooksFactory)
mgr = BaseUIHooksManager(mock_factory)
self.assertEqual(mgr.factory, mock_factory)
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