config.txt handling (missing aggregate calendars)

This commit is contained in:
Jörn-Michael Miehe 2022-09-04 23:03:16 +00:00
parent 955aadfc86
commit 791a196e15
3 changed files with 86 additions and 1 deletions

View file

@ -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

26
api/poetry.lock generated
View file

@ -273,6 +273,22 @@ typing-extensions = {version = ">=3.10.0", markers = "python_version < \"3.10\""
[package.extras] [package.extras]
full = ["itsdangerous", "jinja2", "python-multipart", "pyyaml", "requests"] 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]] [[package]]
name = "typing-extensions" name = "typing-extensions"
version = "4.3.0" version = "4.3.0"
@ -372,7 +388,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>=
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.9" python-versions = "^3.9"
content-hash = "e6a77257433600e323c267400376a4d16ddbae3d72f5d3a2a1b976f02fa36874" content-hash = "f31c08e6b5aabf05d1d144a083abbfb76c8708ef23f6d99122c646126c9f7bcd"
[metadata.files] [metadata.files]
anyio = [ anyio = [
@ -625,6 +641,14 @@ starlette = [
{file = "starlette-0.19.1-py3-none-any.whl", hash = "sha256:5a60c5c2d051f3a8eb546136aa0c9399773a689595e099e0877704d5888279bf"}, {file = "starlette-0.19.1-py3-none-any.whl", hash = "sha256:5a60c5c2d051f3a8eb546136aa0c9399773a689595e099e0877704d5888279bf"},
{file = "starlette-0.19.1.tar.gz", hash = "sha256:c6d21096774ecb9639acad41b86b7706e52ba3bf1dc13ea4ed9ad593d47e24c7"}, {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 = [ typing-extensions = [
{file = "typing_extensions-4.3.0-py3-none-any.whl", hash = "sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02"}, {file = "typing_extensions-4.3.0-py3-none-any.whl", hash = "sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02"},
{file = "typing_extensions-4.3.0.tar.gz", hash = "sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6"}, {file = "typing_extensions-4.3.0.tar.gz", hash = "sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6"},

View file

@ -14,6 +14,8 @@ python = "^3.9"
uvicorn = "^0.18.3" uvicorn = "^0.18.3"
webdavclient3 = "3.14.5" webdavclient3 = "3.14.5"
caldav = "^0.9.1" caldav = "^0.9.1"
tomli = "^2.0.1"
tomli-w = "^1.0.0"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
# pytest = "^5.2" # pytest = "^5.2"