139 lines
5.0 KiB
Python
139 lines
5.0 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
os.environ.setdefault(
|
|
"QTWEBENGINE_CHROMIUM_FLAGS",
|
|
"--disable-gpu --disable-gpu-compositing --disable-software-rasterizer --log-level=3",
|
|
)
|
|
os.environ.setdefault(
|
|
"QT_LOGGING_RULES",
|
|
"qt.multimedia.ffmpeg.*=false;qt.multimedia.*.debug=false;qt.multimedia.*.info=false",
|
|
)
|
|
|
|
from PySide6.QtCore import QSettings, Qt
|
|
from PySide6.QtGui import QIcon
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
from . import __version__
|
|
from .doc_check import main as check_documents_main
|
|
from .logging_config import setup_logging
|
|
from .main_window import MainWindow
|
|
|
|
|
|
def parse_arguments(argv: list[str]) -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(description="MDHelper 本地 Markdown 帮助文档阅读器")
|
|
parser.add_argument("path", nargs="?", help="启动后打开的文件或文件夹;可传相对文件夹名")
|
|
parser.add_argument("--check", action="store_true", help="检查 sample.md 和 docs/ 中缺失的本地链接/媒体")
|
|
parser.add_argument("--project-root", "--folder", dest="project_root", default=None, help="项目根目录,默认使用最近打开的文件夹")
|
|
return parser.parse_args(argv)
|
|
|
|
|
|
def runtime_paths() -> tuple[Path, Path]:
|
|
if getattr(sys, "frozen", False):
|
|
content_root = Path(getattr(sys, "_MEIPASS")).resolve()
|
|
log_root = Path(sys.executable).resolve().parent
|
|
return content_root, log_root
|
|
|
|
root = Path(__file__).resolve().parent.parent
|
|
return root, root
|
|
|
|
|
|
def resolve_launch_path(value: str | None, search_roots: tuple[Path, ...]) -> Path | None:
|
|
if not value:
|
|
return None
|
|
|
|
raw_path = Path(value).expanduser()
|
|
candidates = [raw_path]
|
|
if not raw_path.is_absolute():
|
|
candidates.extend(root / raw_path for root in search_roots)
|
|
|
|
for candidate in candidates:
|
|
if candidate.exists():
|
|
return candidate.resolve()
|
|
return raw_path.resolve()
|
|
|
|
|
|
def project_and_initial_path(launch_path: Path | None, project_root: Path | None) -> tuple[Path | None, Path | None]:
|
|
if project_root is not None:
|
|
return project_root, launch_path if launch_path is not None and launch_path.is_file() else None
|
|
if launch_path is None:
|
|
return None, None
|
|
if launch_path.is_file():
|
|
return launch_path.parent, launch_path
|
|
return launch_path, None
|
|
|
|
|
|
def configure_process_runtime(log_root: Path) -> Path:
|
|
runtime_root = Path(tempfile.gettempdir()) / "MDHelper" / "runtime" / str(os.getpid())
|
|
user_data_dir = runtime_root / "webengine"
|
|
cache_dir = runtime_root / "cache"
|
|
user_data_dir.mkdir(parents=True, exist_ok=True)
|
|
cache_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
flags = os.environ.get("QTWEBENGINE_CHROMIUM_FLAGS", "")
|
|
additions = [
|
|
f"--user-data-dir={user_data_dir.as_posix()}",
|
|
f"--disk-cache-dir={cache_dir.as_posix()}",
|
|
]
|
|
for addition in additions:
|
|
prefix = addition.split("=", 1)[0]
|
|
if prefix not in flags:
|
|
flags = f"{flags} {addition}".strip()
|
|
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = flags
|
|
os.environ.setdefault("MDHELPER_PROCESS_RUNTIME_DIR", str(runtime_root))
|
|
logging.getLogger(__name__).debug("Process runtime root: %s; log root: %s", runtime_root, log_root)
|
|
return runtime_root
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
args = parse_arguments(list(sys.argv[1:] if argv is None else argv))
|
|
content_root, log_root = runtime_paths()
|
|
search_roots = (Path.cwd(), log_root, content_root)
|
|
cli_launch_path = resolve_launch_path(args.path, search_roots)
|
|
cli_project_root = resolve_launch_path(args.project_root, search_roots)
|
|
project_root, cli_initial_path = project_and_initial_path(cli_launch_path, cli_project_root)
|
|
if args.check:
|
|
return check_documents_main([str(project_root or content_root)])
|
|
|
|
QApplication.setAttribute(Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings, True)
|
|
configure_process_runtime(log_root)
|
|
setup_logging(log_root)
|
|
|
|
app = QApplication([sys.argv[0]])
|
|
app.setApplicationName("MDHelper")
|
|
app.setApplicationVersion(__version__)
|
|
app.setOrganizationName("MDHelper")
|
|
settings = QSettings("MDHelper", "MDHelper")
|
|
if project_root is None:
|
|
last_project_root = Path(str(settings.value("project/last_root", ""))).expanduser()
|
|
if str(last_project_root) and last_project_root.exists():
|
|
project_root = last_project_root.resolve()
|
|
|
|
logging.getLogger(__name__).info(
|
|
"Starting MDHelper %s with project_root=%s content_root=%s log_root=%s",
|
|
__version__,
|
|
project_root,
|
|
content_root,
|
|
log_root,
|
|
)
|
|
|
|
icon_path = content_root / "icon_glass.png"
|
|
if icon_path.exists():
|
|
app.setWindowIcon(QIcon(str(icon_path)))
|
|
|
|
window = MainWindow(
|
|
project_root=project_root,
|
|
initial_path=cli_initial_path,
|
|
app_root=content_root,
|
|
prompt_for_project=project_root is None,
|
|
)
|
|
window.show()
|
|
|
|
return app.exec()
|