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

83 lines
1.8 KiB
Python
Raw Permalink Normal View History

2022-09-05 12:54:02 +00:00
"""
Router "text" provides:
- listing text files
- finding text files by name prefix
2022-09-05 12:58:10 +00:00
- getting text file raw content by name prefix
- getting text file HTML content by name prefix (using Markdown)
2022-09-05 12:54:02 +00:00
"""
2023-10-26 16:21:07 +00:00
import logging
2022-08-29 11:27:18 +00:00
2023-10-26 16:21:07 +00:00
import markdown
2022-09-02 14:51:11 +00:00
from fastapi import APIRouter, Depends
2022-08-29 11:27:18 +00:00
2023-11-09 11:13:48 +00:00
from ...core.dav.webdav import WebDAV
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
2023-10-26 14:31:12 +00:00
from ._common import LM_TEXT, RP_TEXT
2022-08-29 11:27:18 +00:00
2023-10-26 16:21:07 +00:00
_logger = logging.getLogger(__name__)
2022-09-08 14:02:50 +00:00
2022-08-29 11:27:18 +00:00
router = APIRouter(prefix="/text", tags=["text"])
2022-09-08 14:02:50 +00:00
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
2023-10-26 14:31:12 +00:00
remote_path = await RP_TEXT()
2023-10-23 21:32:25 +00:00
if not webdav_ensure_path(remote_path):
webdav_ensure_files(
remote_path,
"message.txt",
"title.txt",
"ticker.txt",
)
2022-09-19 11:42:43 +00:00
2022-09-08 14:02:50 +00:00
2022-09-05 00:23:00 +00:00
@router.get(
"/list",
2023-10-26 14:31:12 +00:00
responses=LM_TEXT.lister.responses,
2022-09-05 00:23:00 +00:00
)
2023-10-26 21:17:38 +00:00
async def list_texts(
2023-10-26 14:31:12 +00:00
names: list[str] = Depends(LM_TEXT.lister.func),
) -> list[str]:
2023-10-23 21:32:25 +00:00
return names
2022-09-05 00:23:00 +00:00
@router.get(
"/find/{prefix}",
2023-10-26 14:31:12 +00:00
responses=LM_TEXT.filter.responses,
2022-09-05 00:23:00 +00:00
)
2023-10-23 21:32:25 +00:00
async def find_texts_by_prefix(
2023-10-26 14:31:12 +00:00
names: list[str] = Depends(LM_TEXT.filter.func),
) -> list[str]:
2023-10-23 21:32:25 +00:00
return names
2023-10-23 21:32:25 +00:00
async def _get_raw_text_by_prefix(
2023-10-26 14:31:12 +00:00
remote_path: str = Depends(RP_TEXT),
name: str = Depends(LM_TEXT.getter.func),
2022-09-05 12:58:10 +00:00
) -> str:
2023-10-23 21:32:25 +00:00
return await WebDAV.read_str(f"{remote_path}/{name}")
2022-09-05 12:58:10 +00:00
@router.get(
2023-10-23 21:32:25 +00:00
"/get/raw/{prefix}",
2023-10-26 14:31:12 +00:00
responses=LM_TEXT.getter.responses,
)
2023-10-23 21:32:25 +00:00
async def get_raw_text_by_prefix(
text: str = Depends(_get_raw_text_by_prefix),
) -> str:
2023-10-23 21:32:25 +00:00
return text
2022-09-05 12:58:10 +00:00
@router.get(
2023-10-23 21:32:25 +00:00
"/get/html/{prefix}",
2023-10-26 14:31:12 +00:00
responses=LM_TEXT.getter.responses,
2022-09-05 12:58:10 +00:00
)
2023-10-23 21:32:25 +00:00
async def get_html_by_prefix(
text: str = Depends(_get_raw_text_by_prefix),
2022-09-05 12:58:10 +00:00
) -> str:
2023-10-26 16:21:07 +00:00
return markdown.markdown(text)