f1tness_parser/apple/notes_parser.py

71 lines
No EOL
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from typing import List, Tuple
from datetime import datetime
from obsidian.py_models import Training
from apple.mapper import unique_apple_exercises_mapper
from parsers.base_parser import BaseNotesParser
class AppleNotesParser(BaseNotesParser):
"""Parser for Apple Notes format training data."""
def __init__(self):
super().__init__("apple.md", unique_apple_exercises_mapper)
def filter_training_data(self, training_data: str) -> str:
"""Filter Apple-specific training data format."""
cleaned_text = re.sub(
r"^\|(\s+|-*|\s-*\s)\|(\s+|-*|\s-*\s)\|(\s+|-*|\s-*\s)\|$",
"",
training_data,
flags=re.MULTILINE,
)
cleaned_text = re.sub(r"^\n", "", cleaned_text, flags=re.MULTILINE)
lines = cleaned_text.splitlines()
redundant_lines = [
"| | | |",
"|---|---|---|",
"|**Упражнение**|**Вес**|**Подходы**|",
]
filtered_lines = [line for line in lines if line not in redundant_lines]
return "\n".join(filtered_lines)
def parse_training_header(self, training_data_line: str) -> Tuple[bool, str, str, str]:
"""Parse Apple Notes training header format."""
pattern: str = (
r"^\*\*(?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()
trainer = match.group("trainer").strip()
if match.group("year_counter"):
year_count = match.group("year_counter").strip()
else:
year_count = "0"
return True, date, trainer, year_count
return False, "", "", ""
def create_training_from_date(self, date_str: str) -> Training:
"""Create Training object from date string with fallback parsing."""
try:
return Training(
date=datetime.strptime(date_str, "%d.%m.%Y").date(), exercises=[]
)
except ValueError:
return Training(
date=datetime.strptime(date_str, "%d.%m.%y").date(), exercises=[]
)
def parse_training_data() -> List[Training]:
"""Parse Apple Notes training data."""
parser = AppleNotesParser()
return parser.parse_training_data()
def remap_unique_exercises(apple_trainings: List[Training]) -> List[Training]:
"""Remap exercise names using Apple-specific mapping (deprecated - use parser.parse_and_map_training_data())."""
parser = AppleNotesParser()
return parser.apply_exercise_mapping(apple_trainings)