wip: functionify routers._common

This commit is contained in:
Jörn-Michael Miehe 2023-10-22 16:25:19 +02:00
parent 2ca6377634
commit 06abdfb953

View file

@ -7,11 +7,11 @@ from dataclasses import dataclass
from logging import getLogger from logging import getLogger
from typing import Iterator, Protocol from typing import Iterator, Protocol
from fastapi import HTTPException, status from fastapi import Depends, HTTPException, status
from webdav3.exceptions import RemoteResourceNotFound from webdav3.exceptions import RemoteResourceNotFound
from ...core.caldav import CalDAV from ...core.caldav import CalDAV
from ...core.config import get_config from ...core.config import Config, get_config
from ...core.webdav import WebDAV from ...core.webdav import WebDAV
_logger = getLogger(__name__) _logger = getLogger(__name__)
@ -74,6 +74,33 @@ class FileNameLister:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
async def get_remote_path(
path_name: str,
*,
cfg: Config = Depends(get_config),
) -> str:
return getattr(cfg, path_name)
async def list_files(
re: re.Pattern[str],
*,
path: str = Depends(get_remote_path),
) -> list[str]:
"""
List files in remote `path` matching the RegEx `re`
"""
try:
return await WebDAV.list_files(path, regex=re)
except RemoteResourceNotFound:
_logger.error(
"WebDAV path %s lost!",
repr(path),
)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class CalendarNameLister: class CalendarNameLister:
""" """
@ -84,6 +111,13 @@ class CalendarNameLister:
return iter(await CalDAV.calendars) return iter(await CalDAV.calendars)
async def list_calendar_names() -> list[str]:
"""
List calendar names
"""
return await CalDAV.calendars
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class AggregateNameLister: class AggregateNameLister:
""" """
@ -96,6 +130,15 @@ class AggregateNameLister:
return iter(cfg.calendar.aggregates.keys()) return iter(cfg.calendar.aggregates.keys())
async def list_aggregate_names(
cfg: Config = Depends(get_config),
) -> list[str]:
"""
List aggregate calendar names
"""
return list(cfg.calendar.aggregates.keys())
@dataclass(frozen=True, slots=True) @dataclass(frozen=True, slots=True)
class PrefixFinder: class PrefixFinder:
""" """