diff --git a/kontr_emails.py b/kontr_emails.py index 4a477d7..bd1a3fa 100644 --- a/kontr_emails.py +++ b/kontr_emails.py @@ -2,9 +2,15 @@ import datetime +import functools +import itertools from mailbox import mbox, mboxMessage +import math import re -from typing import Tuple +from typing import Dict, List, Tuple + + +from submission import Submission, print_submissions class Parser: @@ -38,25 +44,86 @@ class Parser: 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) + self.correction = correction 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 + def __get_submissions(self, hw_tag: str) -> Dict[str, List[Submission]]: + submissions = [] + + for mail in self.box.values(): + uco, login = Parser.parse_info(mail) + path = Parser.parse_submission(mail) + points = Parser.parse_points(mail) + + submissions.append( + Submission(uco, login, path, points, hw_tag, self.correction) + ) + submissions[-1].set_late_tag(self.deadline) + + submissions.sort(key=lambda e: (e.login, e.submitted_at)) + return dict( + map( + lambda val: (val[0], list(val[1])), + itertools.groupby(submissions, key=lambda s: s.login), + ) + ) + + def __filter(self, all_submissions: Dict[str, List[Submission]]) -> None: + for _, submissions in all_submissions.items(): + length = len(submissions) + i = -1 + while not submissions[i].submitted_before_deadline and i >= -length: + i -= 1 + + if i >= -length: + submissions[i].flag = "REVIEW" + + def __correct_errors(self, all_submissions: Dict[str, List[Submission]]) -> None: + response = input("Do you wish to fix errors? ").strip() + if not response or response == "n": + return True + + print("Choose indices of submissions that are to be reviewed") + for login, submissions in all_submissions.items(): + response = input(f"==> Choose submission to review for {login}: ").strip() + if not response or response == "n": + continue + + j = int(response) + for i, submission in enumerate(submissions): + if submission.flag == "REVIEW": + submission.flag = None + if i == j: + submission.flag = "REVIEW" + + return False + + def parse(self, hw_tag: str) -> List[Submission]: + def __reducer(lst, submission): + if submission.flag == "REVIEW": + lst.append(submission) + return lst + + submissions = self.__get_submissions(hw_tag) + + self.__filter(submissions) + print_submissions(submissions) + + while not self.__correct_errors(submissions): + print_submissions(submissions) + + result = [] + for something in submissions.values(): + functools.reduce( + __reducer, something, result, + ) + return result -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)) diff --git a/reviews.py b/pushee.py similarity index 58% rename from reviews.py rename to pushee.py index 17655b9..872c813 100644 --- a/reviews.py +++ b/pushee.py @@ -5,7 +5,7 @@ import sys from commands import MergeRequests, UpdateAssignees, Comments, Merge, Test -from constants import SUBMISSIONS +from kontr_emails import Parser COMMANDS = { @@ -18,20 +18,27 @@ COMMANDS = { def print_usage(): - print("Usage:") + print(f"{sys.argv[0]} ") + print() + print("Commands:") print("\tmrs\t\t\tFetch files and create merge requests for them") print("\tupdate-assignees\tUpdate assignees on MRs") print("\tcomments\t\tFetch all comments on MRs") print("\tmerge\t\t\tMerge all MRs") print("\ttest\t\t\tDebugging function") + print("Format of date: %Y_%m%d_%H%M%S") def main(): - if len(sys.argv) < 2 or sys.argv[1] not in COMMANDS: + if len(sys.argv) != 6 or sys.argv[5] not in COMMANDS: print_usage() exit(2) - COMMANDS[sys.argv[1]](SUBMISSIONS)() + _, path, hw, correction, deadline, command = sys.argv + correction = correction == "y" + + submissions = Parser(path, deadline, correction).parse(hw) + COMMANDS[command](submissions)() if __name__ == "__main__":