diff --git a/api/ovdashboard_api/config.py b/api/ovdashboard_api/config.py index 08a3638..6890fd5 100644 --- a/api/ovdashboard_api/config.py +++ b/api/ovdashboard_api/config.py @@ -1,5 +1,5 @@ from io import BytesIO -from typing import Any, Optional +from typing import Any from pydantic import BaseModel from tomli import loads as toml_loads @@ -7,7 +7,6 @@ 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 @@ -56,35 +55,20 @@ class Config(BaseModel): image: ImageConfig = ImageConfig() - __instance: Optional["Config"] = None - @classmethod async def get(cls) -> "Config": - if cls.__instance is not None: - return cls.__instance + dav_file = DavFile("config.txt") try: - dav_file = DavFile("config.txt") - - cls.__instance = cls.parse_obj( + return cls.parse_obj( toml_loads(await dav_file.string) ) except RemoteResourceNotFound: - cls.__instance = cls() + buffer = BytesIO() + toml_dump(cls().dict(), buffer) + buffer.seek(0) - @run_in_executor - def create_conf() -> None: - buffer = BytesIO() - toml_dump( - cls.__instance.dict(), - buffer, - multiline_strings=True, - ) + await dav_file.dump(buffer.read()) - buffer.seek(0) - webdav_resource("config.txt").read_from(buffer) - - await create_conf() - - return cls.__instance + return cls() diff --git a/api/ovdashboard_api/dav_file.py b/api/ovdashboard_api/dav_file.py index f647a01..137e7a0 100644 --- a/api/ovdashboard_api/dav_file.py +++ b/api/ovdashboard_api/dav_file.py @@ -38,6 +38,10 @@ class DavFile: remote_path=self.remote_path, ) + @property + def resource(self) -> Resource: + return webdav_resource(self.remote_path) + @property async def bytes(self) -> bytes: buffer = await self.__buffer @@ -49,3 +53,11 @@ class DavFile: async def string(self) -> str: bytes = await self.bytes return bytes.decode(encoding="utf-8") + + async def dump(self, content: bytes) -> None: + @run_in_executor + def _inner() -> None: + buffer = BytesIO(content) + self.resource.read_from(buffer) + + await _inner()