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-29 11:27:18 +00:00
|
|
|
|
2022-09-02 14:51:11 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
2022-09-02 12:20:29 +00:00
|
|
|
from markdown import markdown
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2023-10-20 11:40:15 +00:00
|
|
|
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
|
|
|
|
from ...core.webdav import WebDAV
|
2023-10-23 21:32:25 +00:00
|
|
|
from ._common import filter_prefix, filter_prefix_unique, get_remote_path, list_files
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-09-08 14:02:50 +00:00
|
|
|
_logger = getLogger(__name__)
|
2023-10-23 21:32:25 +00:00
|
|
|
_PATH_NAME = "text_dir"
|
2022-09-08 14:02:50 +00:00
|
|
|
|
2022-08-29 11:27:18 +00:00
|
|
|
router = APIRouter(prefix="/text", tags=["text"])
|
|
|
|
|
2023-10-23 21:32:25 +00:00
|
|
|
_ls = list_files(
|
|
|
|
path_name=_PATH_NAME,
|
2022-09-02 13:22:35 +00:00
|
|
|
re=re.compile(
|
|
|
|
r"\.(txt|md)$",
|
|
|
|
flags=re.IGNORECASE,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2023-10-23 21:32:25 +00:00
|
|
|
_rp = get_remote_path(path_name=_PATH_NAME)
|
2023-10-23 21:44:09 +00:00
|
|
|
_fp = filter_prefix(_ls)
|
|
|
|
_fpu = filter_prefix_unique(_fp)
|
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.")
|
|
|
|
|
2023-10-25 18:50:03 +00:00
|
|
|
remote_path = await _rp.func()
|
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-23 21:32:25 +00:00
|
|
|
responses=_ls.responses,
|
2022-09-05 00:23:00 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def list_all_texts(
|
|
|
|
names: list[str] = Depends(_ls.func),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> list[str]:
|
2023-10-23 21:32:25 +00:00
|
|
|
return names
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
|
2022-09-05 00:23:00 +00:00
|
|
|
@router.get(
|
|
|
|
"/find/{prefix}",
|
2023-10-23 21:32:25 +00:00
|
|
|
responses=_fp.responses,
|
2022-09-05 00:23:00 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def find_texts_by_prefix(
|
|
|
|
names: list[str] = Depends(_fp.func),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> list[str]:
|
2023-10-23 21:32:25 +00:00
|
|
|
return names
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
|
2023-10-23 21:32:25 +00:00
|
|
|
async def _get_raw_text_by_prefix(
|
|
|
|
remote_path: str = Depends(_rp.func),
|
|
|
|
name: str = Depends(_fpu.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
|
|
|
|
|
|
|
|
2022-09-02 12:50:43 +00:00
|
|
|
@router.get(
|
2023-10-23 21:32:25 +00:00
|
|
|
"/get/raw/{prefix}",
|
|
|
|
responses=_fpu.responses,
|
2022-09-02 12:50:43 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def get_raw_text_by_prefix(
|
|
|
|
text: str = Depends(_get_raw_text_by_prefix),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> str:
|
2023-10-23 21:32:25 +00:00
|
|
|
return text
|
2022-09-02 12:20:29 +00:00
|
|
|
|
2022-09-05 12:58:10 +00:00
|
|
|
|
|
|
|
@router.get(
|
2023-10-23 21:32:25 +00:00
|
|
|
"/get/html/{prefix}",
|
|
|
|
responses=_fpu.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-23 21:32:25 +00:00
|
|
|
return markdown(text)
|