129 lines
2.8 KiB
Python
129 lines
2.8 KiB
Python
"""
|
|
Router "text" provides:
|
|
|
|
- listing text files
|
|
- finding text files by name prefix
|
|
- getting text file raw content by name prefix
|
|
- getting text file HTML content by name prefix (using Markdown)
|
|
- getting the "ticker" raw content
|
|
- getting the "ticker" HTML content (using Markdown)
|
|
"""
|
|
|
|
import re
|
|
from typing import Iterator
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from markdown import markdown
|
|
|
|
from ..config import Config
|
|
from ..dav_file import DavFile
|
|
from ._common import FileNameLister, PrefixFinder, PrefixUnique
|
|
|
|
router = APIRouter(prefix="/text", tags=["text"])
|
|
|
|
_lister = FileNameLister(
|
|
remote_path="ovdashboard/text",
|
|
re=re.compile(
|
|
r"\.(txt|md)$",
|
|
flags=re.IGNORECASE,
|
|
),
|
|
)
|
|
|
|
_finder = PrefixFinder(_lister)
|
|
_unique = PrefixUnique(_finder)
|
|
|
|
|
|
async def get_ticker_lines() -> Iterator[str]:
|
|
ticker = await DavFile("ovdashboard/text/ticker.txt").string
|
|
|
|
return (
|
|
line.strip()
|
|
for line in ticker.split("\n")
|
|
if line.strip()
|
|
)
|
|
|
|
|
|
async def get_ticker_content_lines(
|
|
ticker_lines: Iterator[str] = Depends(get_ticker_lines),
|
|
) -> Iterator[str]:
|
|
cfg = await Config.get()
|
|
|
|
return (
|
|
line
|
|
for line in ticker_lines
|
|
if not line.startswith(cfg.ticker.comment_marker)
|
|
)
|
|
|
|
|
|
async def get_ticker_content(
|
|
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
|
|
) -> str:
|
|
cfg = await Config.get()
|
|
ticker_content_lines = ["", *ticker_content_lines, ""]
|
|
ticker_content = cfg.ticker.separator.join(ticker_content_lines)
|
|
|
|
return ticker_content.strip()
|
|
|
|
|
|
@ router.get("/get/html/ticker")
|
|
async def get_ticker(
|
|
ticker_content: str = Depends(get_ticker_content),
|
|
) -> str:
|
|
return markdown(ticker_content)
|
|
|
|
|
|
@ router.get("/get/raw/ticker")
|
|
async def get_raw_ticker(
|
|
ticker_content: str = Depends(get_ticker_content),
|
|
) -> str:
|
|
return ticker_content
|
|
|
|
|
|
@router.get(
|
|
"/list",
|
|
response_model=list[str],
|
|
responses=_lister.responses,
|
|
)
|
|
async def list_texts(
|
|
names: Iterator[str] = Depends(_lister),
|
|
) -> list[str]:
|
|
return list(names)
|
|
|
|
|
|
@router.get(
|
|
"/find/{prefix}",
|
|
response_model=list[str],
|
|
responses=_finder.responses,
|
|
)
|
|
async def find_texts(
|
|
names: Iterator[str] = Depends(_finder),
|
|
) -> list[str]:
|
|
return list(names)
|
|
|
|
|
|
async def get_text_content(
|
|
name: str = Depends(_unique),
|
|
) -> str:
|
|
return await DavFile(f"ovdashboard/{_lister.remote_path}/{name}").string
|
|
|
|
|
|
@router.get(
|
|
"/get/html/{prefix}",
|
|
response_model=str,
|
|
responses=_unique.responses,
|
|
)
|
|
async def get_text(
|
|
text: str = Depends(get_text_content),
|
|
) -> str:
|
|
return markdown(text)
|
|
|
|
|
|
@router.get(
|
|
"/get/raw/{prefix}",
|
|
response_model=str,
|
|
responses=_unique.responses,
|
|
)
|
|
async def get_raw_text(
|
|
text: str = Depends(get_text_content),
|
|
) -> str:
|
|
return text
|