advent22/api/advent22_api/config.py

52 lines
899 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
2022-11-18 01:39:05 +00:00
class Puzzle(BaseModel):
2023-09-04 21:23:27 +00:00
background: str
2023-09-04 21:42:58 +00:00
font: str
2022-11-18 01:39:05 +00:00
solution: str
2023-09-07 16:44:44 +00:00
class Door(BaseModel):
day: int
x1: int
y1: int
x2: int
y2: int
Doors: TypeAlias = list[Door] | None
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
2023-09-07 16:44:44 +00:00
doors: Doors = []
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,
)