From b60dbc847160cbce815c52300aff619808d62a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 21 Nov 2019 13:31:39 +0100 Subject: [PATCH] Fix webservice --- solvable/src/webservice/tfw_uplink.py | 29 +++++++++++++++++++++++++++ solvable/src/webservice/user_ops.py | 12 ++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 solvable/src/webservice/tfw_uplink.py diff --git a/solvable/src/webservice/tfw_uplink.py b/solvable/src/webservice/tfw_uplink.py new file mode 100644 index 0000000..8004607 --- /dev/null +++ b/solvable/src/webservice/tfw_uplink.py @@ -0,0 +1,29 @@ +from os import mkfifo, remove +from os.path import join +from contextlib import suppress +from json import dumps + +from pipe_io import PipeWriter + +TFW_PIPES_DIR = '/run/tfw' + + +class TFWUplink: + def __init__(self): + self._send_pipe = join(TFW_PIPES_DIR, 'webservice_send') + with suppress(FileExistsError): + mkfifo(self._send_pipe) + self._pipe_writer = PipeWriter(self._send_pipe) + + def send_message(self, message): + self._pipe_writer.send_message(dumps(message).encode()) + + def __enter__(self): + return self + + def __exit__(self, type_, value, traceback): + self.close() + + def close(self): + self._pipe_writer.close() + remove(self._send_pipe) diff --git a/solvable/src/webservice/user_ops.py b/solvable/src/webservice/user_ops.py index 66855e8..ac43a2d 100644 --- a/solvable/src/webservice/user_ops.py +++ b/solvable/src/webservice/user_ops.py @@ -1,4 +1,4 @@ -from pipe_io import PipeWriter +from tfw_uplink import TFWUplink from crypto import PasswordHasher from model import User from errors import InvalidCredentialsError, UserExistsError @@ -9,8 +9,14 @@ class UserOps: self.username = username self.password = password self.db_session = db_session - self.pipe = PipeWriter('/tmp/tfw_bot_send') - self.log = lambda message: self.pipe.send_message(message.encode()+b"\n") + self._uplink = TFWUplink() + + def log(self, message): + self._uplink.send_message({ + 'key': 'message.send', + 'originator': 'webservice', + 'message': message + }) def authenticate(self): """