output.py (745B)
1 # standard imports 2 import logging 3 import socket 4 import enum 5 6 logg = logging.getLogger(__name__) 7 8 9 class OpMode(enum.Enum): 10 STDOUT = 'standard_output' 11 UNIX = 'unix_socket' 12 13 class Outputter: 14 15 def __init__(self, mode): 16 self.out = getattr(self, 'do_' + mode.value) 17 18 19 def do(self, hx, *args, **kwargs): 20 return self.out(hx, *args, **kwargs) 21 22 23 def do_standard_output(self, hx, *args, **kwargs): 24 return hx 25 26 27 def do_unix_socket(self, hx, *args, **kwargs): 28 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 29 s.connect(kwargs['socket']) 30 s.send(hx.encode('utf-8')) 31 r = s.recv(64+4) 32 logg.debug('r {}'.format(r)) 33 s.close() 34 return r[4:].decode('utf-8')