From 32650ae05a24dbca076277236da1d47433fc2ea1 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 30 Nov 2019 12:19:33 +0100 Subject: [PATCH] Add basic parser for mbox mails --- kontr_emails.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 kontr_emails.py diff --git a/kontr_emails.py b/kontr_emails.py new file mode 100644 index 0000000..5b67e17 --- /dev/null +++ b/kontr_emails.py @@ -0,0 +1,51 @@ +#!/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))