27 lines
1 KiB
Python
27 lines
1 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Desktop notification helper. Prefers notify-send; no GUI imports."""
|
|
from __future__ import annotations
|
|
|
|
import shutil
|
|
import subprocess
|
|
from typing import Callable, Optional, Sequence
|
|
|
|
Runner = Callable[[Sequence[str]], "subprocess.CompletedProcess"]
|
|
|
|
|
|
def _default_runner(cmd: Sequence[str]) -> "subprocess.CompletedProcess":
|
|
try:
|
|
return subprocess.run(list(cmd), capture_output=True, text=True,
|
|
timeout=10, check=False)
|
|
except (OSError, subprocess.TimeoutExpired) as exc:
|
|
return subprocess.CompletedProcess(list(cmd), 127, "", str(exc))
|
|
|
|
|
|
def notify(title: str, body: str, *, runner: Optional[Runner] = None,
|
|
which: Callable[[str], object] = shutil.which) -> bool:
|
|
"""Show a desktop notification. Returns True if notify-send was invoked."""
|
|
if which("notify-send") is None:
|
|
return False
|
|
run = runner or _default_runner
|
|
run(["notify-send", "-a", "Enodia Sentinel", title, body])
|
|
return True
|