| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #!/usr/bin/env python3
- """仓库 ``save/`` 下 JSON 配置文件的读、写与按键合并(主文件为 ``project-config``)。"""
- from __future__ import annotations
- import json
- from pathlib import Path
- from typing import Any
- _REPO_ROOT = Path(__file__).resolve().parent.parent
- def save_directory() -> Path:
- return _REPO_ROOT / "save"
- def project_config_path() -> Path:
- return save_directory() / "project-config"
- def _legacy_input_keyword_config_file_path() -> Path:
- return save_directory() / "input-keyword.config"
- def read_input_keyword_config() -> dict[str, Any]:
- """返回进程内 ``RepositoryConfigIniSnapshot.project_config`` 同一可变字典,不重复读盘。"""
- from workplace.repository_config_snapshot import get_repository_config_ini_snapshot
- return get_repository_config_ini_snapshot().project_config
- def input_keyword_xy_pair_from_config(
- cfg: dict[str, Any],
- key: str,
- ) -> tuple[int, int] | None:
- """若 ``cfg[key]`` 为 ``[x, y]`` 整数对则返回 ``(x, y)``,否则 ``None``。"""
- v = cfg.get(key)
- if not isinstance(v, list) or len(v) != 2:
- return None
- return int(v[0]), int(v[1])
- def write_input_keyword_config(data: dict[str, Any]) -> None:
- """整体覆盖写入 ``save/project-config``。"""
- path = project_config_path()
- path.parent.mkdir(parents=True, exist_ok=True)
- path.write_text(
- json.dumps(data, ensure_ascii=False, indent=2) + "\n",
- encoding="utf-8",
- )
- def merge_input_keyword_config(
- *,
- ocr_search_xiaohongshu_xy: tuple[int, int] | None = None,
- clear_x_button_xy: tuple[int, int] | None = None,
- press_search_button_xy: tuple[int, int] | None = None,
- filter_drop_down_menu_pos: tuple[int, int] | None = None,
- business_cooperation_btn_pos: tuple[int, int] | None = None,
- likes_btn_pos: tuple[int, int] | None = None,
- comment_btn_pos: tuple[int, int] | None = None,
- collect_btn_pos: tuple[int, int] | None = None,
- picture_word_note_pos: tuple[int, int] | None = None,
- ) -> None:
- """读取现有配置后仅更新传入的键,再写回。"""
- data = read_input_keyword_config()
- if ocr_search_xiaohongshu_xy is not None:
- data["ocr_search_xiaohongshu_xy"] = list(ocr_search_xiaohongshu_xy)
- if clear_x_button_xy is not None:
- data["clear_x_button_xy"] = list(clear_x_button_xy)
- if press_search_button_xy is not None:
- data["press_search_button_xy"] = list(press_search_button_xy)
- if filter_drop_down_menu_pos is not None:
- data["filter-drop-down-menu-pos"] = list(filter_drop_down_menu_pos)
- if business_cooperation_btn_pos is not None:
- data["business-cooperation-btn-pos"] = list(business_cooperation_btn_pos)
- if likes_btn_pos is not None:
- data["likes-btn"] = list(likes_btn_pos)
- if comment_btn_pos is not None:
- data["comment-btn"] = list(comment_btn_pos)
- if collect_btn_pos is not None:
- data["collect-btn"] = list(collect_btn_pos)
- if picture_word_note_pos is not None:
- data["picture-word-note-pos"] = list(picture_word_note_pos)
- write_input_keyword_config(data)
- __all__ = [
- "save_directory",
- "project_config_path",
- "read_input_keyword_config",
- "input_keyword_xy_pair_from_config",
- "write_input_keyword_config",
- "merge_input_keyword_config",
- ]
|