config creation
This commit is contained in:
parent
161e0e9628
commit
c903144657
2 changed files with 20 additions and 24 deletions
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue