#!/usr/bin/env python3 from mailbox import mbox, mboxMessage import re from typing import Tuple class Parser: INFO_REGEX = re.compile(r"(\d{6}) \| (x\S*)\s*") SUBMISSION_REGEX = re.compile(r"adresář:\s+\S*\/(\S*)\s*") POINTS_REGEX = re.compile(r"\*\scelkový počet bodů\s+((\d|\.)*)\s*") @staticmethod 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(mail: mboxMessage) -> str: match = Parser.get_match_from_mail(Parser.SUBMISSION_REGEX, mail) return match.group(1) @staticmethod 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: self.path = path self.box = mbox(path) if __name__ == "__main__": mails = mbox("~/xyz.mbox") _, mail = mails.popitem() print(Parser.parse_info(mail)) print(Parser.parse_submission(mail)) print(Parser.parse_points(mail))