Factor out matching
This commit is contained in:
parent
32650ae05a
commit
4e80820ede
1 changed files with 13 additions and 16 deletions
|
@ -12,30 +12,27 @@ class Parser:
|
||||||
POINTS_REGEX = re.compile(r"\*\scelkový počet bodů\s+((\d|\.)*)\s*")
|
POINTS_REGEX = re.compile(r"\*\scelkový počet bodů\s+((\d|\.)*)\s*")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_student_info(msg: mboxMessage) -> Tuple[str, str]:
|
def get_match_from_mail(regex: re.Pattern, mail: mboxMessage) -> re.Match:
|
||||||
body = msg.get_payload()
|
body = mail.get_payload()
|
||||||
match = Parser.INFO_REGEX.search(body)
|
match = regex.search(body)
|
||||||
if not match:
|
if not match:
|
||||||
raise ValueError("invalid mail has been given")
|
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)
|
return match.group(1), match.group(2)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_submission(msg: mboxMessage) -> str:
|
def parse_submission(mail: mboxMessage) -> str:
|
||||||
body = msg.get_payload()
|
match = Parser.get_match_from_mail(Parser.SUBMISSION_REGEX, mail)
|
||||||
match = Parser.SUBMISSION_REGEX.search(body)
|
|
||||||
if not match:
|
|
||||||
raise ValueError("invalid mail has been given")
|
|
||||||
|
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_points(msg: mboxMessage) -> float:
|
def parse_points(mail: mboxMessage) -> float:
|
||||||
body = msg.get_payload()
|
match = Parser.get_match_from_mail(Parser.POINTS_REGEX, mail)
|
||||||
match = Parser.POINTS_REGEX.search(body)
|
|
||||||
if not match:
|
|
||||||
raise ValueError("invalid mail has been given")
|
|
||||||
|
|
||||||
return float(match.group(1))
|
return float(match.group(1))
|
||||||
|
|
||||||
def __init__(self, path: str) -> None:
|
def __init__(self, path: str) -> None:
|
||||||
|
@ -46,6 +43,6 @@ class Parser:
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mails = mbox("~/xyz.mbox")
|
mails = mbox("~/xyz.mbox")
|
||||||
_, mail = mails.popitem()
|
_, mail = mails.popitem()
|
||||||
print(Parser.parse_student_info(mail))
|
print(Parser.parse_info(mail))
|
||||||
print(Parser.parse_submission(mail))
|
print(Parser.parse_submission(mail))
|
||||||
print(Parser.parse_points(mail))
|
print(Parser.parse_points(mail))
|
||||||
|
|
Reference in a new issue