Config for ticker and aggregate Calendars
This commit is contained in:
parent
a069fe5078
commit
e804ae68f8
3 changed files with 39 additions and 12 deletions
|
@ -10,21 +10,25 @@ from tomli import loads as toml_loads
|
|||
from tomli_w import dump as toml_dump
|
||||
from webdav3.exceptions import RemoteResourceNotFound
|
||||
|
||||
from ovdashboard_api.dav_common import caldav_list
|
||||
|
||||
from .dav_file import DavFile
|
||||
|
||||
|
||||
class TickerConfig(BaseModel):
|
||||
"""
|
||||
Sections "[ticker.*]" in "config.txt".
|
||||
Section "[ticker]" in "config.txt".
|
||||
"""
|
||||
|
||||
separator: str = " +++ "
|
||||
comment_marker: str = "#"
|
||||
color: str = "primary"
|
||||
speed: int = 30
|
||||
|
||||
|
||||
class ImageConfig(BaseModel):
|
||||
"""
|
||||
Sections "[image.*]" in "config.txt".
|
||||
Sections "[image*]" in "config.txt".
|
||||
"""
|
||||
|
||||
mode: str = "RGB"
|
||||
|
@ -34,6 +38,15 @@ class ImageConfig(BaseModel):
|
|||
}
|
||||
|
||||
|
||||
class AggregateCalendarConfig(BaseModel):
|
||||
"""
|
||||
Sections "[[aggregate]]" in "config.txt".
|
||||
"""
|
||||
|
||||
name: str = "All Events"
|
||||
calendars: list[str]
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
"""
|
||||
Main representation of "config.txt".
|
||||
|
@ -41,6 +54,7 @@ class Config(BaseModel):
|
|||
|
||||
ticker: TickerConfig = TickerConfig()
|
||||
image: ImageConfig = ImageConfig()
|
||||
aggregate: list[AggregateCalendarConfig] = []
|
||||
|
||||
@classmethod
|
||||
async def get(cls) -> "Config":
|
||||
|
@ -56,10 +70,14 @@ class Config(BaseModel):
|
|||
)
|
||||
|
||||
except RemoteResourceNotFound:
|
||||
buffer = BytesIO()
|
||||
toml_dump(cls().dict(), buffer)
|
||||
buffer.seek(0)
|
||||
cfg = cls()
|
||||
cfg.aggregate.append(
|
||||
AggregateCalendarConfig(calendars=await caldav_list()),
|
||||
)
|
||||
|
||||
buffer = BytesIO()
|
||||
toml_dump(cfg.dict(), buffer)
|
||||
buffer.seek(0)
|
||||
await dav_file.dump(buffer.read())
|
||||
|
||||
return cls()
|
||||
return cfg
|
||||
|
|
|
@ -3,7 +3,7 @@ Definition of WebDAV and CalDAV clients.
|
|||
"""
|
||||
|
||||
from functools import lru_cache
|
||||
from typing import Any
|
||||
from typing import Any, Iterator
|
||||
|
||||
from caldav import DAVClient as CalDAVclient
|
||||
from caldav import Principal as CalDAVPrincipal
|
||||
|
@ -55,3 +55,15 @@ def caldav_principal() -> CalDAVPrincipal:
|
|||
"""
|
||||
|
||||
return _CALDAV_CLIENT.principal()
|
||||
|
||||
|
||||
@run_in_executor
|
||||
def caldav_list() -> Iterator[str]:
|
||||
"""
|
||||
Asynchroneously lists all calendars using the main WebDAV client.
|
||||
"""
|
||||
|
||||
return (
|
||||
cal.name
|
||||
for cal in caldav_principal().calendars()
|
||||
)
|
||||
|
|
|
@ -9,7 +9,7 @@ from typing import Iterator, Protocol
|
|||
from fastapi import HTTPException, status
|
||||
from webdav3.exceptions import RemoteResourceNotFound
|
||||
|
||||
from ..dav_common import caldav_principal, webdav_list
|
||||
from ..dav_common import caldav_list, webdav_list
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
@ -71,10 +71,7 @@ class CalendarNameLister:
|
|||
"""
|
||||
|
||||
async def __call__(self) -> Iterator[str]:
|
||||
return (
|
||||
cal.name
|
||||
for cal in caldav_principal().calendars()
|
||||
)
|
||||
return await caldav_list()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
Loading…
Reference in a new issue