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

99 lines
2 KiB
Python
Raw 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
"""
2022-08-31 01:55:17 +00:00
import re
2022-09-08 14:02:50 +00:00
from logging import getLogger
2022-08-31 01:55:17 +00:00
from typing import Iterator
2022-08-29 11:27:18 +00:00
2022-09-02 14:51:11 +00:00
from fastapi import APIRouter, Depends
from markdown import markdown
2022-08-29 11:27:18 +00:00
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
from ...core.webdav import WebDAV
2022-09-04 14:14:22 +00:00
from ._common import FileNameLister, PrefixFinder, PrefixUnique
2022-08-29 11:27:18 +00:00
2022-09-08 14:02:50 +00:00
_logger = getLogger(__name__)
2022-08-29 11:27:18 +00:00
router = APIRouter(prefix="/text", tags=["text"])
2022-09-06 23:50:42 +00:00
text_lister = FileNameLister(
2022-09-08 14:02:50 +00:00
path_name="text_dir",
2022-09-02 13:22:35 +00:00
re=re.compile(
r"\.(txt|md)$",
flags=re.IGNORECASE,
),
)
2022-09-06 23:50:42 +00:00
text_finder = PrefixFinder(text_lister)
text_unique = PrefixUnique(text_finder)
2022-09-02 13:22:35 +00:00
2022-08-29 11:27:18 +00:00
2022-09-08 14:02:50 +00:00
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
webdav_ensure_path(await text_lister.remote_path)
2022-09-19 11:42:43 +00:00
webdav_ensure_files(
await text_lister.remote_path,
"message.txt",
"title.txt",
"ticker.txt",
)
2022-09-08 14:02:50 +00:00
2022-09-05 00:23:00 +00:00
@router.get(
"/list",
response_model=list[str],
2022-09-06 23:50:42 +00:00
responses=text_lister.responses,
2022-09-05 00:23:00 +00:00
)
async def list_texts(
2022-09-06 23:50:42 +00:00
names: Iterator[str] = Depends(text_lister),
) -> list[str]:
2022-09-04 14:14:22 +00:00
return list(names)
2022-09-05 00:23:00 +00:00
@router.get(
"/find/{prefix}",
response_model=list[str],
2022-09-06 23:50:42 +00:00
responses=text_finder.responses,
2022-09-05 00:23:00 +00:00
)
async def find_texts(
2022-09-06 23:50:42 +00:00
names: Iterator[str] = Depends(text_finder),
) -> list[str]:
2022-09-04 14:14:22 +00:00
return list(names)
2022-09-05 12:58:10 +00:00
async def get_text_content(
2022-09-06 23:50:42 +00:00
name: str = Depends(text_unique),
2022-09-05 12:58:10 +00:00
) -> str:
return await WebDAV.read_str(f"{await text_lister.remote_path}/{name}")
2022-09-05 12:58:10 +00:00
@router.get(
2022-09-05 12:58:10 +00:00
"/get/html/{prefix}",
2022-09-02 13:22:35 +00:00
response_model=str,
2022-09-06 23:50:42 +00:00
responses=text_unique.responses,
)
async def get_text(
2022-09-05 12:58:10 +00:00
text: str = Depends(get_text_content),
) -> str:
2022-09-05 12:58:10 +00:00
return markdown(text)
2022-09-05 12:58:10 +00:00
@router.get(
"/get/raw/{prefix}",
response_model=str,
2022-09-06 23:50:42 +00:00
responses=text_unique.responses,
2022-09-05 12:58:10 +00:00
)
async def get_raw_text(
text: str = Depends(get_text_content),
) -> str:
return text