singleton.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. """
  3. 本仓库进程级单例:仓库根目录、对 ``config.ini`` 的一次性解析快照,以及由快照派生的常用路径与 URL。
  4. 其它脚本可在仓库根已加入 ``sys.path`` 后 ``from workplace.singleton import …``,
  5. 或通过 ``workplace.main`` 再导出的别名引用。本模块在首次导入时若根目录不在 ``sys.path`` 会自行插入。
  6. """
  7. from __future__ import annotations
  8. import sys
  9. from pathlib import Path
  10. # 由本文件位置解析仓库根:workplace/singleton.py → 上级上级为项目根
  11. _SINGLETON_MODULE_SOURCE_FILE_PATH = Path(__file__).resolve()
  12. REPOSITORY_ROOT_DIRECTORY: Path = _SINGLETON_MODULE_SOURCE_FILE_PATH.parent.parent
  13. # 保证可 ``import workplace.*``(例如直接跑 ``workplace/main.py`` 时首项 path 常为 workplace 目录)
  14. _repository_root_directory_string = str(REPOSITORY_ROOT_DIRECTORY)
  15. if _repository_root_directory_string not in sys.path:
  16. sys.path.insert(0, _repository_root_directory_string)
  17. from workplace.repository_config_snapshot import ( # noqa: E402
  18. PyAutoGUIScreenCalibration,
  19. RepositoryConfigIniSnapshot,
  20. get_repository_config_ini_snapshot,
  21. )
  22. # ``config.ini`` 与 ``save/project-config`` 只解析一次,全进程共用(见 ``repository_config_snapshot``)
  23. REPOSITORY_CONFIG_INI_SNAPSHOT: RepositoryConfigIniSnapshot = (
  24. get_repository_config_ini_snapshot()
  25. )
  26. # 以下为快照字段及根目录拼接结果,便于零散导入而不必再读配置
  27. HOME_PAGE_URL: str = REPOSITORY_CONFIG_INI_SNAPSHOT.home_page_url
  28. PYTHON_EXECUTABLE_RELATIVE_PATH: str = (
  29. REPOSITORY_CONFIG_INI_SNAPSHOT.python_executable_relative_path
  30. )
  31. PYTHON_EXECUTABLE_ABSOLUTE_PATH: Path = (
  32. REPOSITORY_ROOT_DIRECTORY / PYTHON_EXECUTABLE_RELATIVE_PATH
  33. )
  34. PYAUTOGUI_SCREEN_CALIBRATION: PyAutoGUIScreenCalibration = (
  35. REPOSITORY_CONFIG_INI_SNAPSHOT.pyautogui_screen_calibration
  36. )
  37. __all__ = [
  38. "REPOSITORY_ROOT_DIRECTORY",
  39. "REPOSITORY_CONFIG_INI_SNAPSHOT",
  40. "RepositoryConfigIniSnapshot",
  41. "PyAutoGUIScreenCalibration",
  42. "HOME_PAGE_URL",
  43. "PYTHON_EXECUTABLE_RELATIVE_PATH",
  44. "PYTHON_EXECUTABLE_ABSOLUTE_PATH",
  45. "PYAUTOGUI_SCREEN_CALIBRATION",
  46. ]