This repository has been archived on 2026-03-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
AIMisc/code-quality-system/hooks/notification-sender.py
T
2026-01-30 00:42:33 +08:00

142 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""
通知发送器
发送 Windows 桌面通知和播放声音
"""
import json
import sys
import os
import platform
from pathlib import Path
def send_notification(title, message, icon_path=None, duration=10):
"""发送 Windows 桌面通知"""
try:
from plyer import notification
notification.notify(
title=title,
message=message,
app_name='Claude Code',
app_icon=icon_path,
timeout=duration
)
return True
except ImportError:
# 如果 plyer 不可用,尝试使用 Windows 原生 API
try:
import win10toast
toaster = win10toast.ToastNotifier()
toaster.show_toast(
title,
message,
icon_path=icon_path,
duration=duration
)
return True
except ImportError:
# 最后的回退:输出到 stderr(会显示在终端)
print(f'\n📢 {title}: {message}', file=sys.stderr)
return False
except Exception as e:
print(f'\n📢 {title}: {message}', file=sys.stderr)
return False
def play_sound(sound_type='success', volume=0.7):
"""播放提示音"""
sound_files = {
'success': 'success.wav',
'warning': 'warning.wav',
'attention': 'attention.wav'
}
sound_file = sound_files.get(sound_type, 'success.wav')
# 获取声音文件路径
script_dir = Path(__file__).parent
sounds_dir = script_dir.parent / 'sounds'
sound_path = sounds_dir / sound_file
try:
# 尝试使用 pygame
import pygame
pygame.mixer.init()
pygame.mixer.music.set_volume(volume)
pygame.mixer.music.load(str(sound_path))
pygame.mixer.music.play()
return True
except ImportError:
# 如果 pygame 不可用,尝试系统 beep
try:
import winsound
if sound_type == 'success':
winsound.Beep(800, 200)
elif sound_type == 'warning':
winsound.Beep(400, 300)
else:
winsound.Beep(600, 400)
return True
except Exception:
# 最后的回退:终端提示
return False
except Exception:
return False
def notify_check_result(check_result):
"""根据检查结果发送通知"""
if check_result.get('passed'):
send_notification(
title='✅ 代码质量检查通过',
message='所有功能已实现,代码规范符合要求',
duration=5
)
play_sound('success')
elif check_result.get('requires_review'):
send_notification(
title='🔔 需要深度审查',
message='自动修复已达上限,请查看详细报告',
duration=15
)
play_sound('attention')
else:
issue_count = check_result.get('issue_count', 0)
send_notification(
title='⚠️ 代码质量检查未通过',
message=f'发现 {issue_count} 个问题需要修复',
duration=10
)
play_sound('warning')
def main():
"""主函数 - 可以独立运行或作为模块导入"""
# 从 stdin 或命令行参数读取
if len(sys.argv) > 1:
# 命令行模式
if sys.argv[1] == 'test':
# 测试通知
send_notification('测试通知', '代码质量保障系统正在运行')
play_sound('success')
elif sys.argv[1] == 'success':
send_notification('✅ 检查通过', '所有功能已实现,代码规范符合要求')
play_sound('success')
elif sys.argv[1] == 'warning':
send_notification('⚠️ 检查未通过', '发现问题需要修复')
play_sound('warning')
elif sys.argv[1] == 'attention':
send_notification('🔔 需要审查', '请查看详细报告')
play_sound('attention')
else:
# 从 stdin 读取 JSON
try:
input_data = json.load(sys.stdin)
notify_check_result(input_data)
except json.JSONDecodeError:
pass
if __name__ == '__main__':
main()