From 613d906b4478d7e886dd6d4e1adc5bb838a4d916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 4 Sep 2019 18:07:36 +0200 Subject: [PATCH] Simplify PipeIOHandlerBase by using meghod assignment --- tfw/components/pipe_io/pipe_io_handler.py | 25 +++++++++-------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/tfw/components/pipe_io/pipe_io_handler.py b/tfw/components/pipe_io/pipe_io_handler.py index 55ea133..d5726a1 100644 --- a/tfw/components/pipe_io/pipe_io_handler.py +++ b/tfw/components/pipe_io/pipe_io_handler.py @@ -1,7 +1,7 @@ import logging from abc import abstractmethod from json import loads, dumps -from subprocess import run, PIPE, Popen +from subprocess import PIPE, Popen from functools import partial from os import getpgid, killpg from os.path import join @@ -21,14 +21,21 @@ class PipeIOHandlerBase: def __init__(self, in_pipe_path, out_pipe_path, permissions=DEFAULT_PERMISSIONS): self.connector = None - self.pipe_io = CallbackPipeIOServer( + self._in_pipe = in_pipe_path + self.pipe_io = PipeIOServer( in_pipe_path, out_pipe_path, - self.handle_pipe_event, permissions ) + self.pipe_io.handle_message = self._server_handle_message self.pipe_io.start() + def _server_handle_message(self, message): + try: + self.handle_pipe_event(message) + except: # pylint: disable=bare-except + LOG.exception('Failed to handle message %s from pipe %s!', message, self._in_pipe) + @abstractmethod def handle_pipe_event(self, message_bytes): raise NotImplementedError() @@ -37,18 +44,6 @@ class PipeIOHandlerBase: 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): - try: - self.callback(message) - except: # pylint: disable=bare-except - LOG.exception('Failed to handle message %s from pipe %s!', message, self.in_pipe) - - class PipeIOHandler(PipeIOHandlerBase): def handle_event(self, message, _): json_bytes = dumps(message).encode()