Merge pull request #26 from avatao-content/niceimports

Niceimports
This commit is contained in:
Bokros Bálint 2018-04-06 16:24:37 +02:00 committed by GitHub
commit c05f9f6a28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 66 additions and 43 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,3 +2,4 @@
# All Rights Reserved. See LICENSE file for details.
from .supervisor_mixin import SupervisorMixin
from .callback_mixin import CallbackMixin

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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