37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""源码运行与 PyInstaller 单文件运行共用的路径规则。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
PRODUCT_DATA_DIR = "ZhenYangTangRPA"
|
|
|
|
|
|
def is_frozen() -> bool:
|
|
return bool(getattr(sys, "frozen", False))
|
|
|
|
|
|
def application_data_dir() -> Path:
|
|
"""返回可长期写入的配置目录,避免单文件 EXE 重启后丢失数据。"""
|
|
if not is_frozen():
|
|
return Path(__file__).resolve().parent
|
|
local_app_data = os.environ.get("LOCALAPPDATA", "").strip()
|
|
base = Path(local_app_data) if local_app_data else Path.home() / "AppData" / "Local"
|
|
target = base / PRODUCT_DATA_DIR
|
|
target.mkdir(parents=True, exist_ok=True)
|
|
return target
|
|
|
|
|
|
def resource_dir() -> Path:
|
|
"""返回打包资源目录;单文件模式下指向 PyInstaller 临时展开目录。"""
|
|
bundle_dir = getattr(sys, "_MEIPASS", "")
|
|
return Path(bundle_dir).resolve() if bundle_dir else Path(__file__).resolve().parent
|
|
|
|
|
|
def resource_path(*parts: str) -> Path:
|
|
return resource_dir().joinpath(*parts)
|