Allow subclassing pipe based event handlers (refactor PipeIOEventHandler to base class and impl)

This commit is contained in:
Kristóf Tóth 2019-05-06 15:23:21 +02:00
parent ddc79c9717
commit 90b780a5c0
2 changed files with 26 additions and 17 deletions

View File

@ -10,4 +10,4 @@ from .terminal_commands import TerminalCommands
from .log_monitoring_event_handler import LogMonitoringEventHandler from .log_monitoring_event_handler import LogMonitoringEventHandler
from .fsm_managing_event_handler import FSMManagingEventHandler from .fsm_managing_event_handler import FSMManagingEventHandler
from .snapshot_provider import SnapshotProvider from .snapshot_provider import SnapshotProvider
from .pipe_io_event_handler import PipeIOEventHandler from .pipe_io_event_handler import PipeIOEventHandlerBase, PipeIOEventHandler

View File

@ -1,3 +1,4 @@
from abc import abstractmethod
from json import loads, dumps, JSONDecodeError from json import loads, dumps, JSONDecodeError
from tfw import EventHandlerBase from tfw import EventHandlerBase
@ -8,37 +9,45 @@ from .pipe_io_server import PipeIOServer
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class PipeIOEventHandler(EventHandlerBase): class PipeIOEventHandlerBase(EventHandlerBase):
def __init__(self, key, in_pipe_path, out_pipe_path, permissions=0o600): def __init__(self, key, in_pipe_path, out_pipe_path, permissions=0o600):
super().__init__(key) super().__init__(key)
self._pipe_io_server = JSONProxyPipeIOServer( self.pipe_io = CallbackPipeIOServer(
in_pipe_path, in_pipe_path,
out_pipe_path, out_pipe_path,
self.server_connector.send, self.handle_pipe_event,
permissions permissions
) )
self._pipe_io_server.start() self.pipe_io.start()
@abstractmethod
def handle_pipe_event(self, message):
raise NotImplementedError()
def cleanup(self): def cleanup(self):
self._pipe_io_server.stop() self.pipe_io.stop()
class CallbackPipeIOServer(PipeIOServer):
def __init__(self, in_pipe_path, out_pipe_path, callback, permissions):
super().__init__(in_pipe_path, out_pipe_path, permissions)
self.callback = callback
def handle_message(self, message):
self.callback(message)
class PipeIOEventHandler(PipeIOEventHandlerBase):
def handle_event(self, message): def handle_event(self, message):
try: try:
json_bytes = dumps(message).encode() json_bytes = dumps(message).encode()
self._pipe_io_server.send_message(json_bytes) self.pipe_io.send_message(json_bytes)
except TypeError: except TypeError:
LOG.error("Message %s not JSON serializable! Ignoring...", message) LOG.error("Message %s not JSON serializable! Ignoring...", message)
def handle_pipe_event(self, message):
class JSONProxyPipeIOServer(PipeIOServer):
def __init__(self, in_pipe_path, out_pipe_path, proxy_method, permissions):
super().__init__(in_pipe_path, out_pipe_path, permissions)
self.proxy = proxy_method
def handle_message(self, message):
try: try:
json = loads(message) json = loads(message)
self.proxy(json) self.server_connector.send(json)
except JSONDecodeError: except JSONDecodeError:
LOG.error("Invalid JSON received on %s! Ignoring...", self._in_pipe) LOG.error("Invalid JSON received on %s! Ignoring...", self.pipe_io.in_pipe)