mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-22 22:21:56 +00:00
Refactor TransformerPipeIOEventHandler
This commit is contained in:
parent
69b3b17724
commit
c4d3319ed9
@ -1,6 +1,7 @@
|
||||
from abc import abstractmethod
|
||||
from json import loads, dumps
|
||||
from subprocess import run, PIPE
|
||||
from functools import partial
|
||||
|
||||
from tfw import EventHandlerBase
|
||||
from tfw.config.logs import logging
|
||||
@ -53,18 +54,13 @@ class TransformerPipeIOEventHandler(PipeIOEventHandlerBase):
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(
|
||||
self, key, in_pipe_path, out_pipe_path,
|
||||
serialize_cmd, deserialize_cmd,
|
||||
transform_in_cmd, transform_out_cmd,
|
||||
permissions=DEFAULT_PERMISSIONS
|
||||
):
|
||||
self._serialize_cmd, self._deserialize_cmd = serialize_cmd, deserialize_cmd
|
||||
self._transform_in = partial(self._transform_message, transform_in_cmd)
|
||||
self._transform_out = partial(self._transform_message, transform_out_cmd)
|
||||
super().__init__(key, in_pipe_path, out_pipe_path, permissions)
|
||||
|
||||
def _serialize_message(self, message):
|
||||
return self._transform_message(self._serialize_cmd, message)
|
||||
|
||||
def _deserialize_message(self, message):
|
||||
return self._transform_message(self._deserialize_cmd, message)
|
||||
|
||||
@staticmethod
|
||||
def _transform_message(transform_cmd, message):
|
||||
proc = run(
|
||||
@ -80,12 +76,12 @@ class TransformerPipeIOEventHandler(PipeIOEventHandlerBase):
|
||||
|
||||
def handle_event(self, message):
|
||||
json_bytes = dumps(message).encode()
|
||||
transformed_bytes = self._serialize_message(json_bytes)
|
||||
transformed_bytes = self._transform_out(json_bytes)
|
||||
if transformed_bytes:
|
||||
self.pipe_io.send_message(transformed_bytes)
|
||||
|
||||
def handle_pipe_event(self, message_bytes):
|
||||
transformed_bytes = self._deserialize_message(message_bytes)
|
||||
transformed_bytes = self._transform_in(message_bytes)
|
||||
if transformed_bytes:
|
||||
json_message = loads(transformed_bytes)
|
||||
self.server_connector.send(json_message)
|
||||
|
Loading…
Reference in New Issue
Block a user