ovdashboard/api/ovdashboard_api/config.py

116 lines
2.5 KiB
Python
Raw Normal View History

2022-09-05 12:54:02 +00:00
"""
Python representation of the "config.txt" file inside the WebDAV directory.
"""
from io import BytesIO
2022-09-06 22:55:23 +00:00
from logging import getLogger
2022-09-04 23:40:56 +00:00
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
2022-09-06 22:20:01 +00:00
from .dav_common import caldav_list
from .dav_file import DavFile
2022-09-06 22:55:23 +00:00
from .settings import SETTINGS
_logger = getLogger(__name__)
2022-09-08 23:59:17 +00:00
class TickerAPIConfig(BaseModel):
2022-09-04 23:25:40 +00:00
"""
2022-09-08 23:59:17 +00:00
Section "[ticker.api]" in "config.txt".
2022-09-09 14:12:36 +00:00
Configuration for how the API generates the ticker content.
2022-09-04 23:25:40 +00:00
"""
2022-09-08 14:02:50 +00:00
file_name: str = "ticker"
separator: str = " +++ "
comment_marker: str = "#"
2022-09-08 23:59:17 +00:00
class TickerUIConfig(BaseModel):
"""
Section "[ticker.ui]" in "config.txt".
2022-09-09 14:12:36 +00:00
Configuration for how the UI displays the ticker content.
2022-09-08 23:59:17 +00:00
"""
display: bool = True
color: str = "primary"
speed: int = 30
2022-09-04 23:25:40 +00:00
2022-09-08 23:59:17 +00:00
class TickerConfig(BaseModel):
"""
Section "[ticker]" in "config.txt".
2022-09-09 14:12:36 +00:00
Combined configuration for the ticker.
2022-09-08 23:59:17 +00:00
"""
api: TickerAPIConfig = TickerAPIConfig()
ui: TickerUIConfig = TickerUIConfig()
class ImageConfig(BaseModel):
2022-09-05 12:54:02 +00:00
"""
Sections "[image*]" in "config.txt".
2022-09-05 12:54:02 +00:00
"""
mode: str = "RGB"
save_params: dict[str, Any] = {
"format": "JPEG",
"quality": 85,
}
2022-09-06 00:03:44 +00:00
class CalendarConfig(BaseModel):
"""
2022-09-09 14:12:36 +00:00
Sections "[calendar*]" in "config.txt".
2022-09-06 00:03:44 +00:00
"""
future_days: int = 365
2022-09-09 14:12:36 +00:00
aggregates: dict[str, list[str]] = {}
2022-09-06 00:03:44 +00:00
class Config(BaseModel):
2022-09-05 12:54:02 +00:00
"""
Main representation of "config.txt".
"""
2022-09-08 14:02:50 +00:00
image_dir: str = "image"
text_dir: str = "text"
ticker: TickerConfig = TickerConfig()
image: ImageConfig = ImageConfig()
2022-09-06 00:03:44 +00:00
calendar: CalendarConfig = CalendarConfig()
@classmethod
async def get(cls) -> "Config":
2022-09-05 12:54:02 +00:00
"""
Load the configuration instance from the server using `TOML`.
"""
2022-09-06 22:55:23 +00:00
dav_file = DavFile(SETTINGS.config_path)
try:
2022-09-08 23:59:17 +00:00
cfg = cls.parse_obj(
2022-09-08 00:24:36 +00:00
toml_loads(await dav_file.as_string)
)
except RemoteResourceNotFound:
2022-09-07 00:17:55 +00:00
_logger.warning(
2022-09-06 22:55:23 +00:00
f"Config file {SETTINGS.config_path!r} not found, creating ..."
)
cfg = cls()
2022-09-09 14:12:36 +00:00
cfg.calendar.aggregates["All Events"] = list(await caldav_list())
2022-09-04 23:40:56 +00:00
buffer = BytesIO()
toml_dump(cfg.dict(), buffer)
2022-09-04 23:40:56 +00:00
buffer.seek(0)
2022-09-06 22:55:23 +00:00
await dav_file.write(buffer.read())
2022-09-08 23:59:17 +00:00
return cfg