-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (54 loc) · 1.86 KB
/
Copy pathmain.py
File metadata and controls
70 lines (54 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""OpenCode Go 使用记录监控 — 程序入口
用法:
python main.py
打包版(PyInstaller --windowed)无控制台,任何未捕获异常都会写入
%APPDATA%/OCGMonitor/error.log,便于排查。
"""
import os
import sys
import traceback
def _log_crash(exc: BaseException):
try:
base = os.environ.get("APPDATA") or os.path.expanduser("~")
d = os.path.join(base, "OCGMonitor")
os.makedirs(d, exist_ok=True)
with open(os.path.join(d, "error.log"), "a", encoding="utf-8") as f:
f.write("---- %s ----\n" % __import__("datetime").datetime.now().isoformat())
traceback.print_exception(type(exc), exc, exc.__traceback__, file=f)
except Exception:
pass
def main():
try:
_run()
except Exception as e:
_log_crash(e)
raise
def _run():
from PySide6.QtWidgets import QApplication
from ocgmon import APP_TITLE, APP_VERSION
from ocgmon.config import Settings, appdata_dir
from ocgmon.db import Database
from ocgmon.main_window import MainWindow, make_app_icon
from ocgmon.theme import build_stylesheet
os.makedirs(appdata_dir(), exist_ok=True)
app = QApplication(sys.argv)
app.setApplicationName(APP_TITLE)
app.setApplicationVersion(APP_VERSION)
app.setWindowIcon(make_app_icon())
settings = Settings()
# 数据库路径固定为 %APPDATA%/OCGMonitor/monitor.db(Linux 为 XDG 数据目录)
from ocgmon.config import db_path
db = Database(db_path())
# 主题(跟随系统时用原生样式)
theme = settings.get("theme", "dark")
if theme != "system":
app.setStyleSheet(build_stylesheet(theme))
win = MainWindow(db, settings)
win.show()
exit_code = app.exec()
db.close()
sys.exit(exit_code)
if __name__ == "__main__":
main()