Resolve Python circular import hell - hopefully forever

This commit is contained in:
Kristóf Tóth 2018-07-26 13:59:06 +02:00
parent 7fb5a37831
commit 7a670f37f2
32 changed files with 105 additions and 105 deletions

View File

@ -4,7 +4,7 @@
from collections import namedtuple from collections import namedtuple
from os import environ from os import environ
from tfw.decorators import lazy_property from tfw.decorators.lazy_property import lazy_property
class LazyEnvironment: class LazyEnvironment:

View File

@ -1,5 +1,2 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from .event_handler_base import EventHandlerBase, FSMAwareEventHandler, BroadcastingEventHandler
from .fsm import FSMBase, LinearFSM, YamlFSM

View File

@ -5,9 +5,9 @@ from functools import wraps
from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler from watchdog.events import FileSystemEventHandler as FileSystemWatchdogEventHandler
from tfw.networking.event_handlers import ServerUplinkConnector from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
from tfw.decorators import RateLimiter from tfw.decorators.rate_limiter import RateLimiter
from tfw.mixins import ObserverMixin from tfw.mixins.observer_mixin import ObserverMixin
from tfw.config.logs import logging from tfw.config.logs import logging

View File

@ -3,10 +3,10 @@
from os.path import isdir, exists 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.config.logs import logging
from tfw.mixins import MonitorManagerMixin
from .directory_monitor import DirectoryMonitor
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # 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.crypto import KeyManager, sign_message, verify_message
from tfw.config.logs import logging from tfw.config.logs import logging

View File

@ -8,8 +8,9 @@ from abc import ABC, abstractmethod
from watchdog.events import PatternMatchingEventHandler from watchdog.events import PatternMatchingEventHandler
from tfw.mixins import CallbackMixin, ObserverMixin from tfw.mixins.callback_mixin import CallbackMixin
from tfw.decorators import RateLimiter from tfw.mixins.observer_mixin import ObserverMixin
from tfw.decorators.rate_limiter import RateLimiter
class CallbackEventHandler(PatternMatchingEventHandler, ABC): class CallbackEventHandler(PatternMatchingEventHandler, ABC):

View File

@ -6,10 +6,10 @@ from glob import glob
from fnmatch import fnmatchcase from fnmatch import fnmatchcase
from typing import Iterable from typing import Iterable
from tfw import EventHandlerBase from tfw.event_handler_base import EventHandlerBase
from tfw.mixins import MonitorManagerMixin from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin
from tfw.components.directory_monitor import DirectoryMonitor
from tfw.config.logs import logging from tfw.config.logs import logging
from .directory_monitor import DirectoryMonitor
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -6,9 +6,10 @@ from os.path import dirname
from watchdog.events import PatternMatchingEventHandler as PatternMatchingWatchdogEventHandler from watchdog.events import PatternMatchingEventHandler as PatternMatchingWatchdogEventHandler
from tfw.networking.event_handlers import ServerUplinkConnector from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
from tfw.decorators import RateLimiter from tfw.decorators.rate_limiter import RateLimiter
from tfw.mixins import ObserverMixin, SupervisorLogMixin from tfw.mixins.observer_mixin import ObserverMixin
from tfw.mixins.supervisor_mixin import SupervisorLogMixin
class LogMonitor(ObserverMixin): class LogMonitor(ObserverMixin):

View File

@ -1,10 +1,10 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from tfw import EventHandlerBase from tfw.event_handler_base import EventHandlerBase
from tfw.mixins import MonitorManagerMixin from tfw.mixins.monitor_manager_mixin import MonitorManagerMixin
from tfw.components.log_monitor import LogMonitor
from tfw.config.logs import logging from tfw.config.logs import logging
from .log_monitor import LogMonitor
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -3,10 +3,10 @@
from xmlrpc.client import Fault as SupervisorFault from xmlrpc.client import Fault as SupervisorFault
from tfw import EventHandlerBase from tfw.event_handler_base import EventHandlerBase
from tfw.mixins import SupervisorMixin, SupervisorLogMixin from tfw.mixins.supervisor_mixin import SupervisorMixin, SupervisorLogMixin
from tfw.components.directory_monitor import with_monitor_paused
from tfw.config.logs import logging from tfw.config.logs import logging
from .directory_monitor import with_monitor_paused
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -1,11 +1,11 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # 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 import TFWENV
from tfw.config.logs import logging from tfw.config.logs import logging
from tao.config import TAOENV from tao.config import TAOENV
from .terminado_mini_server import TerminadoMiniServer
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -14,8 +14,8 @@ from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.hazmat.primitives.hmac import HMAC as _HMAC from cryptography.hazmat.primitives.hmac import HMAC as _HMAC
from cryptography.exceptions import InvalidSignature from cryptography.exceptions import InvalidSignature
from tfw.networking import message_bytes from tfw.networking.serialization import message_bytes
from tfw.decorators import lazy_property from tfw.decorators.lazy_property import lazy_property
from tfw.config import TFWENV from tfw.config import TFWENV

View File

@ -1,5 +1,2 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from .rate_limiter import RateLimiter
from .lazy_property import lazy_property

View File

@ -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

View File

@ -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)

View File

@ -3,9 +3,7 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from tfw.networking import FSMAware from tfw.networking.event_handlers.server_connector import ServerConnector
from tfw.networking.event_handlers import ServerConnector
from tfw.crypto import message_checksum
from tfw.config.logs import logging from tfw.config.logs import logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -105,43 +103,3 @@ class EventHandlerBase(ABC):
connections and stuff like that. connections and stuff like that.
""" """
pass 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)

View File

@ -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)

View File

@ -6,7 +6,7 @@ from datetime import datetime
from transitions import Machine, MachineError from transitions import Machine, MachineError
from tfw.mixins import CallbackMixin from tfw.mixins.callback_mixin import CallbackMixin
from tfw.config.logs import logging from tfw.config.logs import logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -3,7 +3,7 @@
from transitions import State from transitions import State
from .fsm_base import FSMBase from tfw.fsm.fsm_base import FSMBase
class LinearFSM(FSMBase): class LinearFSM(FSMBase):

View File

@ -9,7 +9,7 @@ import yaml
import jinja2 import jinja2
from transitions import State from transitions import State
from .fsm_base import FSMBase from tfw.fsm.fsm_base import FSMBase
class YamlFSM(FSMBase): class YamlFSM(FSMBase):

View File

@ -1,7 +1,2 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # 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

View File

@ -3,7 +3,7 @@
from functools import partial from functools import partial
from tfw.decorators import lazy_property from tfw.decorators.lazy_property import lazy_property
class CallbackMixin: class CallbackMixin:

View File

@ -3,7 +3,7 @@
from watchdog.observers import Observer from watchdog.observers import Observer
from tfw.decorators import lazy_property from tfw.decorators.lazy_property import lazy_property
class ObserverMixin: class ObserverMixin:

View File

@ -6,7 +6,7 @@ from xmlrpc.client import Fault as SupervisorFault
from contextlib import suppress from contextlib import suppress
from os import remove from os import remove
from tfw.decorators import lazy_property from tfw.decorators.lazy_property import lazy_property
from tfw.config import TFWENV from tfw.config import TFWENV

View File

@ -1,10 +1,6 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # 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 .message_sender import MessageSender
from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector
from .server.tfw_server import TFWServer from .server.tfw_server import TFWServer
from .fsm_aware import FSMAware

View File

@ -1,4 +1,2 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from .server_connector import ServerConnector, ServerUplinkConnector, ServerDownlinkConnector

View File

@ -6,8 +6,8 @@ from functools import partial
import zmq import zmq
from zmq.eventloop.zmqstream import ZMQStream from zmq.eventloop.zmqstream import ZMQStream
from tfw.networking import serialize_tfw_msg, with_deserialize_tfw_msg from tfw.networking.zmq_connector_base import ZMQConnectorBase
from tfw.networking import ZMQConnectorBase from tfw.networking.serialization import serialize_tfw_msg, with_deserialize_tfw_msg
from tfw.config import TFWENV from tfw.config import TFWENV
from tfw.config.logs import logging from tfw.config.logs import logging

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # 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: class MessageSender:

View File

@ -1,5 +1,2 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from .event_handler_connector import EventHandlerConnector, EventHandlerUplinkConnector, EventHandlerDownlinkConnector
from .tfw_server import TFWServer

View File

@ -4,7 +4,8 @@
import zmq import zmq
from zmq.eventloop.zmqstream import ZMQStream 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 import TFWENV
from tfw.config.logs import logging from tfw.config.logs import logging

View File

@ -6,15 +6,14 @@ from contextlib import suppress
from tornado.web import Application from tornado.web import Application
from tfw.networking.event_handlers import ServerUplinkConnector from tfw.networking.server.zmq_websocket_proxy import ZMQWebSocketProxy
from tfw.networking.server import EventHandlerConnector from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
from tfw.networking import MessageSender 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.networking.fsm_aware import FSMAware
from tfw.crypto import KeyManager, verify_message, sign_message from tfw.crypto import KeyManager, verify_message, sign_message
from tfw.config.logs import logging from tfw.config.logs import logging
from .zmq_websocket_proxy import ZMQWebSocketProxy
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -5,7 +5,7 @@ import json
from tornado.websocket import WebSocketHandler from tornado.websocket import WebSocketHandler
from tfw.mixins import CallbackMixin from tfw.mixins.callback_mixin import CallbackMixin
from tfw.config.logs import logging from tfw.config.logs import logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)