From b0e95af44ed9dec60020b647fcf6b6347f1f084b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:55:38 +0200 Subject: [PATCH] py3.12: more styling and linting --- api/ovdashboard_api/async_helpers.py | 4 +-- api/ovdashboard_api/dav_calendar.py | 11 ------ api/ovdashboard_api/dav_common.py | 39 ++++++++++----------- api/ovdashboard_api/routers/v1/_common.py | 9 ++--- api/ovdashboard_api/routers/v1/aggregate.py | 16 +++++---- api/ovdashboard_api/routers/v1/file.py | 4 +-- api/ovdashboard_api/routers/v1/image.py | 10 ++---- api/ovdashboard_api/routers/v1/ticker.py | 10 ++---- api/ovdashboard_api/settings.py | 16 ++++----- 9 files changed, 43 insertions(+), 76 deletions(-) diff --git a/api/ovdashboard_api/async_helpers.py b/api/ovdashboard_api/async_helpers.py index ecda7a4..b87af70 100644 --- a/api/ovdashboard_api/async_helpers.py +++ b/api/ovdashboard_api/async_helpers.py @@ -9,9 +9,7 @@ from typing import Awaitable, Callable, TypeVar RT = TypeVar("RT") -def run_in_executor( - function: Callable[..., RT] -) -> Callable[..., Awaitable[RT]]: +def run_in_executor(function: Callable[..., RT]) -> Callable[..., Awaitable[RT]]: """ Decorator to make blocking a function call asyncio compatible. https://stackoverflow.com/questions/41063331/how-to-use-asyncio-with-existing-blocking-library/ diff --git a/api/ovdashboard_api/dav_calendar.py b/api/ovdashboard_api/dav_calendar.py index ab824be..5b69462 100644 --- a/api/ovdashboard_api/dav_calendar.py +++ b/api/ovdashboard_api/dav_calendar.py @@ -22,17 +22,6 @@ from .dav_common import caldav_principal from .settings import SETTINGS _logger = getLogger(__name__) - - -def _string_strip(in_str: str) -> str: - """ - Wrapper for str.strip(). - - Used to define `pydantic` validators. - """ - return in_str.strip() - - StrippedStr = Annotated[str, AfterValidator(lambda s: s.strip())] diff --git a/api/ovdashboard_api/dav_common.py b/api/ovdashboard_api/dav_common.py index 08e2f05..48357c2 100644 --- a/api/ovdashboard_api/dav_common.py +++ b/api/ovdashboard_api/dav_common.py @@ -18,12 +18,14 @@ from . import __file__ as OVD_INIT from .async_helpers import run_in_executor from .settings import SETTINGS -_WEBDAV_CLIENT = WebDAVclient({ - "webdav_hostname": SETTINGS.webdav.url, - "webdav_login": SETTINGS.webdav.username, - "webdav_password": SETTINGS.webdav.password, - "disable_check": SETTINGS.webdav_disable_check, -}) +_WEBDAV_CLIENT = WebDAVclient( + { + "webdav_hostname": SETTINGS.webdav.url, + "webdav_login": SETTINGS.webdav.username, + "webdav_password": SETTINGS.webdav.password, + "disable_check": SETTINGS.webdav_disable_check, + } +) _logger = getLogger(__name__) @@ -113,11 +115,13 @@ def webdav_ensure_files(remote_path: str, *file_names: str) -> None: missing_files = ( file_name for file_name in file_names - if not _WEBDAV_CLIENT.check(path.join( - SETTINGS.webdav_prefix, - remote_path, - file_name, - )) + if not _WEBDAV_CLIENT.check( + path.join( + SETTINGS.webdav_prefix, + remote_path, + file_name, + ) + ) ) webdav_upload_skel( @@ -132,9 +136,7 @@ def webdav_resource(remote_path: Any) -> WebDAVResource: Gets a resource using the main WebDAV client. """ - return _WEBDAV_CLIENT.resource( - f"{SETTINGS.webdav_prefix}/{remote_path}" - ) + return _WEBDAV_CLIENT.resource(f"{SETTINGS.webdav_prefix}/{remote_path}") @run_in_executor @@ -143,9 +145,7 @@ def webdav_list(remote_path: str) -> list[str]: Asynchronously lists a WebDAV path using the main WebDAV client. """ - return _WEBDAV_CLIENT.list( - f"{SETTINGS.webdav_prefix}/{remote_path}" - ) + return _WEBDAV_CLIENT.list(f"{SETTINGS.webdav_prefix}/{remote_path}") _CALDAV_CLIENT = CalDAVclient( @@ -169,7 +169,4 @@ def caldav_list() -> Iterator[str]: Asynchronously lists all calendars using the main WebDAV client. """ - return ( - str(cal.name) - for cal in caldav_principal().calendars() - ) + return (str(cal.name) for cal in caldav_principal().calendars()) diff --git a/api/ovdashboard_api/routers/v1/_common.py b/api/ovdashboard_api/routers/v1/_common.py index 09fe1b5..efd2435 100644 --- a/api/ovdashboard_api/routers/v1/_common.py +++ b/api/ovdashboard_api/routers/v1/_common.py @@ -63,11 +63,7 @@ class FileNameLister: try: file_names = await webdav_list(await self.remote_path) - return ( - name - for name in file_names - if self.re.search(name) - ) + return (name for name in file_names if self.re.search(name)) except RemoteResourceNotFound: _logger.error( @@ -115,8 +111,7 @@ class PrefixFinder: return { **_RESPONSE_OK, status.HTTP_404_NOT_FOUND: { - "description": "Failure in lister " + - repr(self.lister.__class__.__name__), + "description": f"Failure in lister {self.lister.__class__.__name__!r}", "content": None, }, } diff --git a/api/ovdashboard_api/routers/v1/aggregate.py b/api/ovdashboard_api/routers/v1/aggregate.py index fdf21ee..a07403c 100644 --- a/api/ovdashboard_api/routers/v1/aggregate.py +++ b/api/ovdashboard_api/routers/v1/aggregate.py @@ -10,6 +10,7 @@ from logging import getLogger from typing import Iterator from fastapi import APIRouter, Depends + from ovdashboard_api.config import Config from ...dav_calendar import CalEvent, DavCalendar @@ -52,12 +53,13 @@ async def get_aggregate_calendar( aggregate = cfg.calendar.aggregates[name] calendars = ( - DavCalendar(await calendar_unique(cal_prefix)) - for cal_prefix in aggregate + DavCalendar(await calendar_unique(cal_prefix)) for cal_prefix in aggregate ) - return sorted([ - event - async for calendar in calendars # type: ignore - for event in (await calendar.events) - ]) + return sorted( + [ + event + async for calendar in calendars # type: ignore + for event in (await calendar.events) + ] + ) diff --git a/api/ovdashboard_api/routers/v1/file.py b/api/ovdashboard_api/routers/v1/file.py index efd9114..10418bd 100644 --- a/api/ovdashboard_api/routers/v1/file.py +++ b/api/ovdashboard_api/routers/v1/file.py @@ -88,7 +88,5 @@ async def get_file( return StreamingResponse( content=buffer, media_type=mime, - headers={ - "Content-Disposition": f"filename={prefix}" - }, + headers={"Content-Disposition": f"filename={prefix}"}, ) diff --git a/api/ovdashboard_api/routers/v1/image.py b/api/ovdashboard_api/routers/v1/image.py index fc89d24..d613829 100644 --- a/api/ovdashboard_api/routers/v1/image.py +++ b/api/ovdashboard_api/routers/v1/image.py @@ -83,11 +83,7 @@ async def get_image( cfg = await Config.get() dav_file = DavFile(f"{await image_lister.remote_path}/{name}") - img = Image.open( - BytesIO(await dav_file.as_bytes) - ).convert( - cfg.image.mode - ) + img = Image.open(BytesIO(await dav_file.as_bytes)).convert(cfg.image.mode) img_buffer = BytesIO() img.save(img_buffer, **cfg.image.save_params) @@ -96,9 +92,7 @@ async def get_image( return StreamingResponse( content=img_buffer, media_type="image/jpeg", - headers={ - "Content-Disposition": f"filename={prefix}.jpg" - }, + headers={"Content-Disposition": f"filename={prefix}.jpg"}, ) diff --git a/api/ovdashboard_api/routers/v1/ticker.py b/api/ovdashboard_api/routers/v1/ticker.py index 85cecf8..98b07e0 100644 --- a/api/ovdashboard_api/routers/v1/ticker.py +++ b/api/ovdashboard_api/routers/v1/ticker.py @@ -42,11 +42,7 @@ async def get_ticker_lines() -> Iterator[str]: f"{await text_lister.remote_path}/{file_name}", ).as_string - return ( - line.strip() - for line in ticker.split("\n") - if line.strip() - ) + return (line.strip() for line in ticker.split("\n") if line.strip()) async def get_ticker_content_lines( @@ -55,9 +51,7 @@ async def get_ticker_content_lines( cfg = await Config.get() return ( - line - for line in ticker_lines - if not line.startswith(cfg.ticker.comment_marker) + line for line in ticker_lines if not line.startswith(cfg.ticker.comment_marker) ) diff --git a/api/ovdashboard_api/settings.py b/api/ovdashboard_api/settings.py index db42f46..51720e5 100644 --- a/api/ovdashboard_api/settings.py +++ b/api/ovdashboard_api/settings.py @@ -7,7 +7,7 @@ Converts per-run (environment) variables and config files into the Pydantic models might have convenience methods attached. """ -from typing import Any, Optional +from typing import Any from pydantic import BaseModel, root_validator from pydantic_settings import BaseSettings @@ -18,11 +18,11 @@ class DavSettings(BaseModel): Connection to a DAV server. """ - protocol: Optional[str] = None - host: Optional[str] = None - username: Optional[str] = None - password: Optional[str] = None - path: Optional[str] = None + protocol: str | None = None + host: str | None = None + username: str | None = None + password: str | None = None + path: str | None = None @property def url(self) -> str: @@ -57,8 +57,8 @@ class Settings(BaseSettings): ##### openapi_url: str = "/openapi.json" - docs_url: Optional[str] = None if production_mode else "/docs" - redoc_url: Optional[str] = None if production_mode else "/redoc" + docs_url: str | None = None if production_mode else "/docs" + redoc_url: str | None = None if production_mode else "/redoc" ##### # webdav settings