diff options
author | Matt Turner <mattst88@gmail.com> | 2024-05-16 22:47:06 -0400 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2024-05-16 23:08:42 -0400 |
commit | 677735d0e3c9c48ce510caf136664f717005f1bf (patch) | |
tree | 554fb1cef4546c1cd6502e86bbabb331f5ff9045 /mqtt-notify.py | |
parent | 9983e95a40003629fe2b84398aaa63a9e1c33a7d (diff) |
mqtt-notify.py: Run black
Diffstat (limited to 'mqtt-notify.py')
-rwxr-xr-x | mqtt-notify.py | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/mqtt-notify.py b/mqtt-notify.py index 3e6bd9b..96ad556 100755 --- a/mqtt-notify.py +++ b/mqtt-notify.py @@ -17,19 +17,22 @@ import sys import time import paho.mqtt.client as mqtt import gi -gi.require_version('Notify', '0.7') -gi.require_version('Secret', '1') + +gi.require_version("Notify", "0.7") +gi.require_version("Secret", "1") from gi.repository import GLib, Notify, Secret from dbus.mainloop.glib import DBusGMainLoop + DBusGMainLoop(set_as_default=True) -chan_msg = re.compile(r'\[(?P<channel>#.*?)\]\n<\s*(?P<nick>.*?)> \| (?P<msg>.*)') -priv_msg = re.compile(r'\(PM: (?P<nick>.*?)\)\n(?P<msg>.*)') -subj_fmt = re.compile(r'IRC message (on|from) (?P<key>.*)') +chan_msg = re.compile(r"\[(?P<channel>#.*?)\]\n<\s*(?P<nick>.*?)> \| (?P<msg>.*)") +priv_msg = re.compile(r"\(PM: (?P<nick>.*?)\)\n(?P<msg>.*)") +subj_fmt = re.compile(r"IRC message (on|from) (?P<key>.*)") notification_map = {} + class Signaler: def __init__(self, loop): self.loop = loop @@ -37,6 +40,7 @@ class Signaler: def handler(self, *_): self.loop.quit() + def on_connect(client, userdata, flags, rc): print("Connected") @@ -44,10 +48,11 @@ def on_connect(client, userdata, flags, rc): # reconnect then subscriptions will be renewed. client.subscribe(userdata) + def on_close(notification): - key = '' + key = "" if (m := subj_fmt.match(notification.props.summary)) is not None: - key = m.group('key') + key = m.group("key") if key in notification_map: for i in notification_map[key]: @@ -55,27 +60,28 @@ def on_close(notification): i.close() del notification_map[key] + def on_message(client, userdata, msg): - icon = '/usr/share/icons/HighContrast/scalable/apps-extra/internet-group-chat.svg' - message = msg.payload.decode('utf-8') + icon = "/usr/share/icons/HighContrast/scalable/apps-extra/internet-group-chat.svg" + message = msg.payload.decode("utf-8") if (m := re.match(chan_msg, message)) is not None: - subject = 'IRC message on {}'.format(m.group('channel')) - body = '<{}> {}'.format(m.group('nick'), m.group('msg')) - key = m.group('channel') + subject = "IRC message on {}".format(m.group("channel")) + body = "<{}> {}".format(m.group("nick"), m.group("msg")) + key = m.group("channel") if (m := re.match(priv_msg, message)) is not None: - subject = 'IRC message from {}'.format(m.group('nick')) - body = '<{}> {}'.format(m.group('nick'), m.group('msg')) - key = m.group('nick') + subject = "IRC message from {}".format(m.group("nick")) + body = "<{}> {}".format(m.group("nick"), m.group("msg")) + key = m.group("nick") else: - subject = 'IRC' + subject = "IRC" body = message - key = '' + key = "" if key not in notification_map or len(notification_map[key]) == 1: n = Notify.Notification.new(subject, body, icon) - n.set_category('im.received') - n.connect('closed', on_close) + n.set_category("im.received") + n.connect("closed", on_close) if key not in notification_map: notification_map[key] = [n] @@ -91,20 +97,23 @@ def on_message(client, userdata, msg): print("Failed to show notification: {}".format(e), file=sys.stderr) sys.exit(-1) + def on_disconnect(client, userdata, rc): print("Disconnected") + def password(user, host): # Insert password with secret-tool(1). E.g., # secret-tool store --label="mqtts://example.com" user myuser service mqtt host example.com - schema = Secret.Schema.new("org.freedesktop.Secret.Generic", + schema = Secret.Schema.new( + "org.freedesktop.Secret.Generic", Secret.SchemaFlags.NONE, { "user": Secret.SchemaAttributeType.STRING, "service": Secret.SchemaAttributeType.STRING, "host": Secret.SchemaAttributeType.STRING, - } + }, ) attributes = { "user": user, @@ -116,6 +125,7 @@ def password(user, host): time.sleep(5) return pw + def config(filename): try: with open(filename) as file: @@ -123,31 +133,37 @@ def config(filename): config.read_file(file) cfg = config[configparser.DEFAULTSECT] - broker = cfg['broker'] - topic = cfg['topic'] - port = int(cfg['port']) - user = cfg['user'] + broker = cfg["broker"] + topic = cfg["topic"] + port = int(cfg["port"]) + user = cfg["user"] return user, broker, port, topic except: print("Failed to parse {}".format(filename), file=sys.stderr) sys.exit(-1) + def main(argv): loop = GLib.MainLoop() do = Signaler(loop) - signal.signal(signal.SIGINT, do.handler) + signal.signal(signal.SIGINT, do.handler) signal.signal(signal.SIGTERM, do.handler) parser = argparse.ArgumentParser() - parser.add_argument('-c', '--config', help='configuration file', - type=argparse.FileType('r'), required=True) + parser.add_argument( + "-c", + "--config", + help="configuration file", + type=argparse.FileType("r"), + required=True, + ) args = parser.parse_args() user, broker, port, topic = config(args.config.name) - Notify.init('MQTT to Notify bridge') + Notify.init("MQTT to Notify bridge") client = mqtt.Client(userdata=topic) client.tls_set() @@ -165,5 +181,6 @@ def main(argv): client.disconnect() Notify.uninit() -if __name__ == '__main__': + +if __name__ == "__main__": main(sys.argv) |