import re from typing import Iterator from fastapi import APIRouter, Depends from markdown import markdown from ..config import SETTINGS from ..dav_file import DavFile from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader router = APIRouter(prefix="/text", tags=["text"]) _lister = FileNameLister( remote_path="text", re=re.compile( r"\.(txt|md)$", flags=re.IGNORECASE, ), ) _finder = FilePrefixFinder(_lister) _loader = FilePrefixLoader(_finder) async def get_ticker_lines() -> Iterator[str]: ticker = await DavFile("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]: return ( line for line in ticker_lines if not line.startswith("#") ) @router.get("/get/ticker") async def get_ticker_content( ticker_content_lines: Iterator[str] = Depends(get_ticker_content_lines), ) -> str: return markdown( SETTINGS.ticker_separator.join(ticker_content_lines) ) @router.get("/list", response_model=list[str]) async def list_texts( text_file_names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(text_file_names) @router.get("/find/{prefix}", response_model=list[str]) async def find_texts( file_names: Iterator[str] = Depends(_finder), ) -> list[str]: return list(file_names) @router.get( "/get/{prefix}", response_model=str, responses=_loader.responses, ) async def get_text( dav_file: DavFile = Depends(_loader), ) -> str: text = await dav_file.string return markdown( text )