| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #!/usr/bin/env python3
- """仓库根 ``config.ini`` 与 ``save/project-config`` 的一次性快照。"""
- from __future__ import annotations
- import configparser
- import json
- import sys
- from dataclasses import dataclass
- from pathlib import Path
- from typing import Any
- _SINGLETON_MODULE_SOURCE_FILE_PATH = Path(__file__).resolve()
- REPOSITORY_ROOT_DIRECTORY: Path = _SINGLETON_MODULE_SOURCE_FILE_PATH.parent.parent
- _repository_root_directory_string = str(REPOSITORY_ROOT_DIRECTORY)
- if _repository_root_directory_string not in sys.path:
- sys.path.insert(0, _repository_root_directory_string)
- _CONFIG_INI_FILE_PATH = REPOSITORY_ROOT_DIRECTORY / "config.ini"
- _PROJECT_CONFIG_PRIMARY_FILE_PATH = REPOSITORY_ROOT_DIRECTORY / "save" / "project-config"
- _PROJECT_CONFIG_LEGACY_FILE_PATH = REPOSITORY_ROOT_DIRECTORY / "save" / "input-keyword.config"
- @dataclass(frozen=True)
- class PyAutoGUIScreenCalibration:
- viewport_to_screen_scale_x: float = 1.0
- viewport_to_screen_scale_y: float = 1.0
- screen_offset_x: int = 0
- screen_offset_y: int = 0
- @dataclass(frozen=True)
- class RepositoryConfigIniSnapshot:
- home_page_url: str
- python_executable_relative_path: str
- pyautogui_screen_calibration: PyAutoGUIScreenCalibration
- project_config: dict[str, Any]
- _REPOSITORY_CONFIG_INI_SNAPSHOT_SINGLETON: RepositoryConfigIniSnapshot | None = None
- def _read_project_config_mapping_from_repository_save_directory() -> dict[str, Any]:
- if _PROJECT_CONFIG_PRIMARY_FILE_PATH.is_file():
- raw_text = _PROJECT_CONFIG_PRIMARY_FILE_PATH.read_text(encoding="utf-8")
- parsed_object = json.loads(raw_text)
- return parsed_object if isinstance(parsed_object, dict) else {}
- if _PROJECT_CONFIG_LEGACY_FILE_PATH.is_file():
- raw_text = _PROJECT_CONFIG_LEGACY_FILE_PATH.read_text(encoding="utf-8")
- parsed_object = json.loads(raw_text)
- return parsed_object if isinstance(parsed_object, dict) else {}
- return {}
- def _read_repository_config_ini_snapshot_from_disk() -> RepositoryConfigIniSnapshot:
- parser = configparser.ConfigParser()
- parser.read(_CONFIG_INI_FILE_PATH, encoding="utf-8")
- python_executable_relative_path = parser.get(
- "paths",
- "pythonRelative",
- fallback="python/py/python.exe",
- )
- home_page_url = parser.get(
- "urls",
- "homeUrl",
- fallback="https://www.xiaohongshu.com/",
- )
- pyautogui_screen_calibration = PyAutoGUIScreenCalibration()
- project_config_mapping = _read_project_config_mapping_from_repository_save_directory()
- return RepositoryConfigIniSnapshot(
- home_page_url=home_page_url.strip(),
- python_executable_relative_path=python_executable_relative_path.strip(),
- pyautogui_screen_calibration=pyautogui_screen_calibration,
- project_config=project_config_mapping,
- )
- def get_repository_config_ini_snapshot() -> RepositoryConfigIniSnapshot:
- global _REPOSITORY_CONFIG_INI_SNAPSHOT_SINGLETON
- if _REPOSITORY_CONFIG_INI_SNAPSHOT_SINGLETON is None:
- _REPOSITORY_CONFIG_INI_SNAPSHOT_SINGLETON = (
- _read_repository_config_ini_snapshot_from_disk()
- )
- return _REPOSITORY_CONFIG_INI_SNAPSHOT_SINGLETON
- __all__ = [
- "PyAutoGUIScreenCalibration",
- "RepositoryConfigIniSnapshot",
- "REPOSITORY_ROOT_DIRECTORY",
- "get_repository_config_ini_snapshot",
- ]
|