106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
import os
|
||
import re
|
||
import copy
|
||
from typing import Dict, List, Tuple
|
||
from pprint import pprint
|
||
|
||
current_directory = os.path.dirname(os.path.abspath(__file__))
|
||
|
||
|
||
def get_current_path():
|
||
return current_directory
|
||
|
||
|
||
def get_obsidian_examples_file(example_file_name: str):
|
||
return os.path.join(get_current_path(), f"examples/{example_file_name}")
|
||
|
||
|
||
def read_example_file(example_file_name: str):
|
||
path_to_example: str = get_obsidian_examples_file(example_file_name)
|
||
with open(path_to_example, "r") as f:
|
||
content = f.read()
|
||
return content
|
||
|
||
|
||
def parse_training_exercises(exercise_line: str) -> List[str]:
|
||
stripped: List[str] = [entry.strip() for entry in exercise_line.split("|")][1:-1]
|
||
for entry in stripped:
|
||
if entry in ["Упражнение", "Вес", "Подходы"]:
|
||
raise ValueError
|
||
if stripped:
|
||
if "---" in stripped[0]:
|
||
raise ValueError
|
||
if len(stripped) != 3:
|
||
raise ValueError
|
||
return {
|
||
"name": stripped[0],
|
||
"weight": stripped[1],
|
||
"reps": stripped[2],
|
||
}
|
||
|
||
|
||
def parse_training_header(training_data_line: str) -> Tuple[bool, str, str, str]:
|
||
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()
|
||
trainer = match.group("trainer").strip()
|
||
year_count = match.group("year_counter").strip()
|
||
|
||
print(f"Date: {date}")
|
||
print(f"Trainer: {trainer}")
|
||
print(f"Year counter: {year_count}")
|
||
return True, date, trainer, year_count
|
||
return False, None, None, None
|
||
|
||
|
||
def filter_training_data(training_data: str):
|
||
cleaned_text = re.sub(r"^\s*?\n", "", training_data, flags=re.MULTILINE)
|
||
return cleaned_text
|
||
|
||
|
||
def parse_training_data():
|
||
training_data: str = filter_training_data(read_example_file("notes.txt"))
|
||
training_template: Dict = {
|
||
"date": None,
|
||
"trainer": None,
|
||
"year_count": None,
|
||
"exercises": [],
|
||
}
|
||
parsed_trainings: List[List[str]] = []
|
||
current_training: Dict = {}
|
||
lines = training_data.splitlines()
|
||
for index, line in enumerate(lines):
|
||
if index == len(lines) - 1:
|
||
current_training["exercises"] = current_training["exercises"][1:]
|
||
parsed_trainings.append(current_training)
|
||
break
|
||
header_parsed, date, trainer, year_count = parse_training_header(line)
|
||
if header_parsed:
|
||
if not current_training:
|
||
pass
|
||
else:
|
||
current_training["exercises"] = current_training["exercises"][1:]
|
||
parsed_trainings.append(current_training)
|
||
current_training = copy.deepcopy(training_template)
|
||
current_training["date"] = date
|
||
current_training["trainer"] = trainer
|
||
current_training["year_count"] = year_count
|
||
try:
|
||
# exr, weight, reps = parse_training_exercises(line)
|
||
# current_training["exercises"].append(
|
||
# f"Name: {exr}, Weight: {weight}, Reps: {reps}"
|
||
# )
|
||
|
||
# current_training["exercises"].append(
|
||
# {"name": exr, "weight": weight, "reps": reps}
|
||
# )
|
||
|
||
exr = parse_training_exercises(line)
|
||
current_training["exercises"].append(exr)
|
||
except ValueError:
|
||
pass
|
||
return parsed_trainings
|
||
|
||
|
||
pprint(parse_training_data()[1:])
|