2023-09-12 13:50:02 +00:00
|
|
|
import itertools
|
|
|
|
import random
|
2023-09-08 18:17:18 +00:00
|
|
|
import re
|
2023-09-14 23:36:20 +00:00
|
|
|
from datetime import date, datetime, timedelta
|
2023-09-08 18:17:18 +00:00
|
|
|
from io import BytesIO
|
2023-09-22 23:45:27 +00:00
|
|
|
from typing import Any, Iterable, Self, Sequence, TypeVar
|
2023-09-08 18:17:18 +00:00
|
|
|
|
|
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
from .config import get_config
|
2023-10-29 16:08:16 +00:00
|
|
|
from .dav.webdav import WebDAV
|
2023-09-08 18:17:18 +00:00
|
|
|
|
2023-09-12 13:50:02 +00:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
|
class Random(random.Random):
|
|
|
|
@classmethod
|
|
|
|
async def get(cls, bonus_salt: Any = "") -> Self:
|
|
|
|
cfg = await get_config()
|
2023-10-31 19:18:18 +00:00
|
|
|
return cls(f"{cfg.solution.clean}{cfg.random_seed}{bonus_salt}")
|
2023-09-12 13:50:02 +00:00
|
|
|
|
|
|
|
def shuffled(self, population: Sequence[T]) -> Sequence[T]:
|
|
|
|
return self.sample(population, k=len(population))
|
|
|
|
|
|
|
|
|
|
|
|
def set_len(seq: Sequence[T], len: int) -> Sequence[T]:
|
|
|
|
# `seq` unendlich wiederholen
|
|
|
|
infinite = itertools.cycle(seq)
|
|
|
|
|
|
|
|
# Die ersten `length` einträge nehmen
|
|
|
|
return list(itertools.islice(infinite, len))
|
|
|
|
|
2023-09-08 18:17:18 +00:00
|
|
|
|
2023-09-22 23:45:27 +00:00
|
|
|
def spread(
|
|
|
|
given: Iterable[int],
|
|
|
|
n: int,
|
|
|
|
rnd: Random | None = None,
|
|
|
|
) -> list[int]:
|
|
|
|
"""
|
|
|
|
Zu `given` ganzen Zahlen `n` zusätzliche Zahlen hinzunehmen.
|
|
|
|
|
|
|
|
- Die neuen Werte sind im selben Zahlenbereich wie `given`
|
|
|
|
- Zuerst werden alle Werte "zwischen" den `given` Werten genommen
|
|
|
|
"""
|
|
|
|
|
|
|
|
if n == 0:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if len(set(given)) > 1:
|
|
|
|
range_given = range(min(given), max(given) + 1)
|
|
|
|
first_round = set(range_given) - set(given)
|
|
|
|
|
|
|
|
elif len(set(given)) == 1:
|
|
|
|
if (a := next(iter(given))) > 0:
|
|
|
|
range_given = range(1, a + 1)
|
|
|
|
else:
|
|
|
|
range_given = range(1, n + 1)
|
|
|
|
|
|
|
|
first_round = set(range_given) - set(given)
|
|
|
|
|
|
|
|
else:
|
|
|
|
range_given = range(1, n + 1)
|
|
|
|
first_round = range_given
|
|
|
|
|
|
|
|
result = sorted(first_round)[: min(n, len(first_round))]
|
|
|
|
|
|
|
|
full_rounds = (n - len(result)) // len(range_given)
|
|
|
|
result += list(range_given) * full_rounds
|
|
|
|
|
|
|
|
remain = n - len(result)
|
|
|
|
if rnd is None:
|
|
|
|
result += list(range_given)[:remain]
|
|
|
|
|
|
|
|
else:
|
|
|
|
result += rnd.sample(range_given, remain)
|
|
|
|
rnd.shuffle(result)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-09-08 18:17:18 +00:00
|
|
|
async def list_images_auto() -> list[str]:
|
|
|
|
"""
|
|
|
|
Finde alle Bilddateien im "automatisch"-Verzeichnis
|
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
__DIR = "/images_auto"
|
|
|
|
|
|
|
|
return [
|
|
|
|
f"{__DIR}/{file}"
|
|
|
|
for file in await WebDAV.list_files(
|
|
|
|
directory=__DIR,
|
|
|
|
regex=re.compile(r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE),
|
|
|
|
)
|
|
|
|
]
|
2023-09-08 18:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def load_image(file_name: str) -> Image.Image:
|
|
|
|
"""
|
|
|
|
Versuche, Bild aus Datei zu laden
|
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
if not await WebDAV.exists(file_name):
|
2023-09-08 18:17:18 +00:00
|
|
|
raise RuntimeError(f"DAV-File {file_name} does not exist!")
|
|
|
|
|
2023-09-08 19:08:13 +00:00
|
|
|
return Image.open(BytesIO(await WebDAV.read_bytes(file_name)))
|
2023-09-08 18:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def api_return_image(img: Image.Image) -> StreamingResponse:
|
|
|
|
"""
|
|
|
|
Bild mit API zurückgeben
|
|
|
|
"""
|
|
|
|
|
|
|
|
# JPEG-Daten in Puffer speichern
|
|
|
|
img_buffer = BytesIO()
|
|
|
|
img.save(img_buffer, format="JPEG", quality=85)
|
|
|
|
img_buffer.seek(0)
|
|
|
|
|
|
|
|
# zurückgeben
|
|
|
|
return StreamingResponse(
|
|
|
|
media_type="image/jpeg",
|
|
|
|
content=img_buffer,
|
|
|
|
)
|
2023-09-14 23:36:20 +00:00
|
|
|
|
|
|
|
|
2023-09-20 14:25:10 +00:00
|
|
|
class EventDates:
|
2023-09-19 16:49:10 +00:00
|
|
|
"""
|
|
|
|
Events in einem Ereigniszeitraum
|
|
|
|
"""
|
|
|
|
|
|
|
|
__overall_duration: timedelta
|
2023-09-21 11:26:02 +00:00
|
|
|
dates: dict[int, date]
|
2023-09-19 16:49:10 +00:00
|
|
|
|
2023-09-20 14:25:10 +00:00
|
|
|
@property
|
2023-09-19 16:49:10 +00:00
|
|
|
def first(self) -> date:
|
|
|
|
"""Datum des ersten Ereignisses"""
|
2023-09-21 11:26:02 +00:00
|
|
|
return self.dates[min(self.dates.keys())]
|
2023-09-19 16:49:10 +00:00
|
|
|
|
2023-09-20 14:25:10 +00:00
|
|
|
def get_next(self, *, today: date) -> date | None:
|
|
|
|
"""Datum des nächsten Ereignisses"""
|
2023-09-21 11:26:02 +00:00
|
|
|
return next(
|
|
|
|
(event for event in sorted(self.dates.values()) if event > today), None
|
|
|
|
)
|
2023-09-20 14:25:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def next(self) -> date | None:
|
|
|
|
"""Datum des nächsten Ereignisses"""
|
|
|
|
return self.get_next(today=datetime.today().date())
|
|
|
|
|
|
|
|
@property
|
2023-09-19 16:49:10 +00:00
|
|
|
def last(self) -> date:
|
|
|
|
"""Datum des letzten Ereignisses"""
|
2023-09-21 11:26:02 +00:00
|
|
|
return self.dates[max(self.dates.keys())]
|
2023-09-19 16:49:10 +00:00
|
|
|
|
2023-09-20 14:25:10 +00:00
|
|
|
@property
|
2023-09-19 16:49:10 +00:00
|
|
|
def end(self) -> date:
|
|
|
|
"""Letztes Datum des Ereigniszeitraums"""
|
2023-09-20 14:25:10 +00:00
|
|
|
return self.first + self.__overall_duration
|
2023-09-19 16:49:10 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
*,
|
|
|
|
# current date
|
|
|
|
today: date,
|
|
|
|
# month/day when events begin
|
|
|
|
begin_month: int,
|
|
|
|
begin_day: int,
|
2023-09-20 14:25:10 +00:00
|
|
|
# events: e.g. a 2 means there is an event on the 2nd day
|
|
|
|
# i.e. 1 day after begin
|
|
|
|
# - assume sorted (ascending)
|
|
|
|
events: list[int],
|
2023-09-19 16:49:10 +00:00
|
|
|
# countdown to closing begins after last event
|
2023-09-20 16:14:58 +00:00
|
|
|
close_after: int,
|
2023-09-19 16:49:10 +00:00
|
|
|
) -> None:
|
|
|
|
# account for the last event, then add closing period
|
2023-09-20 16:14:58 +00:00
|
|
|
self.__overall_duration = timedelta(days=events[-1] - 1 + close_after)
|
2023-09-19 16:49:10 +00:00
|
|
|
|
|
|
|
# the events may begin last year, this year or next year
|
|
|
|
maybe_begin = (
|
|
|
|
datetime(today.year + year_diff, begin_month, begin_day).date()
|
|
|
|
for year_diff in (-1, 0, +1)
|
|
|
|
)
|
|
|
|
|
|
|
|
# find the first begin where the end date is in the future
|
|
|
|
begin = next(
|
|
|
|
begin for begin in maybe_begin if today <= (begin + self.__overall_duration)
|
|
|
|
)
|
|
|
|
|
|
|
|
# all event dates
|
2023-09-21 11:26:02 +00:00
|
|
|
self.dates = {event: begin + timedelta(days=event - 1) for event in events}
|