2025-01-31 13:50:07 +08:00
|
|
|
# config_manager.py
|
|
|
|
|
# -*- coding: utf-8 -*-
|
2025-01-29 20:33:20 +08:00
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
def load_config(config_file: str) -> dict:
|
|
|
|
|
"""从指定的 config_file 加载配置,若不存在则返回空字典。"""
|
|
|
|
|
if os.path.exists(config_file):
|
|
|
|
|
try:
|
|
|
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
def save_config(config_data: dict, config_file: str) -> bool:
|
|
|
|
|
"""将 config_data 保存到 config_file 中,返回 True/False 表示是否成功。"""
|
|
|
|
|
try:
|
|
|
|
|
with open(config_file, 'w', encoding='utf-8') as f:
|
|
|
|
|
json.dump(config_data, f, ensure_ascii=False, indent=4)
|
|
|
|
|
return True
|
|
|
|
|
except:
|
|
|
|
|
return False
|