PrefixUnique class, unified naming
This commit is contained in:
parent
f448ca79a6
commit
cf684ee5f9
4 changed files with 38 additions and 44 deletions
|
@ -6,7 +6,6 @@ from fastapi import HTTPException, status
|
||||||
from webdav3.exceptions import RemoteResourceNotFound
|
from webdav3.exceptions import RemoteResourceNotFound
|
||||||
|
|
||||||
from .. import caldav_principal, webdav_list
|
from .. import caldav_principal, webdav_list
|
||||||
from ..dav_file import DavFile
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
|
@ -56,7 +55,7 @@ class PrefixFinder:
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class FilePrefixLoader:
|
class PrefixUnique:
|
||||||
finder: PrefixFinder
|
finder: PrefixFinder
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -66,24 +65,25 @@ class FilePrefixLoader:
|
||||||
"description": "Operation successful",
|
"description": "Operation successful",
|
||||||
},
|
},
|
||||||
status.HTTP_404_NOT_FOUND: {
|
status.HTTP_404_NOT_FOUND: {
|
||||||
"description": "file not found in " +
|
"description": "Prefix not found",
|
||||||
repr(self.finder.lister.remote_path),
|
|
||||||
"content": None,
|
"content": None,
|
||||||
},
|
},
|
||||||
status.HTTP_409_CONFLICT: {
|
status.HTTP_409_CONFLICT: {
|
||||||
"description": "ambiguous file name for " +
|
"description": "Ambiguous prefix",
|
||||||
repr(self.finder.lister.remote_path),
|
|
||||||
"content": None,
|
"content": None,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
async def __call__(self, prefix: str) -> DavFile:
|
async def __call__(self, prefix: str) -> str:
|
||||||
file_names = list(await self.finder(prefix))
|
names = await self.finder(prefix)
|
||||||
|
|
||||||
if not (file_names):
|
try:
|
||||||
|
name = next(names)
|
||||||
|
|
||||||
|
except StopIteration:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
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)
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
||||||
|
|
||||||
return DavFile(f"{self.finder.lister.remote_path}/{file_names[0]}")
|
return name
|
||||||
|
|
|
@ -1,30 +1,31 @@
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from .. import caldav_principal
|
from .. import caldav_principal
|
||||||
from ._common import CalendarNameLister, PrefixFinder
|
from ._common import CalendarNameLister, PrefixFinder, PrefixUnique
|
||||||
|
|
||||||
router = APIRouter(prefix="/calendar", tags=["calendar"])
|
router = APIRouter(prefix="/calendar", tags=["calendar"])
|
||||||
|
|
||||||
_lister = CalendarNameLister()
|
_lister = CalendarNameLister()
|
||||||
_finder = PrefixFinder(_lister)
|
_finder = PrefixFinder(_lister)
|
||||||
|
_unique = PrefixUnique(_finder)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/list", response_model=list[str])
|
@router.get("/list", response_model=list[str])
|
||||||
async def list_calendars(
|
async def list_calendars(
|
||||||
calendar_names: Iterator[str] = Depends(_lister),
|
names: Iterator[str] = Depends(_lister),
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
return list(calendar_names)
|
return list(names)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/find/{prefix}", response_model=list[str])
|
@router.get("/find/{prefix}", response_model=list[str])
|
||||||
async def find_calendars(
|
async def find_calendars(
|
||||||
calendar_names: Iterator[str] = Depends(_finder),
|
names: Iterator[str] = Depends(_finder),
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
return list(calendar_names)
|
return list(names)
|
||||||
|
|
||||||
|
|
||||||
class CalEvent(BaseModel):
|
class CalEvent(BaseModel):
|
||||||
|
@ -36,17 +37,9 @@ class CalEvent(BaseModel):
|
||||||
|
|
||||||
@router.get("/get/{prefix}", response_model=list[CalEvent])
|
@router.get("/get/{prefix}", response_model=list[CalEvent])
|
||||||
async def get_calendar(
|
async def get_calendar(
|
||||||
calendar_names: Iterator[str] = Depends(_finder),
|
name: Iterator[str] = Depends(_unique),
|
||||||
) -> list[CalEvent]:
|
) -> list[CalEvent]:
|
||||||
calendar_names = list(calendar_names)
|
calendar = caldav_principal().calendar(name=name)
|
||||||
|
|
||||||
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])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
CalEvent(
|
CalEvent(
|
||||||
|
|
|
@ -7,7 +7,7 @@ from fastapi.responses import StreamingResponse
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from ..dav_file import DavFile
|
from ..dav_file import DavFile
|
||||||
from ._common import FileNameLister, FilePrefixLoader, PrefixFinder
|
from ._common import FileNameLister, PrefixFinder, PrefixUnique
|
||||||
|
|
||||||
router = APIRouter(prefix="/image", tags=["image"])
|
router = APIRouter(prefix="/image", tags=["image"])
|
||||||
|
|
||||||
|
@ -20,32 +20,33 @@ _lister = FileNameLister(
|
||||||
)
|
)
|
||||||
|
|
||||||
_finder = PrefixFinder(_lister)
|
_finder = PrefixFinder(_lister)
|
||||||
_loader = FilePrefixLoader(_finder)
|
_unique = PrefixUnique(_finder)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/list", response_model=list[str])
|
@router.get("/list", response_model=list[str])
|
||||||
async def list_images(
|
async def list_images(
|
||||||
image_file_names: Iterator[str] = Depends(_lister),
|
names: Iterator[str] = Depends(_lister),
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
return list(image_file_names)
|
return list(names)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/find/{prefix}", response_model=list[str])
|
@router.get("/find/{prefix}", response_model=list[str])
|
||||||
async def find_images(
|
async def find_images(
|
||||||
file_names: Iterator[str] = Depends(_finder),
|
names: Iterator[str] = Depends(_finder),
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
return list(file_names)
|
return list(names)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/get/{prefix}",
|
"/get/{prefix}",
|
||||||
response_class=StreamingResponse,
|
response_class=StreamingResponse,
|
||||||
responses=_loader.responses,
|
responses=_unique.responses,
|
||||||
)
|
)
|
||||||
async def get_image(
|
async def get_image(
|
||||||
prefix: str,
|
prefix: str,
|
||||||
dav_file: DavFile = Depends(_loader),
|
name: str = Depends(_unique),
|
||||||
) -> str:
|
) -> str:
|
||||||
|
dav_file = DavFile(f"{_lister.remote_path}/{name}")
|
||||||
img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB")
|
img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB")
|
||||||
|
|
||||||
img_buffer = BytesIO()
|
img_buffer = BytesIO()
|
||||||
|
|
|
@ -6,7 +6,7 @@ from markdown import markdown
|
||||||
|
|
||||||
from ..config import SETTINGS
|
from ..config import SETTINGS
|
||||||
from ..dav_file import DavFile
|
from ..dav_file import DavFile
|
||||||
from ._common import FileNameLister, FilePrefixLoader, PrefixFinder
|
from ._common import FileNameLister, PrefixFinder, PrefixUnique
|
||||||
|
|
||||||
router = APIRouter(prefix="/text", tags=["text"])
|
router = APIRouter(prefix="/text", tags=["text"])
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ _lister = FileNameLister(
|
||||||
)
|
)
|
||||||
|
|
||||||
_finder = PrefixFinder(_lister)
|
_finder = PrefixFinder(_lister)
|
||||||
_loader = FilePrefixLoader(_finder)
|
_unique = PrefixUnique(_finder)
|
||||||
|
|
||||||
|
|
||||||
async def get_ticker_lines() -> Iterator[str]:
|
async def get_ticker_lines() -> Iterator[str]:
|
||||||
|
@ -53,27 +53,27 @@ async def get_ticker_content(
|
||||||
|
|
||||||
@router.get("/list", response_model=list[str])
|
@router.get("/list", response_model=list[str])
|
||||||
async def list_texts(
|
async def list_texts(
|
||||||
text_file_names: Iterator[str] = Depends(_lister),
|
names: Iterator[str] = Depends(_lister),
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
return list(text_file_names)
|
return list(names)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/find/{prefix}", response_model=list[str])
|
@router.get("/find/{prefix}", response_model=list[str])
|
||||||
async def find_texts(
|
async def find_texts(
|
||||||
file_names: Iterator[str] = Depends(_finder),
|
names: Iterator[str] = Depends(_finder),
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
return list(file_names)
|
return list(names)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/get/{prefix}",
|
"/get/{prefix}",
|
||||||
response_model=str,
|
response_model=str,
|
||||||
responses=_loader.responses,
|
responses=_unique.responses,
|
||||||
)
|
)
|
||||||
async def get_text(
|
async def get_text(
|
||||||
dav_file: DavFile = Depends(_loader),
|
name: str = Depends(_unique),
|
||||||
) -> str:
|
) -> str:
|
||||||
text = await dav_file.string
|
text = await DavFile(f"{_lister.remote_path}/{name}").string
|
||||||
|
|
||||||
return markdown(
|
return markdown(
|
||||||
text
|
text
|
||||||
|
|
Loading…
Reference in a new issue