Simplify PipeIOHandlerBase by using meghod assignment

This commit is contained in:
Kristóf Tóth 2019-09-04 18:07:36 +02:00
parent b733f53b83
commit 613d906b44

View File

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