ovdashboard/api/ovdashboard_api/routers/v1/_common.py

142 lines
3.3 KiB
Python
Raw Normal View History

2022-09-05 12:54:02 +00:00
"""
Dependables for defining Routers.
"""
2023-10-26 16:21:07 +00:00
import logging
2022-09-02 13:22:35 +00:00
import re
2023-10-26 16:26:51 +00:00
import tomllib
2022-09-02 13:22:35 +00:00
2023-10-26 16:26:51 +00:00
import tomli_w
2023-10-26 14:31:12 +00:00
from fastapi import Depends, HTTPException, params, status
2022-09-02 13:22:35 +00:00
from webdav3.exceptions import RemoteResourceNotFound
2023-10-26 15:46:12 +00:00
from ...core.caldav import CalDAV
2023-10-26 16:26:51 +00:00
from ...core.config import Config
from ...core.settings import SETTINGS
2023-10-20 08:43:15 +00:00
from ...core.webdav import WebDAV
2023-10-26 16:26:51 +00:00
from ._list_manager import Dependable, DependableFn, ListManager
2022-09-02 13:22:35 +00:00
2023-10-26 16:21:07 +00:00
_logger = logging.getLogger(__name__)
2022-09-08 14:02:50 +00:00
2022-09-05 00:23:00 +00:00
_RESPONSE_OK = {
status.HTTP_200_OK: {
"description": "Operation successful",
},
}
2023-10-23 21:32:25 +00:00
2023-10-26 16:26:51 +00:00
async def get_config() -> Config:
"""
Load the configuration instance from the server using `TOML`.
"""
2023-10-26 13:58:02 +00:00
2023-10-26 16:26:51 +00:00
try:
cfg_str = await WebDAV.read_str(SETTINGS.webdav.config_filename)
cfg = Config.model_validate(tomllib.loads(cfg_str))
2023-10-23 21:32:25 +00:00
2023-10-26 16:26:51 +00:00
except RemoteResourceNotFound:
_logger.warning(
f"Config file {SETTINGS.webdav.config_filename!r} not found, creating ..."
)
2023-10-26 13:58:02 +00:00
2023-10-26 16:26:51 +00:00
cfg = Config()
cfg.calendar.aggregates["All Events"] = list(await CalDAV.calendars)
2023-10-26 13:58:02 +00:00
2023-10-26 16:26:51 +00:00
await WebDAV.write_str(
SETTINGS.webdav.config_filename,
tomli_w.dumps(cfg.model_dump()),
2023-10-26 13:58:02 +00:00
)
2023-10-23 21:44:09 +00:00
2023-10-26 16:26:51 +00:00
return cfg
2023-10-26 15:46:12 +00:00
2022-09-05 00:23:00 +00:00
2023-10-23 21:32:25 +00:00
def get_remote_path(
2023-10-22 14:25:19 +00:00
path_name: str,
2023-10-26 15:48:33 +00:00
) -> DependableFn[[], str]:
2023-10-23 21:32:25 +00:00
async def _get_remote_path() -> str:
cfg = await get_config()
return getattr(cfg, path_name)
2023-10-26 13:58:02 +00:00
return _get_remote_path
2023-10-23 18:56:01 +00:00
2023-10-26 14:31:12 +00:00
RP_FILE = get_remote_path("file_dir")
RP_IMAGE = get_remote_path("image_dir")
RP_TEXT = get_remote_path("text_dir")
def get_file_lister(
2023-10-26 15:48:33 +00:00
rp: DependableFn[[], str],
2023-10-22 14:25:19 +00:00
*,
2023-10-23 21:32:25 +00:00
re: re.Pattern[str],
2023-10-26 13:58:02 +00:00
) -> Dependable[[], list[str]]:
2023-10-22 14:25:19 +00:00
"""
List files in remote `path` matching the RegEx `re`
"""
2023-10-26 13:58:02 +00:00
async def _list_files(
remote_path: str = Depends(rp),
) -> list[str]:
2023-10-26 14:31:12 +00:00
if isinstance(remote_path, params.Depends):
remote_path = await rp()
_logger.debug("list %s", repr(remote_path))
2023-10-23 21:32:25 +00:00
try:
2023-10-26 13:58:02 +00:00
return await WebDAV.list_files(remote_path, regex=re)
2023-10-22 14:25:19 +00:00
2023-10-23 21:32:25 +00:00
except RemoteResourceNotFound:
2023-10-26 13:58:02 +00:00
_logger.error("WebDAV path %s lost!", repr(remote_path))
2023-10-23 21:32:25 +00:00
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return Dependable(
func=_list_files,
responses={
**_RESPONSE_OK,
status.HTTP_404_NOT_FOUND: {
2023-10-26 13:58:02 +00:00
"description": "Remote path not found",
2023-10-23 21:32:25 +00:00
"content": None,
},
},
)
2023-10-22 14:25:19 +00:00
2023-10-26 15:46:12 +00:00
LM_FILE = ListManager.from_lister(
2023-10-26 14:31:12 +00:00
get_file_lister(rp=RP_FILE, re=re.compile(r"[^/]$", flags=re.IGNORECASE))
)
2023-10-26 15:46:12 +00:00
LM_IMAGE = ListManager.from_lister(
2023-10-26 14:31:12 +00:00
get_file_lister(
rp=RP_IMAGE, re=re.compile(r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE)
)
)
2023-10-26 15:46:12 +00:00
LM_TEXT = ListManager.from_lister(
2023-10-26 14:31:12 +00:00
get_file_lister(rp=RP_TEXT, re=re.compile(r"\.(txt|md)$", flags=re.IGNORECASE))
)
2023-10-26 15:46:12 +00:00
async def list_calendar_names() -> list[str]:
"""
List calendar names
"""
return await CalDAV.calendars
LM_CALENDARS = ListManager.from_lister_fn(list_calendar_names)
2023-10-22 14:25:19 +00:00
2023-10-26 15:58:54 +00:00
async def list_aggregate_names(
cfg: Config = Depends(get_config),
) -> list[str]:
"""
List aggregate calendar names
"""
if isinstance(cfg, params.Depends):
cfg = await get_config()
2023-10-26 15:46:12 +00:00
2023-10-26 15:58:54 +00:00
return list(cfg.calendar.aggregates.keys())
2023-10-26 15:46:12 +00:00
2023-10-26 15:58:54 +00:00
LM_AGGREGATES = ListManager.from_lister_fn(list_aggregate_names)