common code for helpers.list_* functions

This commit is contained in:
Jörn-Michael Miehe 2023-10-31 23:43:38 +01:00
parent 2a9635c8c1
commit a1729d13f7

View file

@ -3,7 +3,7 @@ import random
import re import re
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from io import BytesIO from io import BytesIO
from typing import Any, Iterable, Self, Sequence, TypeVar from typing import Any, Awaitable, Callable, Iterable, Self, Sequence, TypeVar
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
@ -13,6 +13,7 @@ from .dav.webdav import WebDAV
T = TypeVar("T") T = TypeVar("T")
RE_IMG = re.compile(r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE) RE_IMG = re.compile(r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE)
RE_TTF = re.compile(r"\.(ttf)$", flags=re.IGNORECASE)
class Random(random.Random): class Random(random.Random):
@ -80,30 +81,26 @@ def spread(
return result return result
async def list_images_auto() -> list[str]: def list_helper(
directory: str,
regex: re.Pattern[str],
) -> Callable[[], Awaitable[list[str]]]:
""" """
Finde alle Bilddateien im "automatisch"-Verzeichnis Finde alle Dateien im Verzeichnis `dir`, passend zu `re`
""" """
__DIR = "/images_auto" async def _list_helper() -> list[str]:
return [ return [
f"{__DIR}/{file}" f"{directory}/{file}"
for file in await WebDAV.list_files(directory=__DIR, regex=RE_IMG) for file in await WebDAV.list_files(directory=directory, regex=regex)
] ]
return _list_helper
async def list_images_manual() -> list[str]:
"""
Finde alle Bilddateien im "manuell"-Verzeichnis
"""
__DIR = "/images_manual" list_images_auto = list_helper("/images_auto", RE_IMG)
list_images_manual = list_helper("/images_manual", RE_IMG)
return [ list_fonts = list_helper("/files", RE_TTF)
f"{__DIR}/{file}"
for file in await WebDAV.list_files(directory=__DIR, regex=RE_IMG)
]
async def load_image(file_name: str) -> Image.Image: async def load_image(file_name: str) -> Image.Image: