109 lines
2.3 KiB
Python
109 lines
2.3 KiB
Python
"""
|
|
Python representation of the "config.txt" file inside the WebDAV directory.
|
|
"""
|
|
|
|
from io import BytesIO
|
|
from logging import getLogger
|
|
from typing import Any
|
|
|
|
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_common import caldav_list
|
|
from .dav_file import DavFile
|
|
from .settings import SETTINGS
|
|
|
|
_logger = getLogger(__name__)
|
|
|
|
|
|
class TickerAPIConfig(BaseModel):
|
|
"""
|
|
Section "[ticker.api]" in "config.txt".
|
|
"""
|
|
|
|
file_name: str = "ticker"
|
|
separator: str = " +++ "
|
|
comment_marker: str = "#"
|
|
|
|
|
|
class TickerUIConfig(BaseModel):
|
|
"""
|
|
Section "[ticker.ui]" in "config.txt".
|
|
"""
|
|
|
|
display: bool = True
|
|
color: str = "primary"
|
|
speed: int = 30
|
|
|
|
|
|
class TickerConfig(BaseModel):
|
|
"""
|
|
Section "[ticker]" in "config.txt".
|
|
"""
|
|
|
|
api: TickerAPIConfig = TickerAPIConfig()
|
|
ui: TickerUIConfig = TickerUIConfig()
|
|
|
|
|
|
class ImageConfig(BaseModel):
|
|
"""
|
|
Sections "[image*]" in "config.txt".
|
|
"""
|
|
|
|
mode: str = "RGB"
|
|
save_params: dict[str, Any] = {
|
|
"format": "JPEG",
|
|
"quality": 85,
|
|
}
|
|
|
|
|
|
class CalendarConfig(BaseModel):
|
|
"""
|
|
Section "[calendar]" in "config.txt".
|
|
"""
|
|
|
|
future_days: int = 365
|
|
aggregate: dict[str, list[str]] = {}
|
|
|
|
|
|
class Config(BaseModel):
|
|
"""
|
|
Main representation of "config.txt".
|
|
"""
|
|
|
|
image_dir: str = "image"
|
|
text_dir: str = "text"
|
|
|
|
ticker: TickerConfig = TickerConfig()
|
|
image: ImageConfig = ImageConfig()
|
|
calendar: CalendarConfig = CalendarConfig()
|
|
|
|
@classmethod
|
|
async def get(cls) -> "Config":
|
|
"""
|
|
Load the configuration instance from the server using `TOML`.
|
|
"""
|
|
|
|
dav_file = DavFile(SETTINGS.config_path)
|
|
|
|
try:
|
|
cfg = cls.parse_obj(
|
|
toml_loads(await dav_file.as_string)
|
|
)
|
|
|
|
except RemoteResourceNotFound:
|
|
_logger.warning(
|
|
f"Config file {SETTINGS.config_path!r} not found, creating ..."
|
|
)
|
|
|
|
cfg = cls()
|
|
cfg.calendar.aggregate["All Events"] = list(await caldav_list())
|
|
|
|
buffer = BytesIO()
|
|
toml_dump(cfg.dict(), buffer)
|
|
buffer.seek(0)
|
|
await dav_file.write(buffer.read())
|
|
|
|
return cfg
|