advent22/api/advent22_api/config.py

52 lines
912 B
Python
Raw Normal View History

2023-09-07 16:44:44 +00:00
import tomllib
from typing import TypeAlias
import tomli_w
2022-11-04 18:49:31 +00:00
from pydantic import BaseModel
2023-09-07 16:44:44 +00:00
from .dav_common import dav_get_textfile_content, dav_write_textfile_content
2022-11-04 18:49:31 +00:00
from .settings import SETTINGS
2022-11-15 22:17:32 +00:00
class User(BaseModel):
name: str
password: str
2023-09-07 19:34:11 +00:00
class DoorSaved(BaseModel):
2023-09-07 16:44:44 +00:00
day: int
x1: int
y1: int
x2: int
y2: int
2023-09-07 19:34:11 +00:00
DoorsSaved: TypeAlias = list[DoorSaved]
class Puzzle(BaseModel):
background: str
doors: DoorsSaved = []
font: str
solution: str
2023-09-07 16:44:44 +00:00
2022-11-04 18:49:31 +00:00
class Config(BaseModel):
2022-11-15 22:17:32 +00:00
admin: User
2022-11-18 01:39:05 +00:00
puzzle: Puzzle
2022-11-04 18:49:31 +00:00
async def get_config() -> Config:
txt = await dav_get_textfile_content(path=SETTINGS.config_filename)
2023-09-07 16:44:44 +00:00
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,
)