From 2134d743c38e7ef4414b96b843be701fe71d8727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 27 May 2019 14:09:13 +0200 Subject: [PATCH] Improve TFW lib layout --- lib/tfw/components/directory_monitor.py | 2 +- .../directory_monitoring_event_handler.py | 2 +- .../directory_snapshotting_event_handler.py | 2 +- lib/tfw/components/frontend_event_handler.py | 2 +- lib/tfw/components/fsm_managing_event_handler.py | 4 ++-- lib/tfw/components/ide_event_handler.py | 2 +- lib/tfw/components/log_monitor.py | 2 +- lib/tfw/components/log_monitoring_event_handler.py | 2 +- lib/tfw/components/pipe_io_event_handler.py | 2 +- .../components/process_managing_event_handler.py | 2 +- lib/tfw/components/terminal_event_handler.py | 3 +-- lib/tfw/crypto.py | 2 +- .../__init__.py | 3 ++- .../boradcasting_event_handler.py | 5 +++-- .../event_handler_base.py | 7 +------ .../event_handlers/frontend_event_handler_base.py | 8 ++++++++ lib/tfw/{networking => event_handlers}/fsm_aware.py | 1 - .../fsm_aware_event_handler.py | 4 ++-- lib/tfw/networking/__init__.py | 6 ++++-- .../{server => }/event_handler_connector.py | 5 +++-- lib/tfw/networking/event_handlers/__init__.py | 2 -- lib/tfw/networking/message_sender.py | 2 +- lib/tfw/networking/scope.py | 7 +++++++ lib/tfw/networking/server/__init__.py | 2 -- .../{event_handlers => }/server_connector.py | 13 ++++--------- lib/tfw/server/__init__.py | 1 + lib/tfw/{networking => }/server/tfw_server.py | 5 +++-- .../{networking => }/server/zmq_websocket_proxy.py | 2 +- supervisor/tfw_server.py | 2 +- 29 files changed, 54 insertions(+), 48 deletions(-) rename lib/tfw/{event_handler_base => event_handlers}/__init__.py (67%) rename lib/tfw/{event_handler_base => event_handlers}/boradcasting_event_handler.py (87%) rename lib/tfw/{event_handler_base => event_handlers}/event_handler_base.py (93%) create mode 100644 lib/tfw/event_handlers/frontend_event_handler_base.py rename lib/tfw/{networking => event_handlers}/fsm_aware.py (99%) rename lib/tfw/{event_handler_base => event_handlers}/fsm_aware_event_handler.py (84%) rename lib/tfw/networking/{server => }/event_handler_connector.py (92%) delete mode 100644 lib/tfw/networking/event_handlers/__init__.py create mode 100644 lib/tfw/networking/scope.py delete mode 100644 lib/tfw/networking/server/__init__.py rename lib/tfw/networking/{event_handlers => }/server_connector.py (87%) create mode 100644 lib/tfw/server/__init__.py rename lib/tfw/{networking => }/server/tfw_server.py (83%) rename lib/tfw/{networking => }/server/zmq_websocket_proxy.py (97%) diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index 6aa2ca0..f4fb699 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -5,7 +5,7 @@ from functools import wraps from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector, Scope +from tfw.networking import ServerUplinkConnector, Scope from tfw.decorators.rate_limiter import RateLimiter from tfw.mixins.observer_mixin import ObserverMixin diff --git a/lib/tfw/components/directory_monitoring_event_handler.py b/lib/tfw/components/directory_monitoring_event_handler.py index 03c4a3a..18bb8ca 100644 --- a/lib/tfw/components/directory_monitoring_event_handler.py +++ b/lib/tfw/components/directory_monitoring_event_handler.py @@ -3,7 +3,7 @@ from os.path import isdir, exists -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin from tfw.components.directory_monitor import DirectoryMonitor from tfw.config.logs import logging diff --git a/lib/tfw/components/directory_snapshotting_event_handler.py b/lib/tfw/components/directory_snapshotting_event_handler.py index 6a285a8..d60732f 100644 --- a/lib/tfw/components/directory_snapshotting_event_handler.py +++ b/lib/tfw/components/directory_snapshotting_event_handler.py @@ -8,7 +8,7 @@ from datetime import datetime from dateutil import parser as dateparser -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.components.snapshot_provider import SnapshotProvider from tfw.config import TFWENV from tfw.config.logs import logging diff --git a/lib/tfw/components/frontend_event_handler.py b/lib/tfw/components/frontend_event_handler.py index 898bb7e..73028ba 100644 --- a/lib/tfw/components/frontend_event_handler.py +++ b/lib/tfw/components/frontend_event_handler.py @@ -2,7 +2,7 @@ from abc import ABC, abstractmethod from contextlib import suppress from tfw.networking.message_sender import MessageSender -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase class FrontendEventHandler(FrontendEventHandlerBase): diff --git a/lib/tfw/components/fsm_managing_event_handler.py b/lib/tfw/components/fsm_managing_event_handler.py index 3069fc1..ac6e6fc 100644 --- a/lib/tfw/components/fsm_managing_event_handler.py +++ b/lib/tfw/components/fsm_managing_event_handler.py @@ -1,10 +1,10 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.crypto import KeyManager, sign_message, verify_message from tfw.config.logs import logging -from tfw.networking.event_handlers.server_connector import Scope +from tfw.networking import Scope LOG = logging.getLogger(__name__) diff --git a/lib/tfw/components/ide_event_handler.py b/lib/tfw/components/ide_event_handler.py index 534de6a..2632b45 100644 --- a/lib/tfw/components/ide_event_handler.py +++ b/lib/tfw/components/ide_event_handler.py @@ -6,7 +6,7 @@ from glob import glob from fnmatch import fnmatchcase from typing import Iterable -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin from tfw.components.directory_monitor import DirectoryMonitor from tfw.config.logs import logging diff --git a/lib/tfw/components/log_monitor.py b/lib/tfw/components/log_monitor.py index 2ee6d96..b8a0b49 100644 --- a/lib/tfw/components/log_monitor.py +++ b/lib/tfw/components/log_monitor.py @@ -6,7 +6,7 @@ from os.path import dirname from watchdog.events import PatternMatchingEventHandler as PatternMatchingWatchdogEventHandler -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector, Scope +from tfw.networking import ServerUplinkConnector, Scope from tfw.decorators.rate_limiter import RateLimiter from tfw.mixins.observer_mixin import ObserverMixin from tfw.mixins.supervisor_mixin import SupervisorLogMixin diff --git a/lib/tfw/components/log_monitoring_event_handler.py b/lib/tfw/components/log_monitoring_event_handler.py index d384323..23fe5b9 100644 --- a/lib/tfw/components/log_monitoring_event_handler.py +++ b/lib/tfw/components/log_monitoring_event_handler.py @@ -1,7 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin from tfw.components.log_monitor import LogMonitor from tfw.config.logs import logging diff --git a/lib/tfw/components/pipe_io_event_handler.py b/lib/tfw/components/pipe_io_event_handler.py index 37b5b89..e35455e 100644 --- a/lib/tfw/components/pipe_io_event_handler.py +++ b/lib/tfw/components/pipe_io_event_handler.py @@ -9,7 +9,7 @@ from secrets import token_urlsafe from threading import Thread from contextlib import suppress -from tfw.event_handler_base import EventHandlerBase +from tfw.event_handlers import EventHandlerBase from tfw.config.logs import logging from .pipe_io_server import PipeIOServer, terminate_process_on_failure diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index a87bdd6..f6489e6 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -3,7 +3,7 @@ from xmlrpc.client import Fault as SupervisorFault -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.mixins.supervisor_mixin import SupervisorMixin, SupervisorLogMixin from tfw.components.directory_monitor import with_monitor_paused from tfw.config.logs import logging diff --git a/lib/tfw/components/terminal_event_handler.py b/lib/tfw/components/terminal_event_handler.py index f45bc8a..3427c4f 100644 --- a/lib/tfw/components/terminal_event_handler.py +++ b/lib/tfw/components/terminal_event_handler.py @@ -1,7 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from tfw.event_handler_base import FrontendEventHandlerBase +from tfw.event_handlers import FrontendEventHandlerBase from tfw.components.terminado_mini_server import TerminadoMiniServer from tfw.config import TFWENV from tfw.config.logs import logging @@ -50,7 +50,6 @@ class TerminalEventHandler(FrontendEventHandlerBase): return self._historymonitor def handle_event(self, message): - LOG.debug('TerminadoEventHandler received event: %s', message) try: data = message['data'] message['data'] = self.commands[data['command']](data) diff --git a/lib/tfw/crypto.py b/lib/tfw/crypto.py index 0b37893..344abee 100644 --- a/lib/tfw/crypto.py +++ b/lib/tfw/crypto.py @@ -14,7 +14,7 @@ from cryptography.hazmat.primitives.hashes import SHA256 from cryptography.hazmat.primitives.hmac import HMAC as _HMAC from cryptography.exceptions import InvalidSignature -from tfw.networking.serialization import message_bytes +from tfw.networking import message_bytes from tfw.decorators.lazy_property import lazy_property from tfw.config import TFWENV diff --git a/lib/tfw/event_handler_base/__init__.py b/lib/tfw/event_handlers/__init__.py similarity index 67% rename from lib/tfw/event_handler_base/__init__.py rename to lib/tfw/event_handlers/__init__.py index 65ca6e1..858b781 100644 --- a/lib/tfw/event_handler_base/__init__.py +++ b/lib/tfw/event_handlers/__init__.py @@ -1,6 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from .event_handler_base import EventHandlerBase, FrontendEventHandlerBase +from .event_handler_base import EventHandlerBase +from .frontend_event_handler_base import FrontendEventHandlerBase from .boradcasting_event_handler import BroadcastingEventHandler from .fsm_aware_event_handler import FSMAwareEventHandler diff --git a/lib/tfw/event_handler_base/boradcasting_event_handler.py b/lib/tfw/event_handlers/boradcasting_event_handler.py similarity index 87% rename from lib/tfw/event_handler_base/boradcasting_event_handler.py rename to lib/tfw/event_handlers/boradcasting_event_handler.py index cfda694..c6f61a9 100644 --- a/lib/tfw/event_handler_base/boradcasting_event_handler.py +++ b/lib/tfw/event_handlers/boradcasting_event_handler.py @@ -3,10 +3,11 @@ from abc import ABC -from tfw.event_handler_base.event_handler_base import EventHandlerBase -from tfw.networking.event_handlers.server_connector import Scope +from tfw.networking import Scope from tfw.crypto import message_checksum +from .event_handler_base import EventHandlerBase + class BroadcastingEventHandler(EventHandlerBase, ABC): # pylint: disable=abstract-method diff --git a/lib/tfw/event_handler_base/event_handler_base.py b/lib/tfw/event_handlers/event_handler_base.py similarity index 93% rename from lib/tfw/event_handler_base/event_handler_base.py rename to lib/tfw/event_handlers/event_handler_base.py index f51c668..bb3155f 100644 --- a/lib/tfw/event_handler_base/event_handler_base.py +++ b/lib/tfw/event_handlers/event_handler_base.py @@ -5,7 +5,7 @@ from abc import ABC, abstractmethod from inspect import currentframe from typing import Iterable -from tfw.networking.event_handlers.server_connector import ServerConnector, Scope +from tfw.networking import ServerConnector from tfw.config.logs import logging LOG = logging.getLogger(__name__) @@ -131,8 +131,3 @@ class EventHandlerBase(ABC): instance for instance in locals_values if isinstance(instance, cls) } - - -class FrontendEventHandlerBase(EventHandlerBase): # pylint: disable=abstract-method - def send_message(self, message): - self.server_connector.send_message(message, Scope.WEBSOCKET) diff --git a/lib/tfw/event_handlers/frontend_event_handler_base.py b/lib/tfw/event_handlers/frontend_event_handler_base.py new file mode 100644 index 0000000..6990632 --- /dev/null +++ b/lib/tfw/event_handlers/frontend_event_handler_base.py @@ -0,0 +1,8 @@ +from tfw.networking import Scope + +from .event_handler_base import EventHandlerBase + + +class FrontendEventHandlerBase(EventHandlerBase): # pylint: disable=abstract-method + def send_message(self, message): + self.server_connector.send_message(message, Scope.WEBSOCKET) diff --git a/lib/tfw/networking/fsm_aware.py b/lib/tfw/event_handlers/fsm_aware.py similarity index 99% rename from lib/tfw/networking/fsm_aware.py rename to lib/tfw/event_handlers/fsm_aware.py index cb2b287..bec3633 100644 --- a/lib/tfw/networking/fsm_aware.py +++ b/lib/tfw/event_handlers/fsm_aware.py @@ -2,7 +2,6 @@ # All Rights Reserved. See LICENSE file for details. from tfw.crypto import KeyManager, verify_message - from tfw.config.logs import logging LOG = logging.getLogger(__name__) diff --git a/lib/tfw/event_handler_base/fsm_aware_event_handler.py b/lib/tfw/event_handlers/fsm_aware_event_handler.py similarity index 84% rename from lib/tfw/event_handler_base/fsm_aware_event_handler.py rename to lib/tfw/event_handlers/fsm_aware_event_handler.py index 01d6360..60313df 100644 --- a/lib/tfw/event_handler_base/fsm_aware_event_handler.py +++ b/lib/tfw/event_handlers/fsm_aware_event_handler.py @@ -3,8 +3,8 @@ from abc import ABC -from tfw.event_handler_base.event_handler_base import EventHandlerBase -from tfw.networking.fsm_aware import FSMAware +from .event_handler_base import EventHandlerBase +from .fsm_aware import FSMAware class FSMAwareEventHandler(EventHandlerBase, FSMAware, ABC): diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index 500dd7a..7772eb6 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -1,6 +1,8 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. +from .serialization import serialize_tfw_msg, deserialize_tfw_msg, with_deserialize_tfw_msg, message_bytes +from .server_connector import ServerUplinkConnector, ServerDownlinkConnector, ServerConnector +from .event_handler_connector import EventHandlerConnector from .message_sender import MessageSender -from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector -from .server.tfw_server import TFWServer +from .scope import Scope diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/event_handler_connector.py similarity index 92% rename from lib/tfw/networking/server/event_handler_connector.py rename to lib/tfw/networking/event_handler_connector.py index 7b56a88..67ca7ba 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/event_handler_connector.py @@ -4,11 +4,12 @@ import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking.zmq_connector_base import ZMQConnectorBase -from tfw.networking.serialization import serialize_tfw_msg, with_deserialize_tfw_msg from tfw.config import TFWENV from tfw.config.logs import logging +from .serialization import serialize_tfw_msg, with_deserialize_tfw_msg +from .zmq_connector_base import ZMQConnectorBase + LOG = logging.getLogger(__name__) diff --git a/lib/tfw/networking/event_handlers/__init__.py b/lib/tfw/networking/event_handlers/__init__.py deleted file mode 100644 index db64b25..0000000 --- a/lib/tfw/networking/event_handlers/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# Copyright (C) 2018 Avatao.com Innovative Learning Kft. -# All Rights Reserved. See LICENSE file for details. diff --git a/lib/tfw/networking/message_sender.py b/lib/tfw/networking/message_sender.py index 1232aaa..10dd462 100644 --- a/lib/tfw/networking/message_sender.py +++ b/lib/tfw/networking/message_sender.py @@ -1,7 +1,7 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from tfw.networking.event_handlers.server_connector import ServerUplinkConnector +from .server_connector import ServerUplinkConnector class MessageSender: diff --git a/lib/tfw/networking/scope.py b/lib/tfw/networking/scope.py new file mode 100644 index 0000000..639461b --- /dev/null +++ b/lib/tfw/networking/scope.py @@ -0,0 +1,7 @@ +from enum import Enum + + +class Scope(Enum): + ZMQ = 'zmq' + WEBSOCKET = 'websocket' + BROADCAST = 'broadcast' diff --git a/lib/tfw/networking/server/__init__.py b/lib/tfw/networking/server/__init__.py deleted file mode 100644 index db64b25..0000000 --- a/lib/tfw/networking/server/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# Copyright (C) 2018 Avatao.com Innovative Learning Kft. -# All Rights Reserved. See LICENSE file for details. diff --git a/lib/tfw/networking/event_handlers/server_connector.py b/lib/tfw/networking/server_connector.py similarity index 87% rename from lib/tfw/networking/event_handlers/server_connector.py rename to lib/tfw/networking/server_connector.py index 73b04e0..c0e2bf1 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/server_connector.py @@ -2,16 +2,17 @@ # All Rights Reserved. See LICENSE file for details. from functools import partial -from enum import Enum import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking.zmq_connector_base import ZMQConnectorBase -from tfw.networking.serialization import serialize_tfw_msg, with_deserialize_tfw_msg from tfw.config import TFWENV from tfw.config.logs import logging +from .scope import Scope +from .serialization import serialize_tfw_msg, with_deserialize_tfw_msg +from .zmq_connector_base import ZMQConnectorBase + LOG = logging.getLogger(__name__) @@ -34,12 +35,6 @@ class ServerDownlinkConnector(ZMQConnectorBase): self._zmq_sub_stream.close() -class Scope(Enum): - ZMQ = 'zmq' - WEBSOCKET = 'websocket' - BROADCAST = 'broadcast' - - class ServerUplinkConnector(ZMQConnectorBase): def __init__(self, zmq_context=None): super(ServerUplinkConnector, self).__init__(zmq_context) diff --git a/lib/tfw/server/__init__.py b/lib/tfw/server/__init__.py new file mode 100644 index 0000000..e2c01a9 --- /dev/null +++ b/lib/tfw/server/__init__.py @@ -0,0 +1 @@ +from .tfw_server import TFWServer diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/server/tfw_server.py similarity index 83% rename from lib/tfw/networking/server/tfw_server.py rename to lib/tfw/server/tfw_server.py index ed28e62..a4cdd3f 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/server/tfw_server.py @@ -3,10 +3,11 @@ from tornado.web import Application -from tfw.networking.server.zmq_websocket_proxy import ZMQWebSocketProxy -from tfw.networking.server.event_handler_connector import EventHandlerConnector +from tfw.networking import EventHandlerConnector from tfw.config.logs import logging +from .zmq_websocket_proxy import ZMQWebSocketProxy + LOG = logging.getLogger(__name__) diff --git a/lib/tfw/networking/server/zmq_websocket_proxy.py b/lib/tfw/server/zmq_websocket_proxy.py similarity index 97% rename from lib/tfw/networking/server/zmq_websocket_proxy.py rename to lib/tfw/server/zmq_websocket_proxy.py index b813dca..b820f85 100644 --- a/lib/tfw/networking/server/zmq_websocket_proxy.py +++ b/lib/tfw/server/zmq_websocket_proxy.py @@ -5,7 +5,7 @@ import json from tornado.websocket import WebSocketHandler -from tfw.networking.event_handlers.server_connector import Scope +from tfw.networking import Scope from tfw.config.logs import logging LOG = logging.getLogger(__name__) diff --git a/supervisor/tfw_server.py b/supervisor/tfw_server.py index 78766ac..4cb8bd6 100644 --- a/supervisor/tfw_server.py +++ b/supervisor/tfw_server.py @@ -1,6 +1,6 @@ from tornado.ioloop import IOLoop -from tfw.networking import TFWServer +from tfw.server import TFWServer from tfw.config import TFWENV