|
| 1 | +import platform |
| 2 | +from logging import getLogger |
| 3 | + |
| 4 | +from .discord import send_notification as send_discord_notification |
| 5 | + |
| 6 | +log = getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +match (platform.system()): |
| 10 | + case "Windows": |
| 11 | + from .windows import send_notification as _send_notification |
| 12 | + |
| 13 | + send_sys_notification = _send_notification |
| 14 | + case "Darwin": # macOS |
| 15 | + from .macos import send_notification as _send_notification |
| 16 | + |
| 17 | + send_sys_notification = _send_notification |
| 18 | + case "Linux": |
| 19 | + from .linux import send_notification as _send_notification |
| 20 | + |
| 21 | + send_sys_notification = _send_notification |
| 22 | + case _ as system: |
| 23 | + log.warning(f"Desktop notifications not supported on {system}") |
| 24 | + |
| 25 | + def send_sys_notification(message: str, title: str) -> None: |
| 26 | + _ = message, title |
| 27 | + raise ImportError( |
| 28 | + f"Desktop notifications not supported on {system}" |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def send_notification( |
| 33 | + message: str, *, title: str, discord_webhook: str | None = None |
| 34 | +) -> None: |
| 35 | + """ |
| 36 | + Send a desktop notification with the given message across different operating systems. |
| 37 | +
|
| 38 | + Dependencies: |
| 39 | + - Windows: win10toast package (pip install win10toast) |
| 40 | + - macOS: AppleScript or pync package (pip install pync) |
| 41 | + - Linux: notify-send command |
| 42 | +
|
| 43 | + Args: |
| 44 | + message (str): The message to display in the notification |
| 45 | +
|
| 46 | + Returns: |
| 47 | + bool: True if notification was sent successfully, False otherwise |
| 48 | + """ |
| 49 | + assert isinstance(message, str), "Message must be a string" |
| 50 | + assert ( |
| 51 | + isinstance(discord_webhook, str) or discord_webhook is None |
| 52 | + ), "Discord webhook must be a string or None" |
| 53 | + system = platform.system() |
| 54 | + |
| 55 | + log.info(f"Sending notification: {message}") |
| 56 | + |
| 57 | + if discord_webhook: |
| 58 | + send_discord_notification(message, discord_webhook=discord_webhook) |
| 59 | + |
| 60 | + log.debug(f"Sending notification on {system} platform") |
| 61 | + |
| 62 | + try: |
| 63 | + send_sys_notification(message, title) |
| 64 | + except ImportError as e: |
| 65 | + log.exception(f"Failed to setup notification: {e}") |
| 66 | + raise |
| 67 | + except Exception as e: |
| 68 | + log.exception(f"Failed to send notification: {e}") |
| 69 | + raise |
0 commit comments