advent22/api/advent22_api/calendar_config.py

53 lines
1.1 KiB
Python
Raw Normal View History

2023-09-08 00:56:14 +00:00
import tomllib
from typing import TypeAlias
import tomli_w
from pydantic import BaseModel
2023-09-08 19:59:26 +00:00
from .config import Config, get_config
2023-09-08 00:56:14 +00:00
from .dav_common import dav_get_textfile_content, dav_write_textfile_content
class DoorSaved(BaseModel):
# Tag, an dem die Tür aufgeht
day: int
# Koordinaten für zwei Eckpunkte
x1: int
y1: int
x2: int
y2: int
DoorsSaved: TypeAlias = list[DoorSaved]
class CalendarConfig(BaseModel):
# Dateiname Hintergrundbild
background: str = "adventskalender.jpg"
# Türen für die UI
doors: DoorsSaved = []
async def get_calendar_config() -> CalendarConfig:
cfg = await get_config()
txt = await dav_get_textfile_content(
path=f"files/{cfg.puzzle.calendar}",
)
return CalendarConfig.model_validate(tomllib.loads(txt))
2023-09-08 19:59:26 +00:00
async def set_calendar_config(cfg: Config, cal_cfg: CalendarConfig) -> None:
2023-09-08 00:56:14 +00:00
await dav_write_textfile_content(
2023-09-08 19:59:26 +00:00
path=f"files/{cfg.puzzle.calendar}",
2023-09-08 00:56:14 +00:00
content=tomli_w.dumps(
cal_cfg.model_dump(
exclude_defaults=True,
exclude_unset=True,
)
),
)