advent22/api/advent22_api/config.py

51 lines
912 B
Python

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 DoorSaved(BaseModel):
day: int
x1: int
y1: int
x2: int
y2: int
DoorsSaved: TypeAlias = list[DoorSaved]
class Puzzle(BaseModel):
background: str
doors: DoorsSaved = []
font: str
solution: str
class Config(BaseModel):
admin: User
puzzle: Puzzle
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,
)