Add trainings endpoints
This commit is contained in:
parent
74a3aae531
commit
3cadfb1d09
4 changed files with 34 additions and 14 deletions
|
|
@ -1,7 +1,8 @@
|
|||
from fastapi import APIRouter
|
||||
from app.api.v1.endpoints import parser, health
|
||||
from app.api.v1.endpoints import parser, health, trainings
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
api_router.include_router(health.router, prefix="/health", tags=["health"])
|
||||
api_router.include_router(parser.router, prefix="/parser", tags=["parser"])
|
||||
api_router.include_router(parser.router, prefix="/parser", tags=["parser"])
|
||||
api_router.include_router(trainings.router, prefix="/trainings", tags=["trainings"])
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ router = APIRouter()
|
|||
parser_service = ParserService()
|
||||
|
||||
|
||||
@router.post("/parse", response_model=List[ParseResponse])
|
||||
@router.post("/parse/", response_model=List[ParseResponse])
|
||||
async def parse_training_data(request: ParseRequest):
|
||||
try:
|
||||
result = await parser_service.parse_data(request)
|
||||
|
|
@ -16,7 +16,7 @@ async def parse_training_data(request: ParseRequest):
|
|||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/supported-formats")
|
||||
@router.get("/supported-formats/")
|
||||
async def get_supported_formats():
|
||||
return {
|
||||
"formats": [
|
||||
|
|
@ -24,4 +24,4 @@ async def get_supported_formats():
|
|||
{"name": "obsidian", "description": "Obsidian Markdown format"},
|
||||
{"name": "text", "description": "Plain text format"}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
app/api/v1/endpoints/trainings.py
Normal file
11
app/api/v1/endpoints/trainings.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from fastapi import APIRouter
|
||||
|
||||
from app.core.parsers.obsidian import parse_training_data
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/obsidian/")
|
||||
async def obsidian_trainings_list():
|
||||
return {
|
||||
"data": parse_training_data()
|
||||
}
|
||||
|
|
@ -8,18 +8,25 @@ from app.core.parsers.base import BaseNotesParser
|
|||
|
||||
class ObsidianNotesParser(BaseNotesParser):
|
||||
"""Parser for Obsidian Notes format training data."""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("obsidian.md", obsidian_unique_exercies_mapping)
|
||||
|
||||
super().__init__(
|
||||
data_file_name="obsidian.md",
|
||||
exercise_mapper=obsidian_unique_exercies_mapping,
|
||||
)
|
||||
|
||||
def filter_training_data(self, training_data: str) -> str:
|
||||
"""Filter Obsidian-specific training data format."""
|
||||
cleaned_text = re.sub(r"^\s*?\n", "", training_data, flags=re.MULTILINE)
|
||||
return cleaned_text
|
||||
|
||||
def parse_training_header(self, training_data_line: str) -> Tuple[bool, str, str, str]:
|
||||
|
||||
def parse_training_header(
|
||||
self, training_data_line: str
|
||||
) -> Tuple[bool, str, str, str]:
|
||||
"""Parse Obsidian Notes training header format."""
|
||||
pattern: str = r"#\s(?P<date>\d+.\d+.\d+)\s\((?P<trainer>.+)-(?P<year_counter>.+)\)"
|
||||
pattern: str = (
|
||||
r"#\s(?P<date>\d+.\d+.\d+)\s\((?P<trainer>.+)-(?P<year_counter>.+)\)"
|
||||
)
|
||||
match = re.search(pattern, training_data_line)
|
||||
if match:
|
||||
date = match.group("date").strip()
|
||||
|
|
@ -27,13 +34,13 @@ class ObsidianNotesParser(BaseNotesParser):
|
|||
year_count = match.group("year_counter").strip()
|
||||
return True, date, trainer, year_count
|
||||
return False, "", "", ""
|
||||
|
||||
|
||||
def parse(self, data: str) -> List[Training]:
|
||||
"""Parse Obsidian training data from string input."""
|
||||
# Override the data file reading with direct string input
|
||||
original_method = self.read_data_file
|
||||
self.read_data_file = lambda _: data
|
||||
|
||||
|
||||
try:
|
||||
trainings = self.parse_and_map_training_data()
|
||||
return trainings
|
||||
|
|
@ -51,4 +58,5 @@ def parse_training_data() -> List[Training]:
|
|||
def remap_unique_exercises(obsidian_trainings: List[Training]) -> List[Training]:
|
||||
"""Remap exercise names using Obsidian-specific mapping (deprecated - use parser.parse_and_map_training_data())."""
|
||||
parser = ObsidianNotesParser()
|
||||
return parser.apply_exercise_mapping(obsidian_trainings)
|
||||
return parser.apply_exercise_mapping(obsidian_trainings)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue