your-workflow/backend/app/core/config.py

31 lines
841 B
Python

import json
from pathlib import Path
import asyncio
CONFIG_PATH = Path(__file__).parent.parent / "config" / "init.json"
def _sync_load_config() -> dict:
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
try:
with CONFIG_PATH.open("r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
default_config = {"is_init": False}
_sync_save_config(default_config)
return default_config
def _sync_save_config(config_data: dict):
with CONFIG_PATH.open("w", encoding="utf-8") as f:
json.dump(config_data, f, indent=4, ensure_ascii=False)
# 异步接口
async def load_init_config() -> dict:
return await asyncio.to_thread(_sync_load_config)
async def save_init_config(config_data: dict):
await asyncio.to_thread(_sync_save_config, config_data)