diff --git a/kontr_emails.py b/kontr_emails.py index 5b67e17..c8ede89 100644 --- a/kontr_emails.py +++ b/kontr_emails.py @@ -12,30 +12,27 @@ class Parser: POINTS_REGEX = re.compile(r"\*\scelkový počet bodů\s+((\d|\.)*)\s*") @staticmethod - def parse_student_info(msg: mboxMessage) -> Tuple[str, str]: - body = msg.get_payload() - match = Parser.INFO_REGEX.search(body) + def get_match_from_mail(regex: re.Pattern, mail: mboxMessage) -> re.Match: + body = mail.get_payload() + match = regex.search(body) if not match: raise ValueError("invalid mail has been given") + return match + + @staticmethod + def parse_info(mail: mboxMessage) -> Tuple[str, str]: + match = Parser.get_match_from_mail(Parser.INFO_REGEX, mail) return match.group(1), match.group(2) @staticmethod - def parse_submission(msg: mboxMessage) -> str: - body = msg.get_payload() - match = Parser.SUBMISSION_REGEX.search(body) - if not match: - raise ValueError("invalid mail has been given") - + def parse_submission(mail: mboxMessage) -> str: + match = Parser.get_match_from_mail(Parser.SUBMISSION_REGEX, mail) return match.group(1) @staticmethod - def parse_points(msg: mboxMessage) -> float: - body = msg.get_payload() - match = Parser.POINTS_REGEX.search(body) - if not match: - raise ValueError("invalid mail has been given") - + def parse_points(mail: mboxMessage) -> float: + match = Parser.get_match_from_mail(Parser.POINTS_REGEX, mail) return float(match.group(1)) def __init__(self, path: str) -> None: @@ -46,6 +43,6 @@ class Parser: if __name__ == "__main__": mails = mbox("~/xyz.mbox") _, mail = mails.popitem() - print(Parser.parse_student_info(mail)) + print(Parser.parse_info(mail)) print(Parser.parse_submission(mail)) print(Parser.parse_points(mail))