From 791a196e15703b12d995c60c4afa7eeb8762aaf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 4 Sep 2022 23:03:16 +0000 Subject: [PATCH] config.txt handling (missing aggregate calendars) --- api/ovdashboard_api/config.py | 59 +++++++++++++++++++++++++++++++++++ api/poetry.lock | 26 ++++++++++++++- api/pyproject.toml | 2 ++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 api/ovdashboard_api/config.py diff --git a/api/ovdashboard_api/config.py b/api/ovdashboard_api/config.py new file mode 100644 index 0000000..b4c1bc3 --- /dev/null +++ b/api/ovdashboard_api/config.py @@ -0,0 +1,59 @@ +from io import BytesIO +from typing import Any, Optional + +from pydantic import BaseModel +from tomli import loads as toml_loads +from tomli_w import dump as toml_dump +from webdav3.exceptions import RemoteResourceNotFound + +from .async_helpers import run_in_executor +from .dav_common import webdav_resource +from .dav_file import DavFile + + +class ImageConfig(BaseModel): + mode: str = "RGB" + save_params: dict[str, Any] = { + "format": "JPEG", + "quality": 85, + } + + +class Config(BaseModel): + ticker_separator: str = " +++ " + cache_seconds: int = 30 + + image: ImageConfig = ImageConfig() + + __instance: Optional["Config"] = None + + @classmethod + async def get(cls) -> "Config": + if cls.__instance is not None: + return cls.__instance + + try: + dav_file = DavFile("config.txt") + + cls.__instance = cls.parse_obj( + toml_loads(await dav_file.string) + ) + + except RemoteResourceNotFound: + cls.__instance = cls() + + @run_in_executor + def create_conf() -> None: + buffer = BytesIO() + toml_dump( + cls.__instance.dict(), + buffer, + multiline_strings=True, + ) + + buffer.seek(0) + webdav_resource("config.txt").read_from(buffer) + + await create_conf() + + return cls.__instance diff --git a/api/poetry.lock b/api/poetry.lock index 5e20d06..a8c5fd9 100644 --- a/api/poetry.lock +++ b/api/poetry.lock @@ -273,6 +273,22 @@ typing-extensions = {version = ">=3.10.0", markers = "python_version < \"3.10\"" [package.extras] full = ["itsdangerous", "jinja2", "python-multipart", "pyyaml", "requests"] +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +category = "main" +optional = false +python-versions = ">=3.7" + +[[package]] +name = "tomli-w" +version = "1.0.0" +description = "A lil' TOML writer" +category = "main" +optional = false +python-versions = ">=3.7" + [[package]] name = "typing-extensions" version = "4.3.0" @@ -372,7 +388,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>= [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "e6a77257433600e323c267400376a4d16ddbae3d72f5d3a2a1b976f02fa36874" +content-hash = "f31c08e6b5aabf05d1d144a083abbfb76c8708ef23f6d99122c646126c9f7bcd" [metadata.files] anyio = [ @@ -625,6 +641,14 @@ starlette = [ {file = "starlette-0.19.1-py3-none-any.whl", hash = "sha256:5a60c5c2d051f3a8eb546136aa0c9399773a689595e099e0877704d5888279bf"}, {file = "starlette-0.19.1.tar.gz", hash = "sha256:c6d21096774ecb9639acad41b86b7706e52ba3bf1dc13ea4ed9ad593d47e24c7"}, ] +tomli = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] +tomli-w = [ + {file = "tomli_w-1.0.0-py3-none-any.whl", hash = "sha256:9f2a07e8be30a0729e533ec968016807069991ae2fd921a78d42f429ae5f4463"}, + {file = "tomli_w-1.0.0.tar.gz", hash = "sha256:f463434305e0336248cac9c2dc8076b707d8a12d019dd349f5c1e382dd1ae1b9"}, +] typing-extensions = [ {file = "typing_extensions-4.3.0-py3-none-any.whl", hash = "sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02"}, {file = "typing_extensions-4.3.0.tar.gz", hash = "sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6"}, diff --git a/api/pyproject.toml b/api/pyproject.toml index 278093a..43154f2 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -14,6 +14,8 @@ python = "^3.9" uvicorn = "^0.18.3" webdavclient3 = "3.14.5" caldav = "^0.9.1" +tomli = "^2.0.1" +tomli-w = "^1.0.0" [tool.poetry.dev-dependencies] # pytest = "^5.2"