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 io import BytesIO
|
||||||
from typing import Any, Optional
|
from typing import Any
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from tomli import loads as toml_loads
|
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 webdav3.exceptions import RemoteResourceNotFound
|
||||||
|
|
||||||
from .async_helpers import run_in_executor
|
from .async_helpers import run_in_executor
|
||||||
from .dav_common import webdav_resource
|
|
||||||
from .dav_file import DavFile
|
from .dav_file import DavFile
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,35 +55,20 @@ class Config(BaseModel):
|
||||||
|
|
||||||
image: ImageConfig = ImageConfig()
|
image: ImageConfig = ImageConfig()
|
||||||
|
|
||||||
__instance: Optional["Config"] = None
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def get(cls) -> "Config":
|
async def get(cls) -> "Config":
|
||||||
if cls.__instance is not None:
|
dav_file = DavFile("config.txt")
|
||||||
return cls.__instance
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dav_file = DavFile("config.txt")
|
return cls.parse_obj(
|
||||||
|
|
||||||
cls.__instance = cls.parse_obj(
|
|
||||||
toml_loads(await dav_file.string)
|
toml_loads(await dav_file.string)
|
||||||
)
|
)
|
||||||
|
|
||||||
except RemoteResourceNotFound:
|
except RemoteResourceNotFound:
|
||||||
cls.__instance = cls()
|
buffer = BytesIO()
|
||||||
|
toml_dump(cls().dict(), buffer)
|
||||||
|
buffer.seek(0)
|
||||||
|
|
||||||
@run_in_executor
|
await dav_file.dump(buffer.read())
|
||||||
def create_conf() -> None:
|
|
||||||
buffer = BytesIO()
|
|
||||||
toml_dump(
|
|
||||||
cls.__instance.dict(),
|
|
||||||
buffer,
|
|
||||||
multiline_strings=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
buffer.seek(0)
|
return cls()
|
||||||
webdav_resource("config.txt").read_from(buffer)
|
|
||||||
|
|
||||||
await create_conf()
|
|
||||||
|
|
||||||
return cls.__instance
|
|
||||||
|
|
|
@ -38,6 +38,10 @@ class DavFile:
|
||||||
remote_path=self.remote_path,
|
remote_path=self.remote_path,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def resource(self) -> Resource:
|
||||||
|
return webdav_resource(self.remote_path)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
async def bytes(self) -> bytes:
|
async def bytes(self) -> bytes:
|
||||||
buffer = await self.__buffer
|
buffer = await self.__buffer
|
||||||
|
@ -49,3 +53,11 @@ class DavFile:
|
||||||
async def string(self) -> str:
|
async def string(self) -> str:
|
||||||
bytes = await self.bytes
|
bytes = await self.bytes
|
||||||
return bytes.decode(encoding="utf-8")
|
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