From 7a670f37f2011a87c9a7faf092e0b47ceb3b31d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 26 Jul 2018 13:59:06 +0200 Subject: [PATCH] Resolve Python circular import hell - hopefully forever --- lib/envvars/__init__.py | 2 +- lib/tfw/__init__.py | 3 -- lib/tfw/components/directory_monitor.py | 6 +-- .../directory_monitoring_event_handler.py | 6 +-- .../components/fsm_managing_event_handler.py | 2 +- lib/tfw/components/history_monitor.py | 5 ++- lib/tfw/components/ide_event_handler.py | 6 +-- lib/tfw/components/log_monitor.py | 7 +-- .../log_monitoring_event_handler.py | 6 +-- .../process_managing_event_handler.py | 6 +-- lib/tfw/components/terminal_event_handler.py | 4 +- lib/tfw/crypto.py | 4 +- lib/tfw/decorators/__init__.py | 3 -- lib/tfw/event_handler_base/__init__.py | 6 +++ .../boradcasting_event_handler.py | 30 +++++++++++++ .../event_handler_base.py | 44 +------------------ .../fsm_aware_event_handler.py | 24 ++++++++++ lib/tfw/fsm/fsm_base.py | 2 +- lib/tfw/fsm/linear_fsm.py | 2 +- lib/tfw/fsm/yaml_fsm.py | 2 +- lib/tfw/mixins/__init__.py | 5 --- lib/tfw/mixins/callback_mixin.py | 2 +- lib/tfw/mixins/observer_mixin.py | 2 +- lib/tfw/mixins/supervisor_mixin.py | 2 +- lib/tfw/networking/__init__.py | 4 -- lib/tfw/networking/event_handlers/__init__.py | 2 - .../event_handlers/server_connector.py | 4 +- lib/tfw/networking/message_sender.py | 2 +- lib/tfw/networking/server/__init__.py | 3 -- .../server/event_handler_connector.py | 3 +- lib/tfw/networking/server/tfw_server.py | 9 ++-- .../networking/server/zmq_websocket_proxy.py | 2 +- 32 files changed, 105 insertions(+), 105 deletions(-) create mode 100644 lib/tfw/event_handler_base/__init__.py create mode 100644 lib/tfw/event_handler_base/boradcasting_event_handler.py rename lib/tfw/{ => event_handler_base}/event_handler_base.py (68%) create mode 100644 lib/tfw/event_handler_base/fsm_aware_event_handler.py diff --git a/lib/envvars/__init__.py b/lib/envvars/__init__.py index d1ed0d4..46774d4 100644 --- a/lib/envvars/__init__.py +++ b/lib/envvars/__init__.py @@ -4,7 +4,7 @@ from collections import namedtuple from os import environ -from tfw.decorators import lazy_property +from tfw.decorators.lazy_property import lazy_property class LazyEnvironment: diff --git a/lib/tfw/__init__.py b/lib/tfw/__init__.py index 9c67327..db64b25 100644 --- a/lib/tfw/__init__.py +++ b/lib/tfw/__init__.py @@ -1,5 +1,2 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. - -from .event_handler_base import EventHandlerBase, FSMAwareEventHandler, BroadcastingEventHandler -from .fsm import FSMBase, LinearFSM, YamlFSM diff --git a/lib/tfw/components/directory_monitor.py b/lib/tfw/components/directory_monitor.py index c72a13e..f65c8ff 100644 --- a/lib/tfw/components/directory_monitor.py +++ b/lib/tfw/components/directory_monitor.py @@ -5,9 +5,9 @@ from functools import wraps from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler -from tfw.networking.event_handlers import ServerUplinkConnector -from tfw.decorators import RateLimiter -from tfw.mixins import ObserverMixin +from tfw.networking.event_handlers.server_connector import ServerUplinkConnector +from tfw.decorators.rate_limiter import RateLimiter +from tfw.mixins.observer_mixin import ObserverMixin 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 6f89e91..8d97022 100644 --- a/lib/tfw/components/directory_monitoring_event_handler.py +++ b/lib/tfw/components/directory_monitoring_event_handler.py @@ -3,10 +3,10 @@ from os.path import isdir, exists -from tfw import EventHandlerBase +from tfw.event_handler_base import EventHandlerBase +from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin +from tfw.components.directory_monitor import DirectoryMonitor from tfw.config.logs import logging -from tfw.mixins import MonitorManagerMixin -from .directory_monitor import DirectoryMonitor LOG = logging.getLogger(__name__) diff --git a/lib/tfw/components/fsm_managing_event_handler.py b/lib/tfw/components/fsm_managing_event_handler.py index 39ec3ae..376ce50 100644 --- a/lib/tfw/components/fsm_managing_event_handler.py +++ b/lib/tfw/components/fsm_managing_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 import EventHandlerBase +from tfw.event_handler_base import EventHandlerBase from tfw.crypto import KeyManager, sign_message, verify_message from tfw.config.logs import logging diff --git a/lib/tfw/components/history_monitor.py b/lib/tfw/components/history_monitor.py index 9d259ae..410558a 100644 --- a/lib/tfw/components/history_monitor.py +++ b/lib/tfw/components/history_monitor.py @@ -8,8 +8,9 @@ from abc import ABC, abstractmethod from watchdog.events import PatternMatchingEventHandler -from tfw.mixins import CallbackMixin, ObserverMixin -from tfw.decorators import RateLimiter +from tfw.mixins.callback_mixin import CallbackMixin +from tfw.mixins.observer_mixin import ObserverMixin +from tfw.decorators.rate_limiter import RateLimiter class CallbackEventHandler(PatternMatchingEventHandler, ABC): diff --git a/lib/tfw/components/ide_event_handler.py b/lib/tfw/components/ide_event_handler.py index 2c698ad..05905be 100644 --- a/lib/tfw/components/ide_event_handler.py +++ b/lib/tfw/components/ide_event_handler.py @@ -6,10 +6,10 @@ from glob import glob from fnmatch import fnmatchcase from typing import Iterable -from tfw import EventHandlerBase -from tfw.mixins import MonitorManagerMixin +from tfw.event_handler_base import EventHandlerBase +from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin +from tfw.components.directory_monitor import DirectoryMonitor from tfw.config.logs import logging -from .directory_monitor import DirectoryMonitor LOG = logging.getLogger(__name__) diff --git a/lib/tfw/components/log_monitor.py b/lib/tfw/components/log_monitor.py index 2c6ade8..cf06a0a 100644 --- a/lib/tfw/components/log_monitor.py +++ b/lib/tfw/components/log_monitor.py @@ -6,9 +6,10 @@ from os.path import dirname from watchdog.events import PatternMatchingEventHandler as PatternMatchingWatchdogEventHandler -from tfw.networking.event_handlers import ServerUplinkConnector -from tfw.decorators import RateLimiter -from tfw.mixins import ObserverMixin, SupervisorLogMixin +from tfw.networking.event_handlers.server_connector import ServerUplinkConnector +from tfw.decorators.rate_limiter import RateLimiter +from tfw.mixins.observer_mixin import ObserverMixin +from tfw.mixins.supervisor_mixin import SupervisorLogMixin class LogMonitor(ObserverMixin): diff --git a/lib/tfw/components/log_monitoring_event_handler.py b/lib/tfw/components/log_monitoring_event_handler.py index c5dd84c..0bc7ab2 100644 --- a/lib/tfw/components/log_monitoring_event_handler.py +++ b/lib/tfw/components/log_monitoring_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 import EventHandlerBase -from tfw.mixins import MonitorManagerMixin +from tfw.event_handler_base import EventHandlerBase +from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin +from tfw.components.log_monitor import LogMonitor from tfw.config.logs import logging -from .log_monitor import LogMonitor LOG = logging.getLogger(__name__) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 040995e..61b6c62 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 import EventHandlerBase -from tfw.mixins import SupervisorMixin, SupervisorLogMixin +from tfw.event_handler_base import EventHandlerBase +from tfw.mixins.supervisor_mixin import SupervisorMixin, SupervisorLogMixin +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/terminal_event_handler.py b/lib/tfw/components/terminal_event_handler.py index 79e96f7..27bbce7 100644 --- a/lib/tfw/components/terminal_event_handler.py +++ b/lib/tfw/components/terminal_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 import EventHandlerBase +from tfw.event_handler_base import EventHandlerBase +from tfw.components.terminado_mini_server import TerminadoMiniServer 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/crypto.py b/lib/tfw/crypto.py index af632d8..0b37893 100644 --- a/lib/tfw/crypto.py +++ b/lib/tfw/crypto.py @@ -14,8 +14,8 @@ from cryptography.hazmat.primitives.hashes import SHA256 from cryptography.hazmat.primitives.hmac import HMAC as _HMAC from cryptography.exceptions import InvalidSignature -from tfw.networking import message_bytes -from tfw.decorators import lazy_property +from tfw.networking.serialization import message_bytes +from tfw.decorators.lazy_property import lazy_property from tfw.config import TFWENV diff --git a/lib/tfw/decorators/__init__.py b/lib/tfw/decorators/__init__.py index ed79d7f..db64b25 100644 --- a/lib/tfw/decorators/__init__.py +++ b/lib/tfw/decorators/__init__.py @@ -1,5 +1,2 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. - -from .rate_limiter import RateLimiter -from .lazy_property import lazy_property diff --git a/lib/tfw/event_handler_base/__init__.py b/lib/tfw/event_handler_base/__init__.py new file mode 100644 index 0000000..fd50525 --- /dev/null +++ b/lib/tfw/event_handler_base/__init__.py @@ -0,0 +1,6 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from .event_handler_base import EventHandlerBase +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_handler_base/boradcasting_event_handler.py new file mode 100644 index 0000000..59ee493 --- /dev/null +++ b/lib/tfw/event_handler_base/boradcasting_event_handler.py @@ -0,0 +1,30 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from abc import ABC + +from tfw.event_handler_base.event_handler_base import EventHandlerBase +from tfw.crypto import message_checksum + + +class BroadcastingEventHandler(EventHandlerBase, ABC): + # pylint: disable=abstract-method + """ + Abstract base class for EventHandlers which broadcast responses + and intelligently ignore their own broadcasted messages they receive. + """ + def __init__(self, key): + super().__init__(key) + self.own_message_hashes = [] + + def event_handler_callback(self, message): + message_hash = message_checksum(message) + + if message_hash in self.own_message_hashes: + self.own_message_hashes.remove(message_hash) + return + + response = self.dispatch_handling(message) + if response: + self.own_message_hashes.append(message_checksum(response)) + self.server_connector.broadcast(response) diff --git a/lib/tfw/event_handler_base.py b/lib/tfw/event_handler_base/event_handler_base.py similarity index 68% rename from lib/tfw/event_handler_base.py rename to lib/tfw/event_handler_base/event_handler_base.py index 15d46bc..f676bac 100644 --- a/lib/tfw/event_handler_base.py +++ b/lib/tfw/event_handler_base/event_handler_base.py @@ -3,9 +3,7 @@ from abc import ABC, abstractmethod -from tfw.networking import FSMAware -from tfw.networking.event_handlers import ServerConnector -from tfw.crypto import message_checksum +from tfw.networking.event_handlers.server_connector import ServerConnector from tfw.config.logs import logging LOG = logging.getLogger(__name__) @@ -105,43 +103,3 @@ class EventHandlerBase(ABC): connections and stuff like that. """ pass - - -class FSMAwareEventHandler(EventHandlerBase, FSMAware, ABC): - # pylint: disable=abstract-method - """ - Abstract base class for EventHandlers which automatically - keep track of the state of the TFW FSM. - """ - def __init__(self, key): - EventHandlerBase.__init__(self, key) - FSMAware.__init__(self) - self.subscribe('fsm_update') - - def dispatch_handling(self, message): - if self.update_fsm_data(message): - return None - return super().dispatch_handling(message) - - -class BroadcastingEventHandler(EventHandlerBase, ABC): - # pylint: disable=abstract-method - """ - Abstract base class for EventHandlers which broadcast responses - and intelligently ignore their own broadcasted messages they receive. - """ - def __init__(self, key): - super().__init__(key) - self.own_message_hashes = [] - - def event_handler_callback(self, message): - message_hash = message_checksum(message) - - if message_hash in self.own_message_hashes: - self.own_message_hashes.remove(message_hash) - return - - response = self.dispatch_handling(message) - if response: - self.own_message_hashes.append(message_checksum(response)) - self.server_connector.broadcast(response) diff --git a/lib/tfw/event_handler_base/fsm_aware_event_handler.py b/lib/tfw/event_handler_base/fsm_aware_event_handler.py new file mode 100644 index 0000000..01d6360 --- /dev/null +++ b/lib/tfw/event_handler_base/fsm_aware_event_handler.py @@ -0,0 +1,24 @@ +# Copyright (C) 2018 Avatao.com Innovative Learning Kft. +# All Rights Reserved. See LICENSE file for details. + +from abc import ABC + +from tfw.event_handler_base.event_handler_base import EventHandlerBase +from tfw.networking.fsm_aware import FSMAware + + +class FSMAwareEventHandler(EventHandlerBase, FSMAware, ABC): + # pylint: disable=abstract-method + """ + Abstract base class for EventHandlers which automatically + keep track of the state of the TFW FSM. + """ + def __init__(self, key): + EventHandlerBase.__init__(self, key) + FSMAware.__init__(self) + self.subscribe('fsm_update') + + def dispatch_handling(self, message): + if self.update_fsm_data(message): + return None + return super().dispatch_handling(message) diff --git a/lib/tfw/fsm/fsm_base.py b/lib/tfw/fsm/fsm_base.py index e33ae33..5b58ce3 100644 --- a/lib/tfw/fsm/fsm_base.py +++ b/lib/tfw/fsm/fsm_base.py @@ -6,7 +6,7 @@ from datetime import datetime from transitions import Machine, MachineError -from tfw.mixins import CallbackMixin +from tfw.mixins.callback_mixin import CallbackMixin from tfw.config.logs import logging LOG = logging.getLogger(__name__) diff --git a/lib/tfw/fsm/linear_fsm.py b/lib/tfw/fsm/linear_fsm.py index a053c30..cc5a0d1 100644 --- a/lib/tfw/fsm/linear_fsm.py +++ b/lib/tfw/fsm/linear_fsm.py @@ -3,7 +3,7 @@ from transitions import State -from .fsm_base import FSMBase +from tfw.fsm.fsm_base import FSMBase class LinearFSM(FSMBase): diff --git a/lib/tfw/fsm/yaml_fsm.py b/lib/tfw/fsm/yaml_fsm.py index 38f7a8e..af0b682 100644 --- a/lib/tfw/fsm/yaml_fsm.py +++ b/lib/tfw/fsm/yaml_fsm.py @@ -9,7 +9,7 @@ import yaml import jinja2 from transitions import State -from .fsm_base import FSMBase +from tfw.fsm.fsm_base import FSMBase class YamlFSM(FSMBase): diff --git a/lib/tfw/mixins/__init__.py b/lib/tfw/mixins/__init__.py index 58915ca..db64b25 100644 --- a/lib/tfw/mixins/__init__.py +++ b/lib/tfw/mixins/__init__.py @@ -1,7 +1,2 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. - -from .supervisor_mixin import SupervisorMixin, SupervisorLogMixin -from .callback_mixin import CallbackMixin -from .observer_mixin import ObserverMixin -from .monitor_manager_mixin import MonitorManagerMixin diff --git a/lib/tfw/mixins/callback_mixin.py b/lib/tfw/mixins/callback_mixin.py index 33ddb6d..8075f47 100644 --- a/lib/tfw/mixins/callback_mixin.py +++ b/lib/tfw/mixins/callback_mixin.py @@ -3,7 +3,7 @@ from functools import partial -from tfw.decorators import lazy_property +from tfw.decorators.lazy_property import lazy_property class CallbackMixin: diff --git a/lib/tfw/mixins/observer_mixin.py b/lib/tfw/mixins/observer_mixin.py index 9d8b5e2..d5415e5 100644 --- a/lib/tfw/mixins/observer_mixin.py +++ b/lib/tfw/mixins/observer_mixin.py @@ -3,7 +3,7 @@ from watchdog.observers import Observer -from tfw.decorators import lazy_property +from tfw.decorators.lazy_property import lazy_property class ObserverMixin: diff --git a/lib/tfw/mixins/supervisor_mixin.py b/lib/tfw/mixins/supervisor_mixin.py index 2238985..189d7cc 100644 --- a/lib/tfw/mixins/supervisor_mixin.py +++ b/lib/tfw/mixins/supervisor_mixin.py @@ -6,7 +6,7 @@ from xmlrpc.client import Fault as SupervisorFault from contextlib import suppress from os import remove -from tfw.decorators import lazy_property +from tfw.decorators.lazy_property import lazy_property from tfw.config import TFWENV diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index 0f038b0..500dd7a 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -1,10 +1,6 @@ # 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 -from .serialization import with_deserialize_tfw_msg, message_bytes -from .zmq_connector_base import ZMQConnectorBase from .message_sender import MessageSender from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector from .server.tfw_server import TFWServer -from .fsm_aware import FSMAware diff --git a/lib/tfw/networking/event_handlers/__init__.py b/lib/tfw/networking/event_handlers/__init__.py index b3ad530..db64b25 100644 --- a/lib/tfw/networking/event_handlers/__init__.py +++ b/lib/tfw/networking/event_handlers/__init__.py @@ -1,4 +1,2 @@ # 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 612bb8d..7d24803 100644 --- a/lib/tfw/networking/event_handlers/server_connector.py +++ b/lib/tfw/networking/event_handlers/server_connector.py @@ -6,8 +6,8 @@ from functools import partial import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking import serialize_tfw_msg, with_deserialize_tfw_msg -from tfw.networking import ZMQConnectorBase +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 diff --git a/lib/tfw/networking/message_sender.py b/lib/tfw/networking/message_sender.py index 378ad91..58eb1c9 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 import ServerUplinkConnector +from tfw.networking.event_handlers.server_connector import ServerUplinkConnector class MessageSender: diff --git a/lib/tfw/networking/server/__init__.py b/lib/tfw/networking/server/__init__.py index eb9adde..db64b25 100644 --- a/lib/tfw/networking/server/__init__.py +++ b/lib/tfw/networking/server/__init__.py @@ -1,5 +1,2 @@ # 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 diff --git a/lib/tfw/networking/server/event_handler_connector.py b/lib/tfw/networking/server/event_handler_connector.py index c4c6338..5075c56 100644 --- a/lib/tfw/networking/server/event_handler_connector.py +++ b/lib/tfw/networking/server/event_handler_connector.py @@ -4,7 +4,8 @@ import zmq from zmq.eventloop.zmqstream import ZMQStream -from tfw.networking import ZMQConnectorBase, serialize_tfw_msg, with_deserialize_tfw_msg +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 diff --git a/lib/tfw/networking/server/tfw_server.py b/lib/tfw/networking/server/tfw_server.py index f4a83a6..fcea107 100644 --- a/lib/tfw/networking/server/tfw_server.py +++ b/lib/tfw/networking/server/tfw_server.py @@ -6,15 +6,14 @@ from contextlib import suppress from tornado.web import Application -from tfw.networking.event_handlers import ServerUplinkConnector -from tfw.networking.server import EventHandlerConnector -from tfw.networking import MessageSender +from tfw.networking.server.zmq_websocket_proxy import ZMQWebSocketProxy +from tfw.networking.event_handlers.server_connector import ServerUplinkConnector +from tfw.networking.server.event_handler_connector import EventHandlerConnector +from tfw.networking.message_sender import MessageSender from tfw.networking.fsm_aware import FSMAware from tfw.crypto import KeyManager, verify_message, sign_message 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/networking/server/zmq_websocket_proxy.py index cb43a30..6df7c9a 100644 --- a/lib/tfw/networking/server/zmq_websocket_proxy.py +++ b/lib/tfw/networking/server/zmq_websocket_proxy.py @@ -5,7 +5,7 @@ import json from tornado.websocket import WebSocketHandler -from tfw.mixins import CallbackMixin +from tfw.mixins.callback_mixin import CallbackMixin from tfw.config.logs import logging LOG = logging.getLogger(__name__)