Files
tick-stock-panel/backend/app/services/notify_adapter.py
T
shy3130 41be5096dc feat: 桌面客户端打包 (pywebview + PyInstaller) + 系统通知
- 桌面入口: desktop.py (单实例锁 + 端口探测 + uvicorn + pywebview 窗口)
- 配置层: config.py 加 sys.frozen 检测, frozen 下 data_dir 指向用户目录
- 系统通知: notify_adapter.py 三平台 (Win winotify / mac osascript / Linux notify-send)
  接入 quote_service 告警出口, preferences 开关控制, 前端监控设置加 toggle
- 前端: 监控设置加系统通知开关, 系统设置加检查更新入口
- 打包: packaging/tickflow.spec (onedir, collect-all 原生库, 排除 vectorbt)
- CI: .github/workflows/release.yml 手动触发 + 平台可选 (windows/macos/linux)
- 依赖: platformdirs/winotify/plyer 入主依赖, pywebview 入 desktop extras
- README 加桌面客户端下载章节
- bump 版本号到 0.1.33
2026-06-23 21:47:29 +08:00

137 lines
4.2 KiB
Python

"""系统通知适配器 — 三平台原生通知中心。
职责: 把后端产生的告警事件推送到操作系统通知中心。
窗口最小化 / 被遮挡 / 后台运行时都能弹通知 (不依赖前端 WebView)。
平台实现:
- Windows: winotify (进现代操作中心, 支持图标)
- macOS: osascript (系统已内置, 无需额外依赖)
- Linux: notify-send (系统已内置) / plyer 兜底
设计: 失败静默降级, 绝不因通知失败阻断告警主流程 (落盘 / SSE 推送)。
通知去重不在本层做, 复用 MonitorRuleEngine 的 cooldown 逻辑。
"""
from __future__ import annotations
import logging
import subprocess
import sys
from pathlib import Path
logger = logging.getLogger(__name__)
# 单次通知最长字符 (避免某些平台截断报错)
_MAX_LEN = 200
# 避免重复探测平台能力, 缓存一次
_backend_cache: str | None = None
def _detect_backend() -> str | None:
"""探测当前平台可用的通知后端。"""
global _backend_cache
if _backend_cache is not None:
return _backend_cache if _backend_cache != "none" else None
backend = None
if sys.platform == "win32":
try:
import winotify # type: ignore[import-not-found] # noqa: F401
backend = "winotify"
except ImportError:
logger.debug("winotify 不可用, Windows 通知降级")
backend = None
elif sys.platform == "darwin":
backend = "osascript"
elif sys.platform.startswith("linux"):
backend = "notify-send"
_backend_cache = backend or "none"
return backend
def _truncate(text: str) -> str:
"""截断超长文本, 避免平台通知上限报错。"""
text = (text or "").strip()
return text[:_MAX_LEN] + ("…" if len(text) > _MAX_LEN else "")
def notify(title: str, message: str, icon: Path | None = None) -> bool:
"""推送一条系统通知。
Args:
title: 通知标题
message: 通知正文
icon: 可选图标路径 (部分平台支持)
Returns:
True=成功送达, False=失败或无可用后端。
失败静默, 不抛异常 (通知是辅助通道, 不能阻断告警主流程)。
"""
title = _truncate(title)
message = _truncate(message)
if not title:
return False
backend = _detect_backend()
if backend is None:
return False
try:
if backend == "winotify":
return _notify_winotify(title, message)
if backend == "osascript":
return _notify_osascript(title, message)
if backend == "notify-send":
return _notify_notify_send(title, message, icon)
except Exception as e: # noqa: BLE001
logger.debug("系统通知失败 (%s): %s", backend, e)
return False
return False
def _notify_winotify(title: str, message: str) -> bool:
"""Windows 通知 (winotify) — 进现代操作中心。"""
from winotify import Notifier # type: ignore[import-not-found]
Notifier().create_notification(
title=title,
msg=message,
# winotify 要求 duration 为 "short" 或 "long"
duration="short",
# 无可点击动作 (桌面版不实现"点击回到窗口"的复杂交互)
).show()
return True
def _notify_osascript(title: str, message: str) -> bool:
"""macOS 通知 (osascript) — 调用系统 AppleScript。"""
# 转义双引号, 避免 AppleScript 注入
safe_title = title.replace('"', '\\"')
safe_msg = message.replace('"', '\\"')
script = (
f'display notification "{safe_msg}" with title "{safe_title}"'
)
result = subprocess.run( # noqa: S603, S607
["osascript", "-e", script],
capture_output=True,
timeout=5,
)
return result.returncode == 0
def _notify_notify_send(title: str, message: str, icon: Path | None) -> bool:
"""Linux 通知 (notify-send) — freedesktop.org 标准。"""
args = ["notify-send", title]
if icon and Path(icon).exists():
args.extend(["--icon", str(icon)])
args.append(message)
result = subprocess.run( # noqa: S603
args,
capture_output=True,
timeout=5,
)
return result.returncode == 0