From b7292af6ad862675b4a4a90d611aad2d5165e384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 23 Oct 2023 23:44:09 +0200 Subject: [PATCH] wip: functionify routers._common --- api/ovdashboard_api/routers/v1/_common.py | 23 ++++++++++++++--------- api/ovdashboard_api/routers/v1/file.py | 6 +++--- api/ovdashboard_api/routers/v1/image.py | 6 +++--- api/ovdashboard_api/routers/v1/text.py | 6 +++--- api/ovdashboard_api/routers/v1/ticker.py | 6 +++--- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/api/ovdashboard_api/routers/v1/_common.py b/api/ovdashboard_api/routers/v1/_common.py index 4d3ec6c..abfbaf1 100644 --- a/api/ovdashboard_api/routers/v1/_common.py +++ b/api/ovdashboard_api/routers/v1/_common.py @@ -18,7 +18,6 @@ from ...core.webdav import WebDAV _logger = getLogger(__name__) - _RESPONSE_OK = { status.HTTP_200_OK: { "description": "Operation successful", @@ -31,13 +30,19 @@ Return = TypeVar("Return") @dataclass(slots=True, frozen=True) class Dependable[**Params, Return]: - func: Callable[Params, Return] + func: Callable[Params, Awaitable[Return]] responses: dict + async def __call__(self, *args: Params.args, **kwds: Params.kwargs) -> Return: + return await self.func(*args, **kwds) + + +type _NDependable[Return] = Dependable[[], Return] + def get_remote_path( path_name: str, -) -> Dependable[[], Awaitable[str]]: +) -> _NDependable[str]: async def _get_remote_path() -> str: cfg = await get_config() return getattr(cfg, path_name) @@ -52,7 +57,7 @@ def list_files( *, path_name: str, re: re.Pattern[str], -) -> Dependable[[], Awaitable[list[str]]]: +) -> _NDependable[list[str]]: """ List files in remote `path` matching the RegEx `re` """ @@ -100,8 +105,8 @@ def list_files( def filter_prefix( - src: Callable[[], Awaitable[list[str]]], -) -> Dependable[[str], Awaitable[list[str]]]: + src: _NDependable[list[str]], +) -> Dependable[[str], list[str]]: """ Filter names from an async source `src` for names starting with a given prefix. """ @@ -118,7 +123,7 @@ def filter_prefix( responses={ **_RESPONSE_OK, status.HTTP_404_NOT_FOUND: { - "description": f"Failure in lister {src.__name__!r}", + "description": f"Failure in lister {src.__class__.__name__!r}", "content": None, }, }, @@ -126,8 +131,8 @@ def filter_prefix( def filter_prefix_unique( - src: Callable[[str], Awaitable[list[str]]], -) -> Dependable[[str], Awaitable[str]]: + src: Dependable[[str], list[str]], +) -> Dependable[[str], str]: """ Determines if a given prefix is unique in the list produced by the async source `src`. diff --git a/api/ovdashboard_api/routers/v1/file.py b/api/ovdashboard_api/routers/v1/file.py index 1fc3932..6d83091 100644 --- a/api/ovdashboard_api/routers/v1/file.py +++ b/api/ovdashboard_api/routers/v1/file.py @@ -33,15 +33,15 @@ _ls = list_files( ) _rp = get_remote_path(path_name=_PATH_NAME) -_fp = filter_prefix(_ls.func) -_fpu = filter_prefix_unique(_fp.func) +_fp = filter_prefix(_ls) +_fpu = filter_prefix_unique(_fp) @router.on_event("startup") async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") - remote_path = await _rp.func() + remote_path = await _rp() if not webdav_ensure_path(remote_path): webdav_ensure_files( remote_path, diff --git a/api/ovdashboard_api/routers/v1/image.py b/api/ovdashboard_api/routers/v1/image.py index 145b400..5345d80 100644 --- a/api/ovdashboard_api/routers/v1/image.py +++ b/api/ovdashboard_api/routers/v1/image.py @@ -33,15 +33,15 @@ _ls = list_files( ) _rp = get_remote_path(path_name=_PATH_NAME) -_fp = filter_prefix(_ls.func) -_fpu = filter_prefix_unique(_fp.func) +_fp = filter_prefix(_ls) +_fpu = filter_prefix_unique(_fp) @router.on_event("startup") async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") - remote_path = await _rp.func() + remote_path = await _rp() if not webdav_ensure_path(remote_path): webdav_ensure_files( remote_path, diff --git a/api/ovdashboard_api/routers/v1/text.py b/api/ovdashboard_api/routers/v1/text.py index 9a1c239..91aa0a2 100644 --- a/api/ovdashboard_api/routers/v1/text.py +++ b/api/ovdashboard_api/routers/v1/text.py @@ -31,15 +31,15 @@ _ls = list_files( ) _rp = get_remote_path(path_name=_PATH_NAME) -_fp = filter_prefix(_ls.func) -_fpu = filter_prefix_unique(_fp.func) +_fp = filter_prefix(_ls) +_fpu = filter_prefix_unique(_fp) @router.on_event("startup") async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") - remote_path = await _rp.func() + remote_path = await _rp() if not webdav_ensure_path(remote_path): webdav_ensure_files( remote_path, diff --git a/api/ovdashboard_api/routers/v1/ticker.py b/api/ovdashboard_api/routers/v1/ticker.py index bb59b62..7c2e04a 100644 --- a/api/ovdashboard_api/routers/v1/ticker.py +++ b/api/ovdashboard_api/routers/v1/ticker.py @@ -26,7 +26,7 @@ router = APIRouter(prefix="/ticker", tags=["text"]) async def start_router() -> None: _logger.debug(f"{router.prefix} router starting.") - remote_path = await _rp.func() + remote_path = await _rp() if not webdav_ensure_path(remote_path): webdav_ensure_files( remote_path, @@ -36,8 +36,8 @@ async def start_router() -> None: async def get_ticker_lines() -> Iterator[str]: cfg = await get_config() - file_name = await _fpu.func(cfg.ticker.file_name) - remote_path = await _rp.func() + file_name = await _fpu(cfg.ticker.file_name) + remote_path = await _rp() ticker = await WebDAV.read_str(f"{remote_path}/{file_name}")