diff --git a/api/advent22_api/core/calendar_config.py b/api/advent22_api/core/calendar_config.py index 18949c9..9b3e618 100644 --- a/api/advent22_api/core/calendar_config.py +++ b/api/advent22_api/core/calendar_config.py @@ -5,7 +5,7 @@ import tomli_w from fastapi import Depends from pydantic import BaseModel -from .config import Config +from .config import Config, get_config from .webdav import WebDAV @@ -30,18 +30,7 @@ class CalendarConfig(BaseModel): # Türen für die UI doors: DoorsSaved = [] - @staticmethod - async def get_calendar_config( - cfg: Config = Depends(Config.get_config), - ) -> "CalendarConfig": - """ - Kalender Konfiguration lesen - """ - - txt = await WebDAV.read_str(path=f"files/{cfg.puzzle.calendar}") - return CalendarConfig.model_validate(tomllib.loads(txt)) - - async def set_calendar_config(self, cfg: Config) -> None: + async def change(self, cfg: Config) -> None: """ Kalender Konfiguration ändern """ @@ -55,3 +44,14 @@ class CalendarConfig(BaseModel): ) ), ) + + +async def get_calendar_config( + cfg: Config = Depends(get_config), +) -> CalendarConfig: + """ + Kalender Konfiguration lesen + """ + + txt = await WebDAV.read_str(path=f"files/{cfg.puzzle.calendar}") + return CalendarConfig.model_validate(tomllib.loads(txt)) diff --git a/api/advent22_api/core/config.py b/api/advent22_api/core/config.py index d4a2a4c..002cff2 100644 --- a/api/advent22_api/core/config.py +++ b/api/advent22_api/core/config.py @@ -44,11 +44,11 @@ class Config(BaseModel): server: Server puzzle: Puzzle - @staticmethod - async def get_config() -> "Config": - """ - Globale Konfiguration lesen - """ - txt = await WebDAV.read_str(path=SETTINGS.config_filename) - return Config.model_validate(tomllib.loads(txt)) +async def get_config() -> "Config": + """ + Globale Konfiguration lesen + """ + + txt = await WebDAV.read_str(path=SETTINGS.config_filename) + return Config.model_validate(tomllib.loads(txt)) diff --git a/api/advent22_api/core/depends.py b/api/advent22_api/core/depends.py index 6b814e1..cf1b9aa 100644 --- a/api/advent22_api/core/depends.py +++ b/api/advent22_api/core/depends.py @@ -5,14 +5,14 @@ from fastapi import Depends from PIL import Image, ImageFont from .advent_image import _XY, AdventImage -from .config import Config +from .config import Config, get_config from .image_helpers import list_images_auto, load_image from .sequence_helpers import Random, set_len, shuffle from .webdav import WebDAV async def shuffle_solution( - cfg: Config = Depends(Config.get_config), + cfg: Config = Depends(get_config), ) -> str: """ Lösung: Reihenfolge zufällig bestimmen @@ -56,7 +56,7 @@ async def get_random( async def gen_auto_image( day: int, auto_images: list[str] = Depends(shuffle_images_auto), - cfg: Config = Depends(Config.get_config), + cfg: Config = Depends(get_config), rnd: Random = Depends(get_random), part: str = Depends(get_part), ) -> Image.Image: @@ -87,7 +87,7 @@ async def gen_auto_image( async def get_image( day: int, auto_images: list[str] = Depends(shuffle_images_auto), - cfg: Config = Depends(Config.get_config), + cfg: Config = Depends(get_config), rnd: Random = Depends(get_random), part: str = Depends(get_part), ) -> Image.Image: diff --git a/api/advent22_api/core/sequence_helpers.py b/api/advent22_api/core/sequence_helpers.py index 049fc15..27fa454 100644 --- a/api/advent22_api/core/sequence_helpers.py +++ b/api/advent22_api/core/sequence_helpers.py @@ -2,13 +2,13 @@ import itertools import random from typing import Any, Self, Sequence -from .config import Config +from .config import get_config class Random(random.Random): @classmethod async def get(cls, bonus_salt: Any = "") -> Self: - cfg = await Config.get_config() + cfg = await get_config() return cls(f"{cfg.puzzle.solution}{cfg.puzzle.random_pepper}{bonus_salt}") diff --git a/api/advent22_api/routers/_security.py b/api/advent22_api/routers/_security.py index 1bfcbe2..e2c828e 100644 --- a/api/advent22_api/routers/_security.py +++ b/api/advent22_api/routers/_security.py @@ -4,14 +4,14 @@ from datetime import date from fastapi import Depends, HTTPException, status from fastapi.security import HTTPBasic, HTTPBasicCredentials -from ..core.config import Config +from ..core.config import Config, get_config security = HTTPBasic() async def user_is_admin( credentials: HTTPBasicCredentials = Depends(security), - cfg: Config = Depends(Config.get_config), + cfg: Config = Depends(get_config), ) -> bool: """ True iff der user "admin" ist diff --git a/api/advent22_api/routers/days.py b/api/advent22_api/routers/days.py index d74ae26..20d9d80 100644 --- a/api/advent22_api/routers/days.py +++ b/api/advent22_api/routers/days.py @@ -4,7 +4,7 @@ from fastapi import APIRouter, Depends, HTTPException, status from fastapi.responses import StreamingResponse from PIL import Image -from ..core.config import Config +from ..core.config import get_config from ..core.depends import get_image, get_part, shuffle_solution from ..core.image_helpers import api_return_image from ._security import user_can_view_door, user_is_admin, user_visible_doors @@ -14,7 +14,7 @@ router = APIRouter(prefix="/days", tags=["days"]) @router.on_event("startup") async def startup() -> None: - cfg = await Config.get_config() + cfg = await get_config() print(cfg.puzzle.solution) shuffled_solution = await shuffle_solution(cfg) diff --git a/api/advent22_api/routers/general.py b/api/advent22_api/routers/general.py index 4787624..556698b 100644 --- a/api/advent22_api/routers/general.py +++ b/api/advent22_api/routers/general.py @@ -1,8 +1,8 @@ from fastapi import APIRouter, Depends from fastapi.responses import StreamingResponse -from ..core.calendar_config import CalendarConfig, DoorsSaved -from ..core.config import Config +from ..core.calendar_config import CalendarConfig, DoorsSaved, get_calendar_config +from ..core.config import Config, get_config from ..core.image_helpers import api_return_image, load_image router = APIRouter(prefix="/general", tags=["general"]) @@ -13,7 +13,7 @@ router = APIRouter(prefix="/general", tags=["general"]) response_class=StreamingResponse, ) async def get_image_for_day( - cal_cfg: CalendarConfig = Depends(CalendarConfig.get_calendar_config), + cal_cfg: CalendarConfig = Depends(get_calendar_config), ) -> StreamingResponse: """ Hintergrundbild laden @@ -24,7 +24,7 @@ async def get_image_for_day( @router.get("/doors") async def get_doors( - cal_cfg: CalendarConfig = Depends(CalendarConfig.get_calendar_config), + cal_cfg: CalendarConfig = Depends(get_calendar_config), ) -> DoorsSaved: """ Türchen lesen @@ -36,8 +36,8 @@ async def get_doors( @router.put("/doors") async def put_doors( doors: DoorsSaved, - cfg: Config = Depends(Config.get_config), - cal_cfg: CalendarConfig = Depends(CalendarConfig.get_calendar_config), + cfg: Config = Depends(get_config), + cal_cfg: CalendarConfig = Depends(get_calendar_config), ) -> None: """ Türchen setzen @@ -47,4 +47,4 @@ async def put_doors( doors, key=lambda door: door.day, ) - await cal_cfg.set_calendar_config(cfg) + await cal_cfg.change(cfg)