From 749f56268cea518f808a63f109f456a3c0c2a019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Fri, 2 Sep 2022 13:22:35 +0000 Subject: [PATCH] common "Iterator" stuff --- api/ovdashboard_api/routers/_common.py | 38 +++++++++++++++++++ api/ovdashboard_api/routers/image.py | 44 ++++++---------------- api/ovdashboard_api/routers/text.py | 51 ++++++++------------------ 3 files changed, 64 insertions(+), 69 deletions(-) create mode 100644 api/ovdashboard_api/routers/_common.py diff --git a/api/ovdashboard_api/routers/_common.py b/api/ovdashboard_api/routers/_common.py new file mode 100644 index 0000000..4dcf3dd --- /dev/null +++ b/api/ovdashboard_api/routers/_common.py @@ -0,0 +1,38 @@ +import re +from dataclasses import dataclass +from typing import Iterator + +from webdav3.exceptions import RemoteResourceNotFound + +from .. import CLIENT + + +@dataclass(frozen=True) +class FileNameLister: + remote_path: str + re: re.Pattern[str] + + def __call__(self) -> Iterator[str]: + try: + file_names = CLIENT.list(self.remote_path) + + return ( + name + for name in file_names + if self.re.search(name) + ) + + except RemoteResourceNotFound: + return iter(()) + + +@dataclass(frozen=True) +class FilePrefixFinder: + lister: FileNameLister + + def __call__(self, prefix: str) -> Iterator[str]: + return ( + file_name + for file_name in self.lister() + if file_name.lower().startswith(prefix.lower()) + ) diff --git a/api/ovdashboard_api/routers/image.py b/api/ovdashboard_api/routers/image.py index b8ff576..c5ccf07 100644 --- a/api/ovdashboard_api/routers/image.py +++ b/api/ovdashboard_api/routers/image.py @@ -5,55 +5,33 @@ from typing import Iterator from fastapi import APIRouter, Depends, HTTPException, status from fastapi.responses import StreamingResponse from PIL import Image -from webdav3.exceptions import RemoteResourceNotFound -from .. import CLIENT from ..dav_file import DavFile +from ._common import FileNameLister, FilePrefixFinder router = APIRouter(prefix="/image", tags=["image"]) - -_re_image_file = re.compile( - r"\.(gif|jpe?g|tiff?|png|bmp)$", - flags=re.IGNORECASE, +_lister = FileNameLister( + remote_path="img", + re=re.compile( + r"\.(gif|jpe?g|tiff?|png|bmp)$", + flags=re.IGNORECASE, + ), ) - -async def get_image_file_names() -> Iterator[str]: - try: - file_names = CLIENT.list("img") - - return ( - name - for name in file_names - if _re_image_file.search(name) - ) - - except RemoteResourceNotFound: - return iter(()) +_finder = FilePrefixFinder(_lister) @router.get("/list", response_model=list[str]) async def list_images( - image_file_names: Iterator[str] = Depends(get_image_file_names), + image_file_names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(image_file_names) -async def find_file_names( - prefix: str = "", - image_file_names: Iterator[str] = Depends(get_image_file_names), -) -> Iterator[str]: - return ( - file_name - for file_name in image_file_names - if file_name.lower().startswith(prefix.lower()) - ) - - @router.get("/find/{prefix}", response_model=list[str]) async def find_images( - file_names: Iterator[str] = Depends(find_file_names), + file_names: Iterator[str] = Depends(_finder), ) -> list[str]: return list(file_names) @@ -77,7 +55,7 @@ async def find_images( ) async def get_image( prefix: str, - file_names: Iterator[str] = Depends(find_file_names), + file_names: Iterator[str] = Depends(_finder), ) -> StreamingResponse: file_names = list(file_names) diff --git a/api/ovdashboard_api/routers/text.py b/api/ovdashboard_api/routers/text.py index c7aad0a..0bde893 100644 --- a/api/ovdashboard_api/routers/text.py +++ b/api/ovdashboard_api/routers/text.py @@ -3,14 +3,23 @@ from typing import Iterator from fastapi import APIRouter, Depends, HTTPException, status from markdown import markdown -from webdav3.exceptions import RemoteResourceNotFound -from .. import CLIENT from ..config import SETTINGS from ..dav_file import DavFile +from ._common import FileNameLister, FilePrefixFinder router = APIRouter(prefix="/text", tags=["text"]) +_lister = FileNameLister( + remote_path="text", + re=re.compile( + r"\.(txt|md)$", + flags=re.IGNORECASE, + ), +) + +_finder = FilePrefixFinder(_lister) + async def get_ticker_lines() -> Iterator[str]: ticker = await DavFile("text/ticker.txt").string @@ -41,53 +50,23 @@ async def get_ticker_content( ) -_re_text_file = re.compile( - r"\.(txt|md)$", - flags=re.IGNORECASE, -) - - -async def get_text_file_names() -> Iterator[str]: - try: - file_names = CLIENT.list("text") - - return ( - name - for name in file_names - if _re_text_file.search(name) - ) - - except RemoteResourceNotFound: - return iter(()) - - @router.get("/list", response_model=list[str]) async def list_texts( - text_file_names: Iterator[str] = Depends(get_text_file_names), + text_file_names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(text_file_names) -async def find_file_names( - prefix: str = "", - text_file_names: Iterator[str] = Depends(get_text_file_names), -) -> Iterator[str]: - return ( - file_name - for file_name in text_file_names - if file_name.lower().startswith(prefix.lower()) - ) - - @router.get("/find/{prefix}", response_model=list[str]) async def find_texts( - file_names: Iterator[str] = Depends(find_file_names), + file_names: Iterator[str] = Depends(_finder), ) -> list[str]: return list(file_names) @router.get( "/get/{prefix}", + response_model=str, responses={ status.HTTP_200_OK: { "description": "Operation successful", @@ -103,7 +82,7 @@ async def find_texts( }, ) async def get_text( - file_names: Iterator[str] = Depends(find_file_names), + file_names: Iterator[str] = Depends(_finder), ) -> str: file_names = list(file_names)