From df5a7db84432f4f60698b2ddb97a9f8955dd97fb Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Sat, 21 Dec 2024 19:05:04 +0530 Subject: Refactor for modularity, package --- parse_srt.py | 58 ---------------------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 parse_srt.py (limited to 'parse_srt.py') diff --git a/parse_srt.py b/parse_srt.py deleted file mode 100644 index 2e710ea..0000000 --- a/parse_srt.py +++ /dev/null @@ -1,58 +0,0 @@ -from __future__ import annotations -import dataclasses -import re - - -@dataclasses.dataclass -class Event: - start: str - end: str - content: str - - -class SRT: - def __init__(self): - self.events: list[Event] = [] - - @staticmethod - def from_str(text: str) -> SRT: - TIMESTAMP_CAPTURE = r"(\d\d:\d\d:\d\d,\d\d\d)" - TIMING_REGEX = rf"{TIMESTAMP_CAPTURE} --> {TIMESTAMP_CAPTURE}" - - srt = SRT() - counter = 1 - events = [event for event in text.split("\n\n") if event.strip()] - for event_str in events: - lines = event_str.split("\n") - counter_str, timing_str, content_lines = lines[0], lines[1], lines[2:] - - if int(counter_str) != counter: - raise ParseError( - f"Invalid counter '{counter_str}'; expected {counter}", event_str - ) - counter += 1 - - match = re.fullmatch(TIMING_REGEX, timing_str) - if match is None: - raise ParseError(f"Invalid timing info '{timing_str}'", event_str) - - content = "\n".join(content_lines + [""]) - - srt.events.append(Event(match[1], match[2], content)) - - return srt - - def __str__(self): - result = "" - for counter, event in enumerate(self.events, 1): - result += f"{counter}\n" - result += f"{event.start} --> {event.end}\n" - result += f"{event.content}\n" - return result - - -class ParseError(Exception): - def __init__(self, reason: str, event_str: str): - super().__init__(f"{reason}\nwhile parsing event:\n{event_str}") - self.reason = reason - self.event_str = event_str -- cgit v1.2.3-57-g22cb