Skip to content
Snippets Groups Projects
Commit 577db324 authored by Stefano Borini's avatar Stefano Borini
Browse files

Introduced initialize and finalize

parent 4a6e5c26
No related branches found
No related tags found
1 merge request!79Deliver notification info
from __future__ import print_function from __future__ import print_function
import sys import sys
import logging
from traits.api import on_trait_change, Instance, List from traits.api import on_trait_change, Instance, List
...@@ -31,6 +32,17 @@ class CoreMCODriver(BaseCoreDriver): ...@@ -31,6 +32,17 @@ class CoreMCODriver(BaseCoreDriver):
def application_started(self): def application_started(self):
self.mco.run(self.workflow.mco) self.mco.run(self.workflow.mco)
@on_trait_change("application:stopping")
def application_stopping(self):
for listener in self.listeners:
try:
listener.finalize(None)
except Exception as e:
logging.error(
"Failed to finalize "
"listener {}: {}".format(
listener.__class__.__name__, str(e)))
def _mco_default(self): def _mco_default(self):
try: try:
workflow = self.workflow workflow = self.workflow
...@@ -51,8 +63,15 @@ class CoreMCODriver(BaseCoreDriver): ...@@ -51,8 +63,15 @@ class CoreMCODriver(BaseCoreDriver):
listeners = [] listeners = []
for factory in self.factory_registry.notification_listener_factories: for factory in self.factory_registry.notification_listener_factories:
listener = factory.create_listener() try:
listener.init_persistent_state(None) listener = factory.create_listener()
listeners.append(listener) listener.initialize(None)
except Exception as e:
logging.error(
"Failed to create or initialize "
"listener with id {}: {}".format(
factory.id, str(e)))
else:
listeners.append(listener)
return listeners return listeners
...@@ -10,5 +10,8 @@ class DummyNotificationListener(BaseNotificationListener): ...@@ -10,5 +10,8 @@ class DummyNotificationListener(BaseNotificationListener):
elif isinstance(event, MCOProgressEvent): elif isinstance(event, MCOProgressEvent):
print(event.__class__.__name__, event.input, event.output) print(event.__class__.__name__, event.input, event.output)
def init_persistent_state(self, model): def initialize(self, model):
print("Initializing persistent state") print("Initializing persistent state")
def finalize(self, model):
print("Finalizing persistent state")
...@@ -41,7 +41,7 @@ class UINotification(BaseNotificationListener): ...@@ -41,7 +41,7 @@ class UINotification(BaseNotificationListener):
self._msg_cache.append(msg) self._msg_cache.append(msg)
self._pub_socket.send(msg) self._pub_socket.send(msg)
def init_persistent_state(self, model): def initialize(self, model):
self._context = zmq.Context() self._context = zmq.Context()
self._pub_socket = self._context.socket(zmq.PUB) self._pub_socket = self._context.socket(zmq.PUB)
self._pub_socket.bind("tcp://*:12345") self._pub_socket.bind("tcp://*:12345")
...@@ -62,3 +62,9 @@ class UINotification(BaseNotificationListener): ...@@ -62,3 +62,9 @@ class UINotification(BaseNotificationListener):
return None return None
return ("EVENT\n{}".format(data)).encode("utf-8") return ("EVENT\n{}".format(data)).encode("utf-8")
def finalize(self, model):
self._context.destroy()
self._pub_socket = None
self._rep_socket = None
self._context = None
...@@ -24,9 +24,33 @@ class BaseNotificationListener(ABCHasStrictTraits): ...@@ -24,9 +24,33 @@ class BaseNotificationListener(ABCHasStrictTraits):
self.factory = factory self.factory = factory
super(BaseNotificationListener, self).__init__(*args, **kwargs) super(BaseNotificationListener, self).__init__(*args, **kwargs)
def initialize(self, model):
"""
Method used to initialize persistent state of the listener using
information from the model.
Reimplement it in your Notification Listener to perform special
initialization of state that survives across deliver() invocations,
such as setting up a connection, or opening a file.
"""
def finalize(self, model):
"""
Method used to finalize state of the listener.
Reimplement it in your Notification Listener to perform special
finalization of state that survives across deliver() invocations,
such as closing a connection, or closing a file.
"""
@abc.abstractmethod @abc.abstractmethod
def deliver(self, model, message): def deliver(self, model, event):
pass """Delivers the event to the recipient
def init_persistent_state(self, model): Parameters
pass ----------
model:
The model
event: MCOEvent
The event to notify.
"""
import abc import abc
from traits.api import ABCHasStrictTraits, Instance, String, provides, Any from traits.api import ABCHasStrictTraits, Instance, String, provides
from envisage.plugin import Plugin from envisage.plugin import Plugin
from .i_notification_listener_factory import INotificationListenerFactory from .i_notification_listener_factory import INotificationListenerFactory
...@@ -8,14 +8,19 @@ from .i_notification_listener_factory import INotificationListenerFactory ...@@ -8,14 +8,19 @@ from .i_notification_listener_factory import INotificationListenerFactory
@provides(INotificationListenerFactory) @provides(INotificationListenerFactory)
class BaseNotificationListenerFactory(ABCHasStrictTraits): class BaseNotificationListenerFactory(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() id = String()
#: Name of the factory. User friendly for UI
name = String() name = String()
#: A reference to the containing plugin
plugin = Instance(Plugin) plugin = Instance(Plugin)
persistent_state = Any()
def __init__(self, plugin, *args, **kwargs): def __init__(self, plugin, *args, **kwargs):
"""Initializes the instance. """Initializes the instance.
...@@ -29,8 +34,17 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits): ...@@ -29,8 +34,17 @@ class BaseNotificationListenerFactory(ABCHasStrictTraits):
@abc.abstractmethod @abc.abstractmethod
def create_listener(self): def create_listener(self):
"""""" """
Creates an instance of the listener.
"""
@abc.abstractmethod @abc.abstractmethod
def create_model(self, model_data=None): def create_model(self, model_data=None):
"""""" """
Creates an instance of the model.
Parameters
----------
model_data: dict
Data to use to fill the model.
"""
from traits.api import Interface, String, Instance, Any from traits.api import Interface, String, Instance
from envisage.plugin import Plugin from envisage.plugin import Plugin
...@@ -6,7 +6,7 @@ class INotificationListenerFactory(Interface): ...@@ -6,7 +6,7 @@ class INotificationListenerFactory(Interface):
"""Envisage required interface for the BaseNotificationListenerFactory. """Envisage required interface for the BaseNotificationListenerFactory.
You should not need to use this directly. You should not need to use this directly.
Refer to the BaseNotifier for documentation. Refer to the BaseNotificationListenerFactory for documentation.
""" """
id = String() id = String()
...@@ -14,13 +14,8 @@ class INotificationListenerFactory(Interface): ...@@ -14,13 +14,8 @@ class INotificationListenerFactory(Interface):
plugin = Instance(Plugin) plugin = Instance(Plugin)
persistent_state = Any
def create_listener(self): def create_listener(self):
"""""" """"""
def create_model(self, model_data=None): def create_model(self, model_data=None):
"""""" """"""
def init_persistent_state(self):
pass
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