90 lines
2.2 KiB
Python
90 lines
2.2 KiB
Python
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 LogConfig(BaseModel):
|
|
"""
|
|
https://stackoverflow.com/a/67937084
|
|
Logging configuration to be set for the server
|
|
"""
|
|
|
|
LOG_FORMAT: str = "%(levelprefix)s [%(asctime)s] %(name)s: %(message)s"
|
|
LOG_LEVEL: str = "DEBUG"
|
|
|
|
# Logging config
|
|
version = 1
|
|
disable_existing_loggers = False
|
|
formatters = {
|
|
"default": {
|
|
"()": "uvicorn.logging.DefaultFormatter",
|
|
"fmt": LOG_FORMAT,
|
|
"datefmt": "%Y-%m-%d %H:%M:%S",
|
|
},
|
|
}
|
|
handlers = {
|
|
"default": {
|
|
"formatter": "default",
|
|
"class": "logging.StreamHandler",
|
|
"stream": "ext://sys.stderr",
|
|
},
|
|
}
|
|
loggers = {
|
|
"ovdashboard_api": {"handlers": ["default"], "level": LOG_LEVEL},
|
|
}
|
|
|
|
|
|
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
|