#!/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 parse_student_info(msg: mboxMessage) -> Tuple[str, str]: body = msg.get_payload() match = Parser.INFO_REGEX.search(body) if not match: raise ValueError("invalid mail has been given") 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") 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") 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_student_info(mail)) print(Parser.parse_submission(mail)) print(Parser.parse_points(mail))