common "Iterator" stuff
This commit is contained in:
parent
9a350f6a71
commit
749f56268c
3 changed files with 64 additions and 69 deletions
38
api/ovdashboard_api/routers/_common.py
Normal file
38
api/ovdashboard_api/routers/_common.py
Normal file
|
@ -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())
|
||||
)
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue