From 7b7dd15a620707aac5646dcf4d183af8fbff83a9 Mon Sep 17 00:00:00 2001 From: Viv Date: Sat, 10 Feb 2024 18:10:45 -0500 Subject: [PATCH] `parsers.py`: Improve error handling for BPM/OFFSET parsing --- src/tja2fumen/parsers.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/tja2fumen/parsers.py b/src/tja2fumen/parsers.py index 10626c9..8bdaeef 100644 --- a/src/tja2fumen/parsers.py +++ b/src/tja2fumen/parsers.py @@ -69,14 +69,21 @@ def split_tja_lines_into_courses(lines: List[str]) -> TJASong: if line.split("//")[0].strip()] # Initialize song with BPM and OFFSET global metadata - bpm = float([line.split(":")[1] for line in lines - if line.startswith("BPM")][0]) - offset = float([line.split(":")[1] for line in lines - if line.startswith("OFFSET")][0]) + tja_metadata = {} + for required_metadata in ["BPM", "OFFSET"]: + for line in lines: + if line.startswith(required_metadata): + tja_metadata[required_metadata] = float(line.split(":")[1]) + break + else: + raise ValueError(f"TJA does not contain required " + f"'{required_metadata}' metadata.") parsed_tja = TJASong( - bpm=bpm, - offset=offset, - courses={course: TJACourse(bpm=bpm, offset=offset, course=course) + bpm=tja_metadata['BPM'], + offset=tja_metadata['OFFSET'], + courses={course: TJACourse(bpm=tja_metadata['BPM'], + offset=tja_metadata['OFFSET'], + course=course) for course in TJA_COURSE_NAMES} )