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:
|
|
||||||
return cls.__instance
|
|
||||||
|
|
||||||
try:
|
|
||||||
dav_file = DavFile("config.txt")
|
dav_file = DavFile("config.txt")
|
||||||
|
|
||||||
cls.__instance = cls.parse_obj(
|
try:
|
||||||
|
return cls.parse_obj(
|
||||||
toml_loads(await dav_file.string)
|
toml_loads(await dav_file.string)
|
||||||
)
|
)
|
||||||
|
|
||||||
except RemoteResourceNotFound:
|
except RemoteResourceNotFound:
|
||||||
cls.__instance = cls()
|
|
||||||
|
|
||||||
@run_in_executor
|
|
||||||
def create_conf() -> None:
|
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
toml_dump(
|
toml_dump(cls().dict(), buffer)
|
||||||
cls.__instance.dict(),
|
|
||||||
buffer,
|
|
||||||
multiline_strings=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
buffer.seek(0)
|
buffer.seek(0)
|
||||||
webdav_resource("config.txt").read_from(buffer)
|
|
||||||
|
|
||||||
await create_conf()
|
await dav_file.dump(buffer.read())
|
||||||
|
|
||||||
return cls.__instance
|
return cls()
|
||||||
|
|
|
@ -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