diff --git a/force_bdss/core_plugins/dummy/dummy_plugin.py b/force_bdss/core_plugins/dummy/dummy_plugin.py index 63a229b3ac9199b6c71d89b6eabaa799b857a0d0..c8020e198a858cfb312d0080c56a1f31b06c1d26 100644 --- a/force_bdss/core_plugins/dummy/dummy_plugin.py +++ b/force_bdss/core_plugins/dummy/dummy_plugin.py @@ -2,7 +2,6 @@ from force_bdss.api import BaseExtensionPlugin, plugin_id from .dummy_notification_listener.dummy_notification_listener_factory import ( DummyNotificationListenerFactory ) -from .ui_notification.ui_notification_factory import UINotificationFactory from .csv_extractor.csv_extractor_factory import CSVExtractorFactory from .kpi_adder.kpi_adder_factory import KPIAdderFactory from .dummy_dakota.dakota_factory import DummyDakotaFactory @@ -28,5 +27,4 @@ class DummyPlugin(BaseExtensionPlugin): def _notification_listener_factories_default(self): return [DummyNotificationListenerFactory(self), - UINotificationFactory(self) ] diff --git a/force_bdss/core_plugins/dummy/ui_notification/__init__.py b/force_bdss/core_plugins/dummy/ui_notification/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/force_bdss/core_plugins/dummy/ui_notification/tests/__init__.py b/force_bdss/core_plugins/dummy/ui_notification/tests/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/force_bdss/core_plugins/dummy/ui_notification/tests/test_ui_notification.py b/force_bdss/core_plugins/dummy/ui_notification/tests/test_ui_notification.py deleted file mode 100644 index 356c5a039b69f6ed8f4a3f7fac2a3a5abfb9983d..0000000000000000000000000000000000000000 --- a/force_bdss/core_plugins/dummy/ui_notification/tests/test_ui_notification.py +++ /dev/null @@ -1,163 +0,0 @@ -import unittest -from testfixtures import LogCapture - -from force_bdss.core_plugins.dummy.ui_notification.ui_notification import \ - UINotification -from force_bdss.core_plugins.dummy.ui_notification.ui_notification_factory \ - import \ - UINotificationFactory -from force_bdss.core_plugins.dummy.ui_notification.ui_notification_model \ - import \ - UINotificationModel -from force_bdss.mco.events import MCOStartEvent, MCOProgressEvent, \ - MCOFinishEvent - -try: - import mock -except ImportError: - from unittest import mock - -import zmq - - -class TestUINotification(unittest.TestCase): - def setUp(self): - factory = mock.Mock(spec=UINotificationFactory) - self.model = UINotificationModel(factory) - self.model.identifier = "an_id" - - listener = UINotification(factory) - self.sync_socket = mock.Mock(spec=zmq.Socket) - self.sync_socket.recv_string = mock.Mock() - self.sync_socket.recv_string.side_effect = [ - "HELLO\nan_id\n1", - "GOODBYE\nan_id\n1" - ] - - self.pub_socket = mock.Mock(spec=zmq.Socket) - self.context = mock.Mock(spec=zmq.Context) - self.context.socket.side_effect = [self.pub_socket, - self.sync_socket] - listener.__class__._create_context = mock.Mock( - return_value=self.context) - - self.listener = listener - - def test_deliver(self): - listener = self.listener - listener.initialize(self.model) - self.assertEqual( - self.sync_socket.send_string.call_args[0][0], - 'HELLO\nan_id\n1') - - listener.deliver(MCOStartEvent()) - self.assertEqual( - self.pub_socket.send_string.call_args[0][0], - 'EVENT\nan_id\nMCO_START') - - listener.deliver(MCOProgressEvent(input=(1, 2, 3), output=(4, 5))) - self.assertEqual( - self.pub_socket.send_string.call_args[0][0], - 'EVENT\nan_id\nMCO_PROGRESS\n1 2 3\n4 5') - - listener.deliver(MCOFinishEvent()) - self.assertEqual( - self.pub_socket.send_string.call_args[0][0], - 'EVENT\nan_id\nMCO_FINISH') - - def test_finalize(self): - listener = self.listener - listener.initialize(self.model) - listener.finalize() - self.assertTrue(self.context.term.called) - self.assertTrue(self.sync_socket.close.called) - self.assertTrue(self.pub_socket.close.called) - self.assertIsNone(listener._context) - self.assertIsNone(listener._sync_socket) - self.assertIsNone(listener._pub_socket) - - def test_initialize(self): - listener = self.listener - listener.initialize(self.model) - self.assertEqual( - self.sync_socket.send_string.call_args[0][0], - 'HELLO\nan_id\n1') - - def test_polling(self): - self.sync_socket.poll.return_value = 0 - listener = self.listener - with LogCapture() as capture: - listener.initialize(self.model) - capture.check( - ("force_bdss.core_plugins.dummy.ui_notification.ui_notification", # noqa - "INFO", - "Could not connect to UI server after 1000 ms. Continuing without UI notification." # noqa - ), - ) - - self.assertIsNone(listener._context) - - def test_wrong_init_recv_string(self): - listener = self.listener - - self.sync_socket.recv_string.side_effect = [ - "HELLO\nnot_the_right_id\n1", - "GOODBYE\nan_id\n1" - ] - - with LogCapture() as capture: - listener.initialize(self.model) - capture.check( - ("force_bdss.core_plugins.dummy.ui_notification.ui_notification", # noqa - "ERROR", - "Unexpected reply in sync negotiation with UI server. " - "'HELLO\nnot_the_right_id\n1'" # noqa - ), - ) - - self.assertIsNone(listener._context) - - def test_deliver_without_context(self): - self.listener.deliver(MCOStartEvent()) - self.assertFalse(self.pub_socket.send_string.called) - - def test_finalize_without_context(self): - self.listener.finalize() - self.assertFalse(self.sync_socket.send_string.called) - - def test_finalize_no_response(self): - self.sync_socket.poll.side_effect = [1, 0] - listener = self.listener - listener.initialize(self.model) - with LogCapture() as capture: - listener.finalize() - capture.check( - ("force_bdss.core_plugins.dummy.ui_notification.ui_notification", # noqa - "INFO", - "Could not close connection to UI server after 1000 ms." # noqa - ), - ) - - self.assertIsNone(listener._context) - - def test_wrong_finalize_recv_string(self): - listener = self.listener - self.sync_socket.poll.side_effect = [1, 1] - self.sync_socket.recv_string.side_effect = [ - "HELLO\nan_id\n1", - "GOODBYE\nnot_the_right_id\n1" - ] - - listener.initialize(self.model) - - with LogCapture() as capture: - listener.finalize() - capture.check( - ("force_bdss.core_plugins.dummy.ui_notification.ui_notification", # noqa - "ERROR", - "Unexpected reply in goodbye sync negotiation with UI server. " # noqa - "'GOODBYE\nnot_the_right_id\n1'" # noqa - ), - ) - - self.assertIsNone(listener._context) diff --git a/force_bdss/core_plugins/dummy/ui_notification/tests/test_ui_notification_factory.py b/force_bdss/core_plugins/dummy/ui_notification/tests/test_ui_notification_factory.py deleted file mode 100644 index b56c5c95f14fcadd4fffe273ce746533be37b09c..0000000000000000000000000000000000000000 --- a/force_bdss/core_plugins/dummy/ui_notification/tests/test_ui_notification_factory.py +++ /dev/null @@ -1,40 +0,0 @@ -import unittest - -from envisage.plugin import Plugin - -from force_bdss.core_plugins.dummy.ui_notification.ui_notification import \ - UINotification -from force_bdss.core_plugins.dummy.ui_notification.ui_notification_factory \ - import \ - UINotificationFactory -from force_bdss.core_plugins.dummy.ui_notification.ui_notification_model \ - import \ - UINotificationModel - -try: - import mock -except ImportError: - from unittest import mock - - -class TestUINotificationFactory(unittest.TestCase): - def test_initialization(self): - factory = UINotificationFactory(mock.Mock(spec=Plugin)) - self.assertEqual( - factory.id, - "force.bdss.enthought.factory.ui_notification") - - def test_create_model(self): - factory = UINotificationFactory(mock.Mock(spec=Plugin)) - model = factory.create_model() - self.assertIsInstance(model, UINotificationModel) - self.assertEqual(model.factory, factory) - - model = factory.create_model({}) - self.assertIsInstance(model, UINotificationModel) - self.assertEqual(model.factory, factory) - - def test_create_listener(self): - factory = UINotificationFactory(mock.Mock(spec=Plugin)) - listener = factory.create_listener() - self.assertIsInstance(listener, UINotification) diff --git a/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py b/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py deleted file mode 100644 index f3e644c0b7ca925666632e1b23eabaf927d23475..0000000000000000000000000000000000000000 --- a/force_bdss/core_plugins/dummy/ui_notification/ui_notification.py +++ /dev/null @@ -1,127 +0,0 @@ -import logging -from traits.api import Instance, String - -from force_bdss.api import ( - BaseNotificationListener, - MCOStartEvent, - MCOFinishEvent, - MCOProgressEvent -) - -import zmq - -log = logging.getLogger(__name__) - - -class UINotification(BaseNotificationListener): - """ - Notification engine for the UI. Uses zeromq for the traffic handling. - """ - #: The ZMQ context. If None, it means that the service is unavailable. - _context = Instance(zmq.Context) - - #: The pubsub socket. - _pub_socket = Instance(zmq.Socket) - - #: The synchronization socket to communicate with the server (UI) - _sync_socket = Instance(zmq.Socket) - - #: Unique identifier from the UI. To be returned in the protocol. - _identifier = String() - - #: The protocol version that this plugin delivers - _proto_version = "1" - - def initialize(self, model): - self._identifier = model.identifier - self._context = self._create_context() - - self._pub_socket = self._context.socket(zmq.PUB) - self._pub_socket.setsockopt(zmq.LINGER, 0) - self._pub_socket.connect(model.pub_url) - - self._sync_socket = self._context.socket(zmq.REQ) - self._sync_socket.setsockopt(zmq.LINGER, 0) - self._sync_socket.connect(model.sync_url) - - msg = "HELLO\n{}\n{}".format(self._identifier, self._proto_version) - self._sync_socket.send_string(msg) - events = self._sync_socket.poll(1000, zmq.POLLIN) - - if events == 0: - log.info("Could not connect to UI server after 1000 ms. " - "Continuing without UI notification.") - self._close_and_clear_sockets() - return - - recv = self._sync_socket.recv_string() - - if recv != msg: - log.error( - ("Unexpected reply in sync" - " negotiation with UI server. '{}'".format(recv))) - self._close_and_clear_sockets() - return - - def deliver(self, event): - if not self._context: - return - - msg = _format_event(event, self._identifier) - if msg is not None: - self._pub_socket.send_string(msg) - - def finalize(self): - if not self._context: - return - - msg = "GOODBYE\n{}\n{}".format(self._identifier, self._proto_version) - self._sync_socket.send_string(msg) - events = self._sync_socket.poll(1000, zmq.POLLIN) - if events == 0: - log.info("Could not close connection to UI server after " - "1000 ms.") - self._close_and_clear_sockets() - return - - recv = self._sync_socket.recv_string() - - if recv != msg: - log.error( - ("Unexpected reply in goodbye sync" - " negotiation with UI server. '{}'".format(recv))) - - self._close_and_clear_sockets() - - def _close_and_clear_sockets(self): - if self._pub_socket: - self._pub_socket.close() - - if self._sync_socket: - self._sync_socket.close() - - if self._context: - self._context.term() - - self._pub_socket = None - self._sync_socket = None - self._context = None - - def _create_context(self): - return zmq.Context() - - -def _format_event(event, identifier): - """Converts the event into a byte sequence to be transferred via zmq""" - if isinstance(event, MCOStartEvent): - data = "MCO_START" - elif isinstance(event, MCOFinishEvent): - data = "MCO_FINISH" - elif isinstance(event, MCOProgressEvent): - data = "MCO_PROGRESS\n{}\n{}".format( - " ".join([str(x) for x in event.input]), - " ".join([str(x) for x in event.output])) - else: - return None - - return "EVENT\n{}\n{}".format(identifier, data) diff --git a/force_bdss/core_plugins/dummy/ui_notification/ui_notification_factory.py b/force_bdss/core_plugins/dummy/ui_notification/ui_notification_factory.py deleted file mode 100644 index 39cee9b53bd08a828ef1b92d978954c6d6b56fc0..0000000000000000000000000000000000000000 --- a/force_bdss/core_plugins/dummy/ui_notification/ui_notification_factory.py +++ /dev/null @@ -1,21 +0,0 @@ -from traits.api import String - -from force_bdss.api import factory_id, BaseNotificationListenerFactory - -from .ui_notification import UINotification -from .ui_notification_model import UINotificationModel - - -class UINotificationFactory(BaseNotificationListenerFactory): - id = String(factory_id("enthought", "ui_notification")) - - name = String("UI Notification") - - def create_model(self, model_data=None): - if model_data is None: - model_data = {} - - return UINotificationModel(self, **model_data) - - def create_listener(self): - return UINotification(self) diff --git a/force_bdss/core_plugins/dummy/ui_notification/ui_notification_model.py b/force_bdss/core_plugins/dummy/ui_notification/ui_notification_model.py deleted file mode 100644 index 7737cfc95fae26c01f85e04dcf2f15967f1e1efb..0000000000000000000000000000000000000000 --- a/force_bdss/core_plugins/dummy/ui_notification/ui_notification_model.py +++ /dev/null @@ -1,14 +0,0 @@ -from traits.api import String -from force_bdss.api import ( - BaseNotificationListenerModel, ZMQSocketURL) - - -class UINotificationModel(BaseNotificationListenerModel): - #: The socket URL where the UI will be found. Synchronization port. - sync_url = ZMQSocketURL() - - #: The socket URL where the UI will be found. PubSub port. - pub_url = ZMQSocketURL() - - #: Unique identifier assigned by the UI to recognize the connection. - identifier = String() diff --git a/force_bdss/tests/fixtures/test_csv.json b/force_bdss/tests/fixtures/test_csv.json index d9d6e35bbb644d659031530caca14db4f7af3a24..692ff7981d7c8ed1a5ed2b81e66a5f847e341f95 100644 --- a/force_bdss/tests/fixtures/test_csv.json +++ b/force_bdss/tests/fixtures/test_csv.json @@ -72,14 +72,6 @@ } ], "notification_listeners": [ - { - "id": "force.bdss.enthought.factory.ui_notification", - "model_data": { - "sync_url": "tcp://127.0.0.1:12346", - "pub_url": "tcp://127.0.0.1:12345", - "identifier": "vfdjlkadfkljfsd" - } - } ] } } diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 7f6b07c7b8c7244591f43a88cfedfd6c80dbda3c..cfc27054ea4f3f6acf0be5267e8bf9a3809d9881 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -2,4 +2,3 @@ envisage==4.6.0 click==6.7 six==1.10.0 stevedore==1.24.0 -pyzmq==16.0.2 diff --git a/setup.py b/setup.py index d42c8199eff7ba7d95cf8c243196dcedb0533096..98309fea96f60e657927d47f03738ff2d052db01 100644 --- a/setup.py +++ b/setup.py @@ -38,6 +38,5 @@ setup( "click >= 6.7", "stevedore >= 1.24.0", "six >= 1.10.0", - "pyzmq >= 16.0.0" ] )