Skip to content

文件:core/config.py

该模块负责配置与映射文件的读写、默认值生成与目录常量管理。

常量

  • BASE_DIR: 仓库根目录。
  • CONFIG_DIR: config/ 目录。
  • CONFIG_PATH: config/config.json
  • MAP_PATH: config/map.json
  • SERVICES_PATH: config/services.json
  • LEVELS_DIR: levels/
  • SERVICES_DIR: services/

函数定义与行为

_write_json(path: Path, data: dict) -> None

utf-8 写入 JSON 文件,ensure_ascii=False,缩进 2,末尾带换行。

_default_config() -> dict

返回默认配置:

  • _comment: 提示字段
  • secret_key: secrets.token_hex(32)
  • fingerprint_salt: secrets.token_hex(16)
  • token_expire_hours: 48
  • fingerprint_mode: strict
  • debug: False

_scan_levels() -> list[dict]

扫描 levels/ 目录,要求子目录包含 __init__.py。 返回结构:

json
{ "slug": "<module>", "module": "<module>", "name": "<module>", "enabled": true }

_default_map() -> dict

若扫描到关卡,生成对应 levels 列表,否则返回空列表。

load_config() -> dict

读取 CONFIG_PATH,若不存在则写入 _default_config()

save_config(config: dict) -> None

调用 _write_json 保存。

load_map() -> dict

读取 MAP_PATH,若不存在则写入 _default_map()

save_map(mapping: dict) -> None

调用 _write_json 保存。

map_levels() -> list[dict]

返回 load_map() 中的 levels 列表。

resolve_level_index(slug: str) -> int | None

遍历 map_levels(),返回匹配 slug 的索引。

next_level_slug(current_index: int) -> str | None

current_index + 1 对应关卡 slug。

load_services() -> dict

读取 SERVICES_PATH,不存在则返回 { "services": [] }

save_services(services: dict) -> None

调用 _write_json 保存。

map_services() -> list[dict]

返回 load_services() 中的 services 列表。