2020-07-10 17:11:50 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""
|
|
|
|
Displays the message that's received via unix socket.
|
|
|
|
|
2022-07-21 08:07:33 +02:00
|
|
|
Parameters:
|
2020-07-10 17:11:50 +02:00
|
|
|
* messagereceiver : Unix socket address (e.g: /tmp/bumblebee_messagereceiver.sock)
|
2020-07-10 17:35:40 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-07-10 20:19:43 +02:00
|
|
|
contributed by `bbernhard <https://github.com/bbernhard>`_ - many thanks!
|
2020-07-10 17:11:50 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import socket
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
2020-07-11 18:07:57 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
|
|
|
|
|
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.never
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.message))
|
|
|
|
|
|
|
|
self.background = True
|
|
|
|
|
|
|
|
self.__unix_socket_address = self.parameter("address", "")
|
|
|
|
|
|
|
|
self.__message = ""
|
|
|
|
self.__state = []
|
2020-07-10 17:11:50 +02:00
|
|
|
|
2020-07-11 18:07:57 +02:00
|
|
|
def message(self, widget):
|
|
|
|
return self.__message
|
2020-07-10 17:11:50 +02:00
|
|
|
|
2020-07-11 18:07:57 +02:00
|
|
|
def __read_data_from_socket(self):
|
2020-07-10 17:11:50 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
os.unlink(self.__unix_socket_address)
|
2020-07-11 18:07:57 +02:00
|
|
|
except OSError:
|
2020-07-10 17:11:50 +02:00
|
|
|
if os.path.exists(self.__unix_socket_address):
|
|
|
|
logging.exception(
|
2020-07-10 17:17:16 +02:00
|
|
|
"Couldn't bind to unix socket %s", self.__unix_socket_address
|
2020-07-10 17:11:50 +02:00
|
|
|
)
|
|
|
|
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
|
2020-07-11 18:07:57 +02:00
|
|
|
yield data.decode("utf-8")
|
2020-07-10 17:11:50 +02:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
try:
|
2020-07-11 18:07:57 +02:00
|
|
|
for received_data in self.__read_data_from_socket():
|
|
|
|
parsed_data = json.loads(received_data)
|
|
|
|
self.__message = parsed_data["message"]
|
|
|
|
self.__state = parsed_data["state"]
|
|
|
|
core.event.trigger("update", [self.id], redraw_only=True)
|
|
|
|
except json.JSONDecodeError:
|
2020-07-10 17:11:50 +02:00
|
|
|
logging.exception("Couldn't parse message")
|
2020-07-11 18:07:57 +02:00
|
|
|
except Exception:
|
|
|
|
logging.exception("Unexpected exception while reading from socket")
|
2020-07-10 17:11:50 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
return self.__state
|
|
|
|
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|