#!/usr/bin/env python3 import datetime 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*") DATE_FORMAT = "%Y_%m%d_%H%M%S" OFFSET_FOR_CORRECTION = datetime.timedelta(days=8) @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 submitted_before_deadline(self, submission: str) -> bool: submitted = datetime.datetime.strptime( submission.split("/")[-1][-16:], Parser.DATE_FORMAT ) return self.deadline > submitted def __init__(self, path: str, deadline: str, correction: bool = False) -> None: self.box = mbox(path) self.deadline = datetime.datetime.strptime(deadline, Parser.DATE_FORMAT) if correction: # in case of correction pass the date of # submitting review to IS for automatic computation self.deadline = self.deadline.replace(hour=0, minute=0, second=0) self.deadline += Parser.OFFSET_FOR_CORRECTION 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))