|
| 1 | +import asyncio |
| 2 | +import contextlib |
| 3 | +import functools |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import typing |
| 7 | + |
| 8 | +import desktop_notifier |
| 9 | + |
| 10 | +pipe: "structs.DaemonPipe" = None |
| 11 | +server: asyncio.Future = None |
| 12 | +callbacks: dict[int, typing.Callable] = {} |
| 13 | + |
| 14 | + |
| 15 | +@contextlib.contextmanager |
| 16 | +def setup(): |
| 17 | + start() |
| 18 | + try: |
| 19 | + yield |
| 20 | + finally: |
| 21 | + stop() |
| 22 | + |
| 23 | + |
| 24 | +def start(): |
| 25 | + global pipe, server |
| 26 | + |
| 27 | + import shlex |
| 28 | + import subprocess |
| 29 | + from common.structs import DaemonPipe |
| 30 | + from external import async_thread |
| 31 | + from modules import globals |
| 32 | + |
| 33 | + args = [] |
| 34 | + kwargs = dict( |
| 35 | + icon_uri=(globals.self_path / "resources/icons/icon.png").as_uri(), |
| 36 | + ) |
| 37 | + |
| 38 | + proc = async_thread.wait(asyncio.create_subprocess_exec( |
| 39 | + *shlex.split(globals.start_cmd), |
| 40 | + "notification-daemon", |
| 41 | + json.dumps(args), |
| 42 | + json.dumps(kwargs), |
| 43 | + stdin=subprocess.PIPE, |
| 44 | + stdout=subprocess.PIPE, |
| 45 | + )) |
| 46 | + pipe = DaemonPipe(proc) |
| 47 | + |
| 48 | + server = async_thread.run(_server()) |
| 49 | + |
| 50 | + |
| 51 | +def stop(): |
| 52 | + global pipe, server |
| 53 | + server.cancel() |
| 54 | + server = None |
| 55 | + pipe.kill() |
| 56 | + pipe = None |
| 57 | + |
| 58 | + |
| 59 | +async def _server(): |
| 60 | + from modules import globals |
| 61 | + |
| 62 | + while True: |
| 63 | + data = await pipe.get_async() |
| 64 | + |
| 65 | + try: |
| 66 | + event, args, kwargs = data |
| 67 | + if event == "callback": |
| 68 | + callback = callbacks.pop(args[0], globals.gui.show) |
| 69 | + callback() |
| 70 | + else: |
| 71 | + pass |
| 72 | + except Exception: |
| 73 | + pass |
| 74 | + |
| 75 | + |
| 76 | +def notify( |
| 77 | + title: str, |
| 78 | + msg: str, |
| 79 | + urgency=desktop_notifier.Urgency.Normal, |
| 80 | + icon: desktop_notifier.Icon = None, |
| 81 | + buttons: list[desktop_notifier.Button] = [], |
| 82 | + attachment: desktop_notifier.Attachment = None, |
| 83 | + timeout=5, |
| 84 | +): |
| 85 | + button_callbacks = {"View": 0} |
| 86 | + for button in buttons: |
| 87 | + button_callbacks[button.title] = hash(button.on_pressed) |
| 88 | + callbacks[hash(button.on_pressed)] = button.on_pressed |
| 89 | + kwargs = dict( |
| 90 | + title=title, |
| 91 | + msg=msg, |
| 92 | + urgency=urgency.value, |
| 93 | + icon=icon.as_uri() if icon else None, |
| 94 | + button_callbacks=button_callbacks, |
| 95 | + on_clicked_callback=0, |
| 96 | + attachment=attachment.as_uri() if attachment else None, |
| 97 | + timeout=timeout, |
| 98 | + ) |
| 99 | + pipe.put(("notify", [], kwargs)) |
| 100 | + |
| 101 | + |
| 102 | +def _callback(callback: int): |
| 103 | + print(json.dumps(("callback", [callback], {})), flush=True) |
| 104 | + |
| 105 | + |
| 106 | +async def _notify( |
| 107 | + notifier: desktop_notifier.DesktopNotifier, |
| 108 | + title: str, |
| 109 | + msg: str, |
| 110 | + urgency: str, |
| 111 | + icon: str | None, |
| 112 | + button_callbacks: dict[str, int], |
| 113 | + on_clicked_callback: int, |
| 114 | + attachment: str | None, |
| 115 | + timeout: int, |
| 116 | +): |
| 117 | + await notifier.send( |
| 118 | + title=title, |
| 119 | + message=msg, |
| 120 | + urgency=desktop_notifier.Urgency(urgency), |
| 121 | + icon=desktop_notifier.Icon(uri=icon) if icon else None, |
| 122 | + buttons=[ |
| 123 | + desktop_notifier.Button( |
| 124 | + title=button, |
| 125 | + on_pressed=functools.partial(_callback, callback), |
| 126 | + ) |
| 127 | + for button, callback in button_callbacks.items() |
| 128 | + ], |
| 129 | + on_clicked=functools.partial(_callback, on_clicked_callback), |
| 130 | + attachment=desktop_notifier.Attachment(uri=attachment) if attachment else None, |
| 131 | + timeout=timeout, |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +async def _daemon(icon_uri: str): |
| 136 | + loop = asyncio.get_event_loop() |
| 137 | + |
| 138 | + stdin_full = asyncio.Event() |
| 139 | + loop.add_reader(sys.stdin.fileno(), stdin_full.set) |
| 140 | + |
| 141 | + notifier = desktop_notifier.DesktopNotifier( |
| 142 | + app_name="F95Checker", |
| 143 | + app_icon=desktop_notifier.Icon(uri=icon_uri), |
| 144 | + ) |
| 145 | + |
| 146 | + while True: |
| 147 | + await stdin_full.wait() |
| 148 | + stdin_full.clear() |
| 149 | + line = await loop.run_in_executor(None, sys.stdin.readline) |
| 150 | + |
| 151 | + try: |
| 152 | + data = json.loads(line) |
| 153 | + |
| 154 | + event, args, kwargs = data |
| 155 | + if event == "notify": |
| 156 | + await _notify(notifier, *args, **kwargs) |
| 157 | + else: |
| 158 | + pass |
| 159 | + except Exception: |
| 160 | + pass |
| 161 | + |
| 162 | + |
| 163 | +def daemon(*args, **kwargs): |
| 164 | + asyncio.run(_daemon(*args, **kwargs)) |
0 commit comments