2022-09-04 23:03:16 +00:00
|
|
|
from io import BytesIO
|
2022-09-04 23:40:56 +00:00
|
|
|
from typing import Any
|
2022-09-04 23:03:16 +00:00
|
|
|
|
|
|
|
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 .dav_file import DavFile
|
|
|
|
|
|
|
|
|
2022-09-04 23:25:40 +00:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-04 23:03:16 +00:00
|
|
|
class ImageConfig(BaseModel):
|
|
|
|
mode: str = "RGB"
|
|
|
|
save_params: dict[str, Any] = {
|
|
|
|
"format": "JPEG",
|
|
|
|
"quality": 85,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Config(BaseModel):
|
|
|
|
ticker_separator: str = " +++ "
|
|
|
|
|
|
|
|
image: ImageConfig = ImageConfig()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def get(cls) -> "Config":
|
2022-09-04 23:40:56 +00:00
|
|
|
dav_file = DavFile("config.txt")
|
2022-09-04 23:03:16 +00:00
|
|
|
|
|
|
|
try:
|
2022-09-04 23:40:56 +00:00
|
|
|
return cls.parse_obj(
|
2022-09-04 23:03:16 +00:00
|
|
|
toml_loads(await dav_file.string)
|
|
|
|
)
|
|
|
|
|
|
|
|
except RemoteResourceNotFound:
|
2022-09-04 23:40:56 +00:00
|
|
|
buffer = BytesIO()
|
|
|
|
toml_dump(cls().dict(), buffer)
|
|
|
|
buffer.seek(0)
|
2022-09-04 23:03:16 +00:00
|
|
|
|
2022-09-04 23:40:56 +00:00
|
|
|
await dav_file.dump(buffer.read())
|
2022-09-04 23:03:16 +00:00
|
|
|
|
2022-09-04 23:40:56 +00:00
|
|
|
return cls()
|