advent22/api/advent22_api/calendar_config.py

54 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
from .config import get_config
from .dav_common import dav_get_textfile_content, dav_write_textfile_content
from .settings import SETTINGS
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))
async def set_calendar_config(cal_cfg: CalendarConfig) -> None:
await dav_write_textfile_content(
path=SETTINGS.config_filename,
content=tomli_w.dumps(
cal_cfg.model_dump(
exclude_defaults=True,
exclude_unset=True,
)
),
)