From 0067ce83f07f4463b0c391c5bf658f815910b733 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 10 Jul 2020 17:11:50 +0200 Subject: [PATCH 1/3] added new module messagereceiver * binds to unix sockets and listens for incoming messages. The message will then be displayed in the status bar. --- .../modules/contrib/messagereceiver.py | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 bumblebee_status/modules/contrib/messagereceiver.py diff --git a/bumblebee_status/modules/contrib/messagereceiver.py b/bumblebee_status/modules/contrib/messagereceiver.py new file mode 100644 index 0000000..1f643d6 --- /dev/null +++ b/bumblebee_status/modules/contrib/messagereceiver.py @@ -0,0 +1,85 @@ +# pylint: disable=C0111,R0903 + +""" +Displays the message that's received via unix socket. + +Parameteres: + * messagereceiver : Unix socket address (e.g: /tmp/bumblebee_messagereceiver.sock) +""" + +import core.module +import core.widget +import core.input + +import socket +import threading +import logging +import os +import queue +import json + + +class Worker(threading.Thread): + def __init__(self, unix_socket_address, queue): + threading.Thread.__init__(self) + self.__unix_socket_address = unix_socket_address + self.__queue = queue + + def run(self): + while True: + try: + os.unlink(self.__unix_socket_address) + except OSError as e: + if os.path.exists(self.__unix_socket_address): + logging.exception( + "Couldn't bind to unix socket %s" % self.__unix_socket_address + ) + raise + + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: + s.bind(self.__unix_socket_address) + s.listen() + + conn, _ = s.accept() + with conn: + while True: + data = conn.recv(1024) + if not data: + break + self.__queue.put(data.decode("utf-8")) + + +class Module(core.module.Module): + @core.decorators.every(seconds=1) + def __init__(self, config, theme): + super().__init__(config, theme, core.widget.Widget(self.message)) + + self.__unix_socket_address = self.parameter("address", "") + + self.__message = "" + self.__state = [] + + self.__queue = queue.Queue() + self.__worker = Worker(self.__unix_socket_address, self.__queue) + self.__worker.daemon = True + self.__worker.start() + + def message(self, widget): + return self.__message + + def update(self): + try: + received_data = self.__queue.get(block=False) + parsed_data = json.loads(received_data) + self.__message = parsed_data["message"] + self.__state = parsed_data["state"] + except json.JSONDecodeError as e: + logging.exception("Couldn't parse message") + except queue.Empty as e: + pass + + def state(self, widget): + return self.__state + + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From adbba9bf9a06461c9182e77f383a41a5d036203c Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 10 Jul 2020 17:17:16 +0200 Subject: [PATCH 2/3] fixed small bug in messagereceiver * wrong logging syntax --- bumblebee_status/modules/contrib/messagereceiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bumblebee_status/modules/contrib/messagereceiver.py b/bumblebee_status/modules/contrib/messagereceiver.py index 1f643d6..a202b0b 100644 --- a/bumblebee_status/modules/contrib/messagereceiver.py +++ b/bumblebee_status/modules/contrib/messagereceiver.py @@ -32,7 +32,7 @@ class Worker(threading.Thread): except OSError as e: if os.path.exists(self.__unix_socket_address): logging.exception( - "Couldn't bind to unix socket %s" % self.__unix_socket_address + "Couldn't bind to unix socket %s", self.__unix_socket_address ) raise From 01cde70e14b9a873a2a02f44242328843e598075 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 10 Jul 2020 17:35:40 +0200 Subject: [PATCH 3/3] improved documentation of messagereceiver module --- bumblebee_status/modules/contrib/messagereceiver.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bumblebee_status/modules/contrib/messagereceiver.py b/bumblebee_status/modules/contrib/messagereceiver.py index a202b0b..1fb8ca2 100644 --- a/bumblebee_status/modules/contrib/messagereceiver.py +++ b/bumblebee_status/modules/contrib/messagereceiver.py @@ -5,6 +5,17 @@ Displays the message that's received via unix socket. Parameteres: * messagereceiver : Unix socket address (e.g: /tmp/bumblebee_messagereceiver.sock) + +Example: + The following examples assume that /tmp/bumblebee_messagereceiver.sock is used as unix socket address. + + In order to send the string "I  bumblebee-status" to your status bar, use the following command: + echo -e '{"message":"I  bumblebee-status", "state": ""}' | socat unix-connect:/tmp/bumblebee_messagereceiver.sock STDIO + + In order to highlight the text, the state variable can be used: + echo -e '{"message":"I  bumblebee-status", "state": "warning"}' | socat unix-connect:/tmp/bumblebee_messagereceiver.sock STDIO + + """ import core.module