diff --git a/lib/tao/config/__init__.py b/lib/tao/config/__init__.py index 9e0efbb..4876c01 100644 --- a/lib/tao/config/__init__.py +++ b/lib/tao/config/__init__.py @@ -1,4 +1,4 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from .envvars import * # pylint: disable=wildcard-import +from .envvars import TAOENV diff --git a/lib/tfw/__init__.py b/lib/tfw/__init__.py index 1179607..ab6c8a6 100644 --- a/lib/tfw/__init__.py +++ b/lib/tfw/__init__.py @@ -1,3 +1,5 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. +from .event_handler_base import TriggerlessEventHandler, TriggeredEventHandler +from .fsm_base import FSMBase diff --git a/lib/tfw/components/__init__.py b/lib/tfw/components/__init__.py new file mode 100644 index 0000000..661a777 --- /dev/null +++ b/lib/tfw/components/__init__.py @@ -0,0 +1,8 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from .directory_monitoring_event_handler import DirectoryMonitoringEventHandler +from .process_managing_event_handler import ProcessManagingEventHandler +from .terminado_event_handler import TerminadoEventHandler +from .source_code_event_handler import SourceCodeEventHandler +from .history_monitor import HistoryMonitor, BashMonitor, GDBMonitor diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index ccec00f..9726eb9 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -6,7 +6,7 @@ from functools import wraps from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector +from tfw.networking.event_handlers import ServerUplinkConnector from tfw.components.decorators import RateLimiter from tfw.config.logs import logging diff --git a/lib/tfw/components/directory_monitoring_event_handler.py b/lib/tfw/components/directory_monitoring_event_handler.py index 1f29212..ad7ea69 100644 --- a/lib/tfw/components/directory_monitoring_event_handler.py +++ b/lib/tfw/components/directory_monitoring_event_handler.py @@ -3,9 +3,9 @@ from os.path import isdir, exists -from tfw.components.directory_monitor import DirectoryMonitor -from tfw.event_handler_base import TriggerlessEventHandler +from tfw import TriggerlessEventHandler from tfw.config.logs import logging +from .directory_monitor import DirectoryMonitor LOG = logging.getLogger(__name__) diff --git a/lib/tfw/components/history_monitor.py b/lib/tfw/components/history_monitor.py index 690ec7b..1665ad9 100644 --- a/lib/tfw/components/history_monitor.py +++ b/lib/tfw/components/history_monitor.py @@ -9,8 +9,8 @@ from abc import ABC, abstractmethod from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler -from tfw.components.mixins.callback_mixin import CallbackMixin -from tfw.components.decorators.rate_limiter import RateLimiter +from tfw.components.mixins import CallbackMixin +from tfw.components.decorators import RateLimiter class CallbackEventHandler(PatternMatchingEventHandler, ABC): diff --git a/lib/tfw/components/mixins/__init__.py b/lib/tfw/components/mixins/__init__.py index 777f20e..8003c33 100644 --- a/lib/tfw/components/mixins/__init__.py +++ b/lib/tfw/components/mixins/__init__.py @@ -2,3 +2,4 @@ # All Rights Reserved. See LICENSE file for details. from .supervisor_mixin import SupervisorMixin +from .callback_mixin import CallbackMixin diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 8ccac85..f069c67 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -3,10 +3,10 @@ from xmlrpc.client import Fault as SupervisorFault -from tfw.event_handler_base import TriggerlessEventHandler +from tfw import TriggerlessEventHandler from tfw.components.mixins import SupervisorMixin -from tfw.components.directory_monitor import with_monitor_paused from tfw.config.logs import logging +from .directory_monitor import with_monitor_paused LOG = logging.getLogger(__name__) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index fbaffd8..2c8657d 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -6,9 +6,9 @@ from glob import glob from fnmatch import fnmatchcase from collections import Iterable -from tfw.event_handler_base import TriggerlessEventHandler -from tfw.components.directory_monitor import DirectoryMonitor +from tfw import TriggerlessEventHandler from tfw.config.logs import logging +from .directory_monitor import DirectoryMonitor LOG = logging.getLogger(__name__) @@ -154,8 +154,8 @@ class SourceCodeEventHandler(TriggerlessEventHandler): self.read(data) except IndexError: data['content'] = 'No files in this directory :(' - except EnvironmentError: - LOG.error('Failed to select directory "%s"', data['directory']) + except EnvironmentError as err: + LOG.error('Failed to select directory "%s". Reason: %s', data['directory'], str(err)) return data def exclude(self, data): diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 76621a4..58ccd4b 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -1,11 +1,11 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from tfw.components.terminado_mini_server import TerminadoMiniServer -from tfw.event_handler_base import TriggerlessEventHandler +from tfw import TriggerlessEventHandler from tfw.config import TFWENV from tfw.config.logs import logging from tao.config import TAOENV +from .terminado_mini_server import TerminadoMiniServer LOG = logging.getLogger(__name__) diff --git a/lib/tfw/config/__init__.py b/lib/tfw/config/__init__.py index 215a5fa..3614ad9 100644 --- a/lib/tfw/config/__init__.py +++ b/lib/tfw/config/__init__.py @@ -1,4 +1,4 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from .envvars import * +from .envvars import TFWENV diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base.py index d5e3fc2..48eec2d 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base.py @@ -3,8 +3,8 @@ from abc import ABC, abstractmethod -from tfw.networking.serialization import deserialize_all -from tfw.networking.event_handlers.server_connector import ServerConnector +from tfw.networking import deserialize_all +from tfw.networking.event_handlers import ServerConnector class EventHandlerBase(ABC): diff --git a/lib/tfw/fsm_base.py b/lib/tfw/fsm_base.py index 412adb9..545c0b6 100644 --- a/lib/tfw/fsm_base.py +++ b/lib/tfw/fsm_base.py @@ -5,7 +5,7 @@ from typing import List from transitions import Machine -from tfw.components.mixins.callback_mixin import CallbackMixin +from tfw.components.mixins import CallbackMixin class FSMBase(CallbackMixin): diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index 1179607..161659b 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -1,3 +1,9 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. +from .serialization import decode_if_needed, encode_if_needed, serialize_all, deserialize_all +from .zmq_connector_base import ZMQConnectorBase +from .controller_connector import ControllerConnector +from .message_sender import MessageSender +from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector +from .server.tfw_server import TFWServer diff --git a/lib/tfw/networking/controller_connector.py b/lib/tfw/networking/controller_connector.py index 427eefc..708c46d 100644 --- a/lib/tfw/networking/controller_connector.py +++ b/lib/tfw/networking/controller_connector.py @@ -5,7 +5,7 @@ import zmq from zmq.eventloop.zmqstream import ZMQStream from tfw.config import TFWENV -from tfw.networking.zmq_connector_base import ZMQConnectorBase +from tfw.networking import ZMQConnectorBase class ControllerConnector(ZMQConnectorBase): diff --git a/lib/tfw/networking/event_handlers/__init__.py b/lib/tfw/networking/event_handlers/__init__.py index 1179607..b3ad530 100644 --- a/lib/tfw/networking/event_handlers/__init__.py +++ b/lib/tfw/networking/event_handlers/__init__.py @@ -1,3 +1,4 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. +from .server_connector import ServerConnector, ServerUplinkConnector, ServerDownlinkConnector diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/event_handlers/server_connector.py index f202af6..4479e75 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -6,9 +6,9 @@ from functools import partial import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking.serialization import serialize_all +from tfw.networking import serialize_all +from tfw.networking import ZMQConnectorBase from tfw.config import TFWENV -from tfw.networking.zmq_connector_base import ZMQConnectorBase class ServerDownlinkConnector(ZMQConnectorBase): diff --git a/lib/tfw/message_sender.py b/lib/tfw/networking/message_sender.py similarity index 87% rename from lib/tfw/message_sender.py rename to lib/tfw/networking/message_sender.py index 18a985d..ba08daf 100644 --- a/lib/tfw/message_sender.py +++ b/lib/tfw/networking/message_sender.py @@ -3,7 +3,7 @@ from datetime import datetime -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector +from tfw.networking.event_handlers import ServerUplinkConnector class MessageSender: diff --git a/lib/tfw/networking/server/__init__.py b/lib/tfw/networking/server/__init__.py index 1179607..e4c1b25 100644 --- a/lib/tfw/networking/server/__init__.py +++ b/lib/tfw/networking/server/__init__.py @@ -1,3 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. +from .event_handler_connector import EventHandlerConnector, EventHandlerUplinkConnector, EventHandlerDownlinkConnector +from .tfw_server import TFWServer +from .zmq_websocket_handler import ZMQWebSocketProxy +from .controller_responder import ControllerResponder diff --git a/lib/tfw/networking/server/controller_responder.py b/lib/tfw/networking/server/controller_responder.py index a745bcb..02d442f 100644 --- a/lib/tfw/networking/server/controller_responder.py +++ b/lib/tfw/networking/server/controller_responder.py @@ -1,8 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from tfw.networking.controller_connector import ControllerConnector -from tfw.networking.serialization import deserialize_all, serialize_all +from tfw.networking import deserialize_all, serialize_all, ControllerConnector class ControllerResponder: diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/server/event_handler_connector.py index f0a2b55..d8f3801 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/server/event_handler_connector.py @@ -4,8 +4,7 @@ import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking.zmq_connector_base import ZMQConnectorBase -from tfw.networking.serialization import serialize_all +from tfw.networking import ZMQConnectorBase, serialize_all from tfw.config import TFWENV from tfw.config.logs import logging diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index 7df8c1b..02168f5 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -5,10 +5,11 @@ from collections import defaultdict from tornado.web import Application -from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector -from tfw.message_sender import MessageSender +from tfw.networking import MessageSender +from tfw.networking.event_handlers import ServerUplinkConnector +from tfw.networking.server import EventHandlerConnector from tfw.config.logs import logging +from .zmq_websocket_handler import ZMQWebSocketProxy LOG = logging.getLogger(__name__) @@ -19,10 +20,12 @@ class TFWServer: self._fsm_updater = FSMUpdater(self._fsm) self._fsm_manager = FSMManager(self._fsm) self._fsm.subscribe_callback(self._fsm_updater.update) + self._event_handler_connector = EventHandlerConnector() self.application = Application( [(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response, - 'proxy_filter': self.proxy_filter})] + 'proxy_filter': self.proxy_filter, + 'event_handler_connector': self._event_handler_connector})] ) #self.controller_responder = ControllerResponder(self.fsm) TODO: add this once controller stuff is resolved diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index 2f21249..c0c64bb 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -6,17 +6,18 @@ from abc import ABC, abstractmethod from tornado.websocket import WebSocketHandler -from tfw.networking.serialization import deserialize_all -from tfw.networking.server.event_handler_connector import EventHandlerConnector +from tfw.networking import deserialize_all from tfw.config.logs import logging LOG = logging.getLogger(__name__) class ZMQWebSocketHandler(WebSocketHandler, ABC): - _event_handler_connector = EventHandlerConnector() instances = set() + def initialize(self, **kwargs): + self._event_handler_connector = kwargs['event_handler_connector'] + def prepare(self): ZMQWebSocketHandler.instances.add(self) @@ -27,14 +28,13 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): LOG.debug('WebSocket connection initiated') self._event_handler_connector.register_callback(self.zmq_callback) - @classmethod - def zmq_callback(cls, msg_parts): - keyhandlers = {'mirror': cls.mirror} + def zmq_callback(self, msg_parts): + keyhandlers = {'mirror': self.mirror} key, data = deserialize_all(*msg_parts) LOG.debug('Received on pull socket: %s', data) if key not in keyhandlers: - for instance in cls.instances: + for instance in ZMQWebSocketHandler.instances: instance.write_message(data) else: try: @@ -42,10 +42,9 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): except KeyError: LOG.error('Invalid mirror message format! Ignoring.') - @classmethod - def mirror(cls, data): + def mirror(self, data): key = data['key'] - cls._event_handler_connector.send_message({'data': data}, key) + self._event_handler_connector.send_message({'data': data}, key) def on_message(self, message): LOG.debug('Received on WebSocket: %s', message) @@ -65,10 +64,11 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC): class ZMQWebSocketProxy(ZMQWebSocketHandler): # pylint: disable=abstract-method - def initialize(self, make_response, proxy_filter): + def initialize(self, **kwargs): # pylint: disable=arguments-differ - self._make_response = make_response - self._proxy_filter = proxy_filter + super(ZMQWebSocketProxy, self).initialize(**kwargs) + self._make_response = kwargs['make_response'] + self._proxy_filter = kwargs['proxy_filter'] def on_message(self, message): message = json.loads(message)