PrefixUnique class, unified naming

This commit is contained in:
Jörn-Michael Miehe 2022-09-04 14:14:22 +00:00
parent f448ca79a6
commit cf684ee5f9
4 changed files with 38 additions and 44 deletions

View file

@ -6,7 +6,6 @@ from fastapi import HTTPException, status
from webdav3.exceptions import RemoteResourceNotFound
from .. import caldav_principal, webdav_list
from ..dav_file import DavFile
@dataclass(frozen=True)
@ -56,7 +55,7 @@ class PrefixFinder:
@dataclass(frozen=True)
class FilePrefixLoader:
class PrefixUnique:
finder: PrefixFinder
@property
@ -66,24 +65,25 @@ class FilePrefixLoader:
"description": "Operation successful",
},
status.HTTP_404_NOT_FOUND: {
"description": "file not found in " +
repr(self.finder.lister.remote_path),
"description": "Prefix not found",
"content": None,
},
status.HTTP_409_CONFLICT: {
"description": "ambiguous file name for " +
repr(self.finder.lister.remote_path),
"description": "Ambiguous prefix",
"content": None,
},
}
async def __call__(self, prefix: str) -> DavFile:
file_names = list(await self.finder(prefix))
async def __call__(self, prefix: str) -> str:
names = await self.finder(prefix)
if not (file_names):
try:
name = next(names)
except StopIteration:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
elif len(file_names) > 1:
if any(True for _ in names):
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
return DavFile(f"{self.finder.lister.remote_path}/{file_names[0]}")
return name

View file

@ -1,30 +1,31 @@
from datetime import datetime, timedelta
from typing import Iterator
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from .. import caldav_principal
from ._common import CalendarNameLister, PrefixFinder
from ._common import CalendarNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/calendar", tags=["calendar"])
_lister = CalendarNameLister()
_finder = PrefixFinder(_lister)
_unique = PrefixUnique(_finder)
@router.get("/list", response_model=list[str])
async def list_calendars(
calendar_names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(_lister),
) -> list[str]:
return list(calendar_names)
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_calendars(
calendar_names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(_finder),
) -> list[str]:
return list(calendar_names)
return list(names)
class CalEvent(BaseModel):
@ -36,17 +37,9 @@ class CalEvent(BaseModel):
@router.get("/get/{prefix}", response_model=list[CalEvent])
async def get_calendar(
calendar_names: Iterator[str] = Depends(_finder),
name: Iterator[str] = Depends(_unique),
) -> list[CalEvent]:
calendar_names = list(calendar_names)
if not (calendar_names):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
elif len(calendar_names) > 1:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
calendar = caldav_principal().calendar(name=calendar_names[0])
calendar = caldav_principal().calendar(name=name)
return (
CalEvent(

View file

@ -7,7 +7,7 @@ from fastapi.responses import StreamingResponse
from PIL import Image
from ..dav_file import DavFile
from ._common import FileNameLister, FilePrefixLoader, PrefixFinder
from ._common import FileNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/image", tags=["image"])
@ -20,32 +20,33 @@ _lister = FileNameLister(
)
_finder = PrefixFinder(_lister)
_loader = FilePrefixLoader(_finder)
_unique = PrefixUnique(_finder)
@router.get("/list", response_model=list[str])
async def list_images(
image_file_names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(_lister),
) -> list[str]:
return list(image_file_names)
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_images(
file_names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(_finder),
) -> list[str]:
return list(file_names)
return list(names)
@router.get(
"/get/{prefix}",
response_class=StreamingResponse,
responses=_loader.responses,
responses=_unique.responses,
)
async def get_image(
prefix: str,
dav_file: DavFile = Depends(_loader),
name: str = Depends(_unique),
) -> str:
dav_file = DavFile(f"{_lister.remote_path}/{name}")
img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB")
img_buffer = BytesIO()

View file

@ -6,7 +6,7 @@ from markdown import markdown
from ..config import SETTINGS
from ..dav_file import DavFile
from ._common import FileNameLister, FilePrefixLoader, PrefixFinder
from ._common import FileNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/text", tags=["text"])
@ -19,7 +19,7 @@ _lister = FileNameLister(
)
_finder = PrefixFinder(_lister)
_loader = FilePrefixLoader(_finder)
_unique = PrefixUnique(_finder)
async def get_ticker_lines() -> Iterator[str]:
@ -53,27 +53,27 @@ async def get_ticker_content(
@router.get("/list", response_model=list[str])
async def list_texts(
text_file_names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(_lister),
) -> list[str]:
return list(text_file_names)
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_texts(
file_names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(_finder),
) -> list[str]:
return list(file_names)
return list(names)
@router.get(
"/get/{prefix}",
response_model=str,
responses=_loader.responses,
responses=_unique.responses,
)
async def get_text(
dav_file: DavFile = Depends(_loader),
name: str = Depends(_unique),
) -> str:
text = await dav_file.string
text = await DavFile(f"{_lister.remote_path}/{name}").string
return markdown(
text