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)
|
|
|
|
- getting the "ticker" raw content
|
|
|
|
- getting the "ticker" HTML content (using Markdown)
|
2022-09-05 12:54:02 +00:00
|
|
|
"""
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
import re
|
|
|
|
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
|
2022-09-02 12:20:29 +00:00
|
|
|
from markdown import markdown
|
2022-08-29 11:27:18 +00:00
|
|
|
|
2022-09-04 23:58:45 +00:00
|
|
|
from ..config import Config
|
2022-08-29 11:27:18 +00:00
|
|
|
from ..dav_file import DavFile
|
2022-09-04 14:14:22 +00:00
|
|
|
from ._common import FileNameLister, PrefixFinder, PrefixUnique
|
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-05 20:17:27 +00:00
|
|
|
remote_path="text",
|
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-08-31 01:55:17 +00:00
|
|
|
async def get_ticker_lines() -> Iterator[str]:
|
2022-09-05 20:17:27 +00:00
|
|
|
ticker = await DavFile("text/ticker.txt").string
|
2022-09-02 12:20:29 +00:00
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
return (
|
2022-08-29 19:20:03 +00:00
|
|
|
line.strip()
|
2022-09-02 12:20:29 +00:00
|
|
|
for line in ticker.split("\n")
|
2022-08-31 01:55:17 +00:00
|
|
|
if line.strip()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_ticker_content_lines(
|
|
|
|
ticker_lines: Iterator[str] = Depends(get_ticker_lines),
|
|
|
|
) -> Iterator[str]:
|
2022-09-05 13:11:57 +00:00
|
|
|
cfg = await Config.get()
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
return (
|
|
|
|
line
|
|
|
|
for line in ticker_lines
|
2022-09-05 13:11:57 +00:00
|
|
|
if not line.startswith(cfg.ticker.comment_marker)
|
2022-08-29 19:20:03 +00:00
|
|
|
)
|
|
|
|
|
2022-08-31 01:55:17 +00:00
|
|
|
|
|
|
|
async def get_ticker_content(
|
|
|
|
ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines),
|
|
|
|
) -> str:
|
2022-09-04 23:58:45 +00:00
|
|
|
cfg = await Config.get()
|
|
|
|
ticker_content_lines = ["", *ticker_content_lines, ""]
|
2022-09-05 13:11:57 +00:00
|
|
|
ticker_content = cfg.ticker.separator.join(ticker_content_lines)
|
2022-09-04 23:58:45 +00:00
|
|
|
|
2022-09-05 12:58:10 +00:00
|
|
|
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
|
2022-08-31 01:55:17 +00:00
|
|
|
|
2022-09-02 12:50:43 +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
|
|
|
)
|
2022-09-02 12:50:43 +00:00
|
|
|
async def list_texts(
|
2022-09-06 23:50:42 +00:00
|
|
|
names: Iterator[str] = Depends(text_lister),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> list[str]:
|
2022-09-04 14:14:22 +00:00
|
|
|
return list(names)
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
)
|
2022-09-02 12:50:43 +00:00
|
|
|
async def find_texts(
|
2022-09-06 23:50:42 +00:00
|
|
|
names: Iterator[str] = Depends(text_finder),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> list[str]:
|
2022-09-04 14:14:22 +00:00
|
|
|
return list(names)
|
2022-09-02 12:50:43 +00:00
|
|
|
|
|
|
|
|
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:
|
2022-09-06 23:50:42 +00:00
|
|
|
return await DavFile(f"{text_lister.remote_path}/{name}").string
|
2022-09-05 12:58:10 +00:00
|
|
|
|
|
|
|
|
2022-09-02 12:50:43 +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,
|
2022-09-02 12:50:43 +00:00
|
|
|
)
|
|
|
|
async def get_text(
|
2022-09-05 12:58:10 +00:00
|
|
|
text: str = Depends(get_text_content),
|
2022-09-02 12:50:43 +00:00
|
|
|
) -> str:
|
2022-09-05 12:58:10 +00:00
|
|
|
return markdown(text)
|
2022-09-02 12:20:29 +00:00
|
|
|
|
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
|