config-save-read.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. """仓库 ``save/`` 下 JSON 配置文件的读、写与按键合并(主文件为 ``project-config``)。"""
  3. from __future__ import annotations
  4. import json
  5. from pathlib import Path
  6. from typing import Any
  7. _REPO_ROOT = Path(__file__).resolve().parent.parent
  8. def save_directory() -> Path:
  9. return _REPO_ROOT / "save"
  10. def project_config_path() -> Path:
  11. return save_directory() / "project-config"
  12. def _legacy_input_keyword_config_file_path() -> Path:
  13. return save_directory() / "input-keyword.config"
  14. def read_input_keyword_config() -> dict[str, Any]:
  15. """返回进程内 ``RepositoryConfigIniSnapshot.project_config`` 同一可变字典,不重复读盘。"""
  16. from workplace.repository_config_snapshot import get_repository_config_ini_snapshot
  17. return get_repository_config_ini_snapshot().project_config
  18. def input_keyword_xy_pair_from_config(
  19. cfg: dict[str, Any],
  20. key: str,
  21. ) -> tuple[int, int] | None:
  22. """若 ``cfg[key]`` 为 ``[x, y]`` 整数对则返回 ``(x, y)``,否则 ``None``。"""
  23. v = cfg.get(key)
  24. if not isinstance(v, list) or len(v) != 2:
  25. return None
  26. return int(v[0]), int(v[1])
  27. def write_input_keyword_config(data: dict[str, Any]) -> None:
  28. """整体覆盖写入 ``save/project-config``。"""
  29. path = project_config_path()
  30. path.parent.mkdir(parents=True, exist_ok=True)
  31. path.write_text(
  32. json.dumps(data, ensure_ascii=False, indent=2) + "\n",
  33. encoding="utf-8",
  34. )
  35. def merge_input_keyword_config(
  36. *,
  37. ocr_search_xiaohongshu_xy: tuple[int, int] | None = None,
  38. clear_x_button_xy: tuple[int, int] | None = None,
  39. press_search_button_xy: tuple[int, int] | None = None,
  40. filter_drop_down_menu_pos: tuple[int, int] | None = None,
  41. business_cooperation_btn_pos: tuple[int, int] | None = None,
  42. likes_btn_pos: tuple[int, int] | None = None,
  43. comment_btn_pos: tuple[int, int] | None = None,
  44. collect_btn_pos: tuple[int, int] | None = None,
  45. picture_word_note_pos: tuple[int, int] | None = None,
  46. ) -> None:
  47. """读取现有配置后仅更新传入的键,再写回。"""
  48. data = read_input_keyword_config()
  49. if ocr_search_xiaohongshu_xy is not None:
  50. data["ocr_search_xiaohongshu_xy"] = list(ocr_search_xiaohongshu_xy)
  51. if clear_x_button_xy is not None:
  52. data["clear_x_button_xy"] = list(clear_x_button_xy)
  53. if press_search_button_xy is not None:
  54. data["press_search_button_xy"] = list(press_search_button_xy)
  55. if filter_drop_down_menu_pos is not None:
  56. data["filter-drop-down-menu-pos"] = list(filter_drop_down_menu_pos)
  57. if business_cooperation_btn_pos is not None:
  58. data["business-cooperation-btn-pos"] = list(business_cooperation_btn_pos)
  59. if likes_btn_pos is not None:
  60. data["likes-btn"] = list(likes_btn_pos)
  61. if comment_btn_pos is not None:
  62. data["comment-btn"] = list(comment_btn_pos)
  63. if collect_btn_pos is not None:
  64. data["collect-btn"] = list(collect_btn_pos)
  65. if picture_word_note_pos is not None:
  66. data["picture-word-note-pos"] = list(picture_word_note_pos)
  67. write_input_keyword_config(data)
  68. __all__ = [
  69. "save_directory",
  70. "project_config_path",
  71. "read_input_keyword_config",
  72. "input_keyword_xy_pair_from_config",
  73. "write_input_keyword_config",
  74. "merge_input_keyword_config",
  75. ]