From 33359aae43ada6473eabb66066049debda3fbea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 6 Sep 2022 22:55:23 +0000 Subject: [PATCH] logging and naming --- api/ovdashboard_api/config.py | 12 ++++++++++-- api/ovdashboard_api/dav_calendar.py | 2 +- api/ovdashboard_api/dav_file.py | 4 ++-- api/ovdashboard_api/settings.py | 1 + 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/api/ovdashboard_api/config.py b/api/ovdashboard_api/config.py index ea526d4..433093f 100644 --- a/api/ovdashboard_api/config.py +++ b/api/ovdashboard_api/config.py @@ -3,6 +3,7 @@ Python representation of the "config.txt" file inside the WebDAV directory. """ from io import BytesIO +from logging import getLogger from typing import Any from pydantic import BaseModel @@ -12,6 +13,9 @@ from webdav3.exceptions import RemoteResourceNotFound from .dav_common import caldav_list from .dav_file import DavFile +from .settings import SETTINGS + +_logger = getLogger(__name__) class TickerConfig(BaseModel): @@ -61,7 +65,7 @@ class Config(BaseModel): Load the configuration instance from the server using `TOML`. """ - dav_file = DavFile("config.txt") + dav_file = DavFile(SETTINGS.config_path) try: return cls.parse_obj( @@ -69,12 +73,16 @@ class Config(BaseModel): ) except RemoteResourceNotFound: + _logger.warn( + f"Config file {SETTINGS.config_path!r} not found, creating ..." + ) + cfg = cls() cfg.calendar.aggregate["All Events"] = list(await caldav_list()) buffer = BytesIO() toml_dump(cfg.dict(), buffer) buffer.seek(0) - await dav_file.dump(buffer.read()) + await dav_file.write(buffer.read()) return cfg diff --git a/api/ovdashboard_api/dav_calendar.py b/api/ovdashboard_api/dav_calendar.py index dc7a713..7b60825 100644 --- a/api/ovdashboard_api/dav_calendar.py +++ b/api/ovdashboard_api/dav_calendar.py @@ -130,7 +130,7 @@ async def _get_calendar_events( This can return an iterator - only the outer function is cached. """ - _logger.info(f"updating {calendar_name!r} ...") + _logger.info(f"downloading {calendar_name!r} ...") calendar = caldav_principal().calendar(calendar_name) diff --git a/api/ovdashboard_api/dav_file.py b/api/ovdashboard_api/dav_file.py index 4ba7022..6c13f38 100644 --- a/api/ovdashboard_api/dav_file.py +++ b/api/ovdashboard_api/dav_file.py @@ -28,7 +28,7 @@ async def _get_buffer( @run_in_executor def _inner() -> BytesIO: - _logger.info(f"updating {remote_path!r} ...") + _logger.info(f"downloading {remote_path!r} ...") resource = webdav_resource(remote_path) buffer = BytesIO() @@ -85,7 +85,7 @@ class DavFile: bytes = await self.bytes return bytes.decode(encoding="utf-8") - async def dump(self, content: bytes) -> None: + async def write(self, content: bytes) -> None: """ Write bytes into file. """ diff --git a/api/ovdashboard_api/settings.py b/api/ovdashboard_api/settings.py index 652b02d..3dceb79 100644 --- a/api/ovdashboard_api/settings.py +++ b/api/ovdashboard_api/settings.py @@ -55,6 +55,7 @@ class Settings(BaseSettings): webdav: DavSettings = DavSettings() webdav_prefix: str = "/ovdashboard" + config_path: str = "config.txt" caldav: DavSettings = DavSettings() cache_seconds: int = 30