From 2a9635c8c1ea5aa5e0fb8f4d763fefb8822edde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 31 Oct 2023 23:35:24 +0100 Subject: [PATCH] refac: get_all_image_names using new helper list_images_manual --- Ideen.md | 2 -- api/advent22_api/core/depends.py | 19 ++++++++++++------- api/advent22_api/core/helpers.py | 19 +++++++++++++++---- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Ideen.md b/Ideen.md index 4a903bb..bc9d783 100644 --- a/Ideen.md +++ b/Ideen.md @@ -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 diff --git a/api/advent22_api/core/depends.py b/api/advent22_api/core/depends.py index e3dcb60..b1f32ad 100644 --- a/api/advent22_api/core/depends.py +++ b/api/advent22_api/core/depends.py @@ -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 diff --git a/api/advent22_api/core/helpers.py b/api/advent22_api/core/helpers.py index 07365dd..639dc07 100644 --- a/api/advent22_api/core/helpers.py +++ b/api/advent22_api/core/helpers.py @@ -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) ]