utility object naming

This commit is contained in:
Jörn-Michael Miehe 2022-09-06 23:50:42 +00:00
parent e193410725
commit ea85164063
4 changed files with 41 additions and 35 deletions

View file

@ -13,37 +13,43 @@ from ovdashboard_api.config import Config
from ..dav_calendar import CalEvent, DavCalendar
from ._common import CalAggregateLister, PrefixFinder, PrefixUnique
from .calendar import calendar_unique
router = APIRouter(prefix="/aggregate", tags=["calendar"])
_lister = CalAggregateLister()
_finder = PrefixFinder(_lister)
_unique = PrefixUnique(_finder)
cal_aggregate_lister = CalAggregateLister()
cal_aggregate_finder = PrefixFinder(cal_aggregate_lister)
cal_aggregate_unique = PrefixUnique(cal_aggregate_finder)
@router.get("/list", response_model=list[str])
async def list_aggregate_calendars(
names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(cal_aggregate_lister),
) -> list[str]:
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_aggregate_calendars(
names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(cal_aggregate_finder),
) -> list[str]:
return list(names)
@router.get("/get/{prefix}", response_model=list[CalEvent])
async def get_aggregate_calendar(
name: Iterator[str] = Depends(_unique),
name: str = Depends(cal_aggregate_unique),
) -> list[CalEvent]:
cfg = await Config.get()
aggregate = cfg.calendar.aggregate[name]
calendars = (
DavCalendar(await calendar_unique(cal_prefix))
for cal_prefix in aggregate
)
return sorted([
event
for calendar_name in aggregate
for event in (await DavCalendar(calendar_name).events)
async for calendar in calendars
for event in (await calendar.events)
])

View file

@ -15,27 +15,27 @@ from ._common import CalendarNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/calendar", tags=["calendar"])
_lister = CalendarNameLister()
_finder = PrefixFinder(_lister)
_unique = PrefixUnique(_finder)
calendar_lister = CalendarNameLister()
calendar_finder = PrefixFinder(calendar_lister)
calendar_unique = PrefixUnique(calendar_finder)
@router.get("/list", response_model=list[str])
async def list_calendars(
names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(calendar_lister),
) -> list[str]:
return list(names)
@router.get("/find/{prefix}", response_model=list[str])
async def find_calendars(
names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(calendar_finder),
) -> list[str]:
return list(names)
@router.get("/get/{prefix}", response_model=list[CalEvent])
async def get_calendar(
name: str = Depends(_unique),
name: str = Depends(calendar_unique),
) -> list[CalEvent]:
return list(await DavCalendar(name).events)

View file

@ -20,7 +20,7 @@ from ._common import FileNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/image", tags=["image"])
_lister = FileNameLister(
image_lister = FileNameLister(
remote_path="img",
re=re.compile(
r"\.(gif|jpe?g|tiff?|png|bmp)$",
@ -28,17 +28,17 @@ _lister = FileNameLister(
),
)
_finder = PrefixFinder(_lister)
_unique = PrefixUnique(_finder)
image_finder = PrefixFinder(image_lister)
image_unique = PrefixUnique(image_finder)
@router.get(
"/list",
response_model=list[str],
responses=_lister.responses,
responses=image_lister.responses,
)
async def list_images(
names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(image_lister),
) -> list[str]:
return list(names)
@ -46,10 +46,10 @@ async def list_images(
@router.get(
"/find/{prefix}",
response_model=list[str],
responses=_finder.responses,
responses=image_finder.responses,
)
async def find_images(
names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(image_finder),
) -> list[str]:
return list(names)
@ -57,15 +57,15 @@ async def find_images(
@router.get(
"/get/{prefix}",
response_class=StreamingResponse,
responses=_unique.responses,
responses=image_unique.responses,
)
async def get_image(
prefix: str,
name: str = Depends(_unique),
name: str = Depends(image_unique),
) -> str:
cfg = await Config.get()
dav_file = DavFile(f"{_lister.remote_path}/{name}")
dav_file = DavFile(f"{image_lister.remote_path}/{name}")
img = Image.open(
BytesIO(await dav_file.bytes)
).convert(

View file

@ -21,7 +21,7 @@ from ._common import FileNameLister, PrefixFinder, PrefixUnique
router = APIRouter(prefix="/text", tags=["text"])
_lister = FileNameLister(
text_lister = FileNameLister(
remote_path="text",
re=re.compile(
r"\.(txt|md)$",
@ -29,8 +29,8 @@ _lister = FileNameLister(
),
)
_finder = PrefixFinder(_lister)
_unique = PrefixUnique(_finder)
text_finder = PrefixFinder(text_lister)
text_unique = PrefixUnique(text_finder)
async def get_ticker_lines() -> Iterator[str]:
@ -82,10 +82,10 @@ async def get_raw_ticker(
@router.get(
"/list",
response_model=list[str],
responses=_lister.responses,
responses=text_lister.responses,
)
async def list_texts(
names: Iterator[str] = Depends(_lister),
names: Iterator[str] = Depends(text_lister),
) -> list[str]:
return list(names)
@ -93,24 +93,24 @@ async def list_texts(
@router.get(
"/find/{prefix}",
response_model=list[str],
responses=_finder.responses,
responses=text_finder.responses,
)
async def find_texts(
names: Iterator[str] = Depends(_finder),
names: Iterator[str] = Depends(text_finder),
) -> list[str]:
return list(names)
async def get_text_content(
name: str = Depends(_unique),
name: str = Depends(text_unique),
) -> str:
return await DavFile(f"{_lister.remote_path}/{name}").string
return await DavFile(f"{text_lister.remote_path}/{name}").string
@router.get(
"/get/html/{prefix}",
response_model=str,
responses=_unique.responses,
responses=text_unique.responses,
)
async def get_text(
text: str = Depends(get_text_content),
@ -121,7 +121,7 @@ async def get_text(
@router.get(
"/get/raw/{prefix}",
response_model=str,
responses=_unique.responses,
responses=text_unique.responses,
)
async def get_raw_text(
text: str = Depends(get_text_content),