refac: get_all_image_names using new helper list_images_manual

This commit is contained in:
Jörn-Michael Miehe 2023-10-31 23:35:24 +01:00
parent 367fef145d
commit 2a9635c8c1
3 changed files with 27 additions and 13 deletions

View file

@ -10,8 +10,6 @@
## Penner
- immer TODOs finden: Ctrl-Shift-F "penner"
- API: Werte aus `config.py` an sinnvollen Stellen benutzen
- API: `depends.py:get_all_image_names` (ab Z. 86) manuelle Bilder einpflegen
# Erledigt

View file

@ -10,7 +10,14 @@ from .advent_image import _XY, AdventImage
from .calendar_config import CalendarConfig, get_calendar_config
from .config import Config, get_config
from .dav.webdav import WebDAV
from .helpers import EventDates, Random, list_images_auto, load_image, set_len
from .helpers import (
EventDates,
Random,
list_images_auto,
list_images_manual,
load_image,
set_len,
)
async def get_all_sorted_days(
@ -87,19 +94,17 @@ async def get_all_auto_image_names(
async def get_all_image_names(
auto_image_names: dict[int, str] = Depends(get_all_auto_image_names),
manual_image_names: list[str] = Depends(list_images_manual),
) -> dict[int, str]:
"""
Bilder "auto" und "manual" zu Tagen zuordnen
"""
__DIR = "/images_manual"
__RE = re.compile(r"\.jpg$", flags=re.IGNORECASE)
manual_image_names = await WebDAV.list_files(directory=__DIR, regex=__RE)
num_re = re.compile(r"/(\d+)\.", flags=re.IGNORECASE)
for name in manual_image_names:
zahl = int(__RE.sub(string=name, repl=""))
auto_image_names[zahl] = f"{__DIR}/{name}"
assert (num_match := num_re.search(name)) is not None
auto_image_names[int(num_match.group(1))] = name
return auto_image_names

View file

@ -12,6 +12,7 @@ from .config import get_config
from .dav.webdav import WebDAV
T = TypeVar("T")
RE_IMG = re.compile(r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE)
class Random(random.Random):
@ -88,10 +89,20 @@ async def list_images_auto() -> list[str]:
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),
)
for file in await WebDAV.list_files(directory=__DIR, regex=RE_IMG)
]
async def list_images_manual() -> list[str]:
"""
Finde alle Bilddateien im "manuell"-Verzeichnis
"""
__DIR = "/images_manual"
return [
f"{__DIR}/{file}"
for file in await WebDAV.list_files(directory=__DIR, regex=RE_IMG)
]