Files
PDFReaderLite/pdfreaderlite/app.py
T

59 lines
1.7 KiB
Python
Raw Normal View History

2026-06-29 14:42:42 +08:00
from __future__ import annotations
import logging
2026-06-29 14:42:42 +08:00
import sys
from pathlib import Path
2026-06-29 14:42:42 +08:00
from PySide6.QtCore import QStandardPaths
2026-06-29 14:58:36 +08:00
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication, QMessageBox
2026-06-29 14:42:42 +08:00
from pdfreaderlite import __version__
2026-06-29 14:58:36 +08:00
from pdfreaderlite.resources import software_icon_path
2026-06-29 14:42:42 +08:00
from pdfreaderlite.ui.main_window import MainWindow
def configure_logging() -> Path:
log_dir = Path(
QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppDataLocation)
)
log_dir.mkdir(parents=True, exist_ok=True)
log_path = log_dir / "PDFReaderLite.log"
logging.basicConfig(
filename=log_path,
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
encoding="utf-8",
)
logging.info("PDFReaderLite %s started", __version__)
return log_path
2026-06-29 14:42:42 +08:00
def main() -> int:
app = QApplication(sys.argv)
app.setApplicationName("PDFReaderLite")
app.setOrganizationName("PDFReaderLite")
app.setApplicationVersion(__version__)
2026-06-29 14:58:36 +08:00
app.setWindowIcon(QIcon(software_icon_path().as_posix()))
log_path = configure_logging()
def handle_exception(exc_type, exc_value, exc_traceback) -> None:
logging.exception("Unhandled exception", exc_info=(exc_type, exc_value, exc_traceback))
QMessageBox.critical(
None,
"程序错误",
f"PDFReaderLite 遇到未处理错误。\n\n日志位置:{log_path}",
)
sys.excepthook = handle_exception
2026-06-29 14:42:42 +08:00
window = MainWindow()
window.show()
if len(sys.argv) > 1:
window.open_file(sys.argv[1])
2026-06-29 15:47:18 +08:00
else:
window.offer_restore_last_file()
2026-06-29 14:42:42 +08:00
return app.exec()