Files
safe-marks-client/common/utils.py
2025-10-18 23:09:05 -04:00

68 lines
1.7 KiB
Python

import os
import sys
from pathlib import Path
from PySide6.QtWidgets import QWidget
from qfluentwidgets import (
InfoBarIcon, MessageBox,
)
# --- API 客户端配置 ---
BASE_URL = "https://safe-marks.oranj.work/api/v1/ext"
SAFE = [1, 0, -1, -2]
SAFE_MAP = {
1: "安全",
0: "未知",
-1: "不安全",
-2: "未记录"
}
# 反向映射,用于 QComboBox
SAFE_MAP_INV = {v: k for k, v in SAFE_MAP.items()}
SAFE_MAP_ICON = {
1: InfoBarIcon.SUCCESS.icon(),
0: InfoBarIcon.WARNING.icon(),
-1: InfoBarIcon.ERROR.icon(),
-2: InfoBarIcon.INFORMATION.icon(),
}
class APIException(Exception):
pass
def show_quick_tip(widget: QWidget, caption: str, text: str):
mb = MessageBox(caption, text, widget)
mb.cancelButton.setHidden(True)
mb.buttonLayout.insertStretch(0, 1)
mb.buttonLayout.setStretch(1, 0)
mb.yesButton.setMinimumWidth(100)
mb.setClosableOnMaskClicked(True)
mb.exec()
def accept_warning(widget: QWidget, condition: bool,
caption: str = "警告", text: str = "你确定要继续吗?") -> bool:
if condition:
mb = MessageBox(caption, text, widget)
if not mb.exec():
return True
return False
def get_log_dir() -> str | None:
if sys.platform == "win32":
log_dir = Path(os.path.expanduser("~"), "AppData", "Roaming")
elif sys.platform == "darwin":
log_dir = Path(os.path.expanduser("~"), "Library", "Application Support")
else:
return None
if not log_dir.exists():
log_dir.mkdir(parents=True, exist_ok=True)
return str(log_dir)
def get_app_dir(org_name: str, app_name: str):
app_dir = Path(get_log_dir(), org_name, app_name)
app_dir.mkdir(parents=True, exist_ok=True)
return str(app_dir)