fix: better use of images_manual

This commit is contained in:
Jörn-Michael Miehe 2024-11-26 14:44:47 +01:00
parent fbecae5d53
commit 5cf1c8b7ee

View file

@ -100,21 +100,33 @@ async def get_all_auto_image_names(
return dict(zip(days, rnd.shuffled(ls))) return dict(zip(days, rnd.shuffled(ls)))
async def get_all_manual_image_names(
manual_image_names: list[str] = Depends(list_images_manual),
) -> dict[int, str]:
"""
Bilder: "manual" zuordnen
"""
num_re = re.compile(r"/(\d+)\.", flags=re.IGNORECASE)
return {
int(num_match.group(1)): name
for name in manual_image_names
if (num_match := num_re.search(name)) is not None
}
async def get_all_image_names( async def get_all_image_names(
auto_image_names: dict[int, str] = Depends(get_all_auto_image_names), auto_image_names: dict[int, str] = Depends(get_all_auto_image_names),
manual_image_names: list[str] = Depends(list_images_manual), manual_image_names: dict[int, str] = Depends(get_all_manual_image_names),
) -> dict[int, str]: ) -> dict[int, str]:
""" """
Bilder "auto" und "manual" zu Tagen zuordnen Bilder "auto" und "manual" zu Tagen zuordnen
""" """
num_re = re.compile(r"/(\d+)\.", flags=re.IGNORECASE) result = auto_image_names.copy()
result.update(manual_image_names)
for name in manual_image_names: return result
assert (num_match := num_re.search(name)) is not None
auto_image_names[int(num_match.group(1))] = name
return auto_image_names
@dataclass(slots=True, frozen=True) @dataclass(slots=True, frozen=True)
@ -184,6 +196,7 @@ async def get_day_image(
day: int, day: int,
days: list[int] = Depends(get_all_sorted_days), days: list[int] = Depends(get_all_sorted_days),
cfg: Config = Depends(get_config), cfg: Config = Depends(get_config),
manual_image_names: dict[int, str] = Depends(get_all_manual_image_names),
auto_image_names: dict[int, str] = Depends(get_all_auto_image_names), auto_image_names: dict[int, str] = Depends(get_all_auto_image_names),
day_parts: dict[int, str] = Depends(get_all_parts), day_parts: dict[int, str] = Depends(get_all_parts),
ttfonts: list[TTFont] = Depends(get_all_ttfonts), ttfonts: list[TTFont] = Depends(get_all_ttfonts),
@ -196,14 +209,14 @@ async def get_day_image(
return None return None
try: try:
# Versuche, aus "manual"-Ordner zu laden # Versuche "manual"-Bild zu laden
img = await load_image(f"images_manual/{day}.jpg") img = await load_image(manual_image_names[day])
# Als AdventImage verarbeiten # Als AdventImage verarbeiten
image = await AdventImage.from_img(img, cfg) image = await AdventImage.from_img(img, cfg)
return image.img return image.img
except RuntimeError: except (KeyError, RuntimeError):
# Erstelle automatisch generiertes Bild # Erstelle automatisch generiertes Bild
return await gen_day_auto_image( return await gen_day_auto_image(
day=day, day=day,