2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Python representation of the "config.txt" file inside the WebDAV directory.
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
2022-09-05 23:53:53 +00:00
|
|
|
from ovdashboard_api.dav_common import caldav_list
|
|
|
|
|
2022-09-04 23:03:16 +00:00
|
|
|
from .dav_file import DavFile
|
|
|
|
|
|
|
|
|
2022-09-05 13:11:57 +00:00
|
|
|
class TickerConfig(BaseModel):
|
2022-09-04 23:25:40 +00:00
|
|
|
"""
|
2022-09-05 23:53:53 +00:00
|
|
|
Section "[ticker]" in "config.txt".
|
2022-09-04 23:25:40 +00:00
|
|
|
"""
|
|
|
|
|
2022-09-05 13:11:57 +00:00
|
|
|
separator: str = " +++ "
|
|
|
|
comment_marker: str = "#"
|
2022-09-05 23:53:53 +00:00
|
|
|
color: str = "primary"
|
|
|
|
speed: int = 30
|
2022-09-04 23:25:40 +00:00
|
|
|
|
|
|
|
|
2022-09-04 23:03:16 +00:00
|
|
|
class ImageConfig(BaseModel):
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
2022-09-05 23:53:53 +00:00
|
|
|
Sections "[image*]" in "config.txt".
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
|
2022-09-04 23:03:16 +00:00
|
|
|
mode: str = "RGB"
|
|
|
|
save_params: dict[str, Any] = {
|
|
|
|
"format": "JPEG",
|
|
|
|
"quality": 85,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-06 00:03:44 +00:00
|
|
|
class CalAggregateConfig(BaseModel):
|
2022-09-05 23:53:53 +00:00
|
|
|
"""
|
2022-09-06 00:03:44 +00:00
|
|
|
Sections "[[calendar.aggregate]]" in "config.txt".
|
2022-09-05 23:53:53 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
name: str = "All Events"
|
|
|
|
calendars: list[str]
|
|
|
|
|
|
|
|
|
2022-09-06 00:03:44 +00:00
|
|
|
class CalendarConfig(BaseModel):
|
|
|
|
"""
|
|
|
|
Section "[calendar]" in "config.txt".
|
|
|
|
"""
|
|
|
|
|
|
|
|
future_days: int = 365
|
|
|
|
aggregate: list[CalAggregateConfig] = []
|
|
|
|
|
|
|
|
|
2022-09-04 23:03:16 +00:00
|
|
|
class Config(BaseModel):
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
Main representation of "config.txt".
|
|
|
|
"""
|
|
|
|
|
2022-09-05 13:11:57 +00:00
|
|
|
ticker: TickerConfig = TickerConfig()
|
2022-09-04 23:03:16 +00:00
|
|
|
image: ImageConfig = ImageConfig()
|
2022-09-06 00:03:44 +00:00
|
|
|
calendar: CalendarConfig = CalendarConfig()
|
2022-09-04 23:03:16 +00:00
|
|
|
|
|
|
|
@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-05 20:17:27 +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-05 23:53:53 +00:00
|
|
|
cfg = cls()
|
2022-09-06 00:03:44 +00:00
|
|
|
cfg.calendar.aggregate.append(
|
|
|
|
CalAggregateConfig(calendars=await caldav_list()),
|
2022-09-05 23:53:53 +00:00
|
|
|
)
|
|
|
|
|
2022-09-04 23:40:56 +00:00
|
|
|
buffer = BytesIO()
|
2022-09-05 23:53:53 +00:00
|
|
|
toml_dump(cfg.dict(), buffer)
|
2022-09-04 23:40:56 +00:00
|
|
|
buffer.seek(0)
|
|
|
|
await dav_file.dump(buffer.read())
|
2022-09-04 23:03:16 +00:00
|
|
|
|
2022-09-05 23:53:53 +00:00
|
|
|
return cfg
|