import tomllib from typing import TypeAlias import tomli_w from pydantic import BaseModel from .dav_common import dav_get_textfile_content, dav_write_textfile_content from .settings import SETTINGS class User(BaseModel): name: str password: str class Puzzle(BaseModel): background: str font: str solution: str class Door(BaseModel): day: int x1: int y1: int x2: int y2: int Doors: TypeAlias = list[Door] | None class Config(BaseModel): admin: User puzzle: Puzzle doors: Doors = [] async def get_config() -> Config: txt = await dav_get_textfile_content(path=SETTINGS.config_filename) return Config.model_validate(tomllib.loads(txt)) async def set_config(cfg: Config) -> None: txt = tomli_w.dumps(cfg.model_dump()) await dav_write_textfile_content( path=SETTINGS.config_filename, content=txt, )