mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-22 07:01:55 +00:00
Simplify PipeIOHandlerBase by using meghod assignment
This commit is contained in:
parent
b733f53b83
commit
613d906b44
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user