|
| 1 | +import json |
| 2 | + |
| 3 | + |
| 4 | +class Backup: |
| 5 | + def __init__( |
| 6 | + self, |
| 7 | + backup_id: str, |
| 8 | + created: str, |
| 9 | + course: str, |
| 10 | + assignment: str, |
| 11 | + student_email: str, |
| 12 | + is_late: bool, |
| 13 | + submitted: bool, |
| 14 | + autograder_output_location: str = None, |
| 15 | + grading_location: str = None, |
| 16 | + file_contents_location: str = None, |
| 17 | + analytics_location: str = None, |
| 18 | + scoring_location: str = None, |
| 19 | + unlock_location: str = None, |
| 20 | + ): |
| 21 | + self.backup_id = backup_id |
| 22 | + self.created = created |
| 23 | + self.course = course |
| 24 | + self.assignment = assignment |
| 25 | + self.student_email = student_email |
| 26 | + |
| 27 | + self.is_late = is_late |
| 28 | + self.submitted = submitted |
| 29 | + |
| 30 | + self.autograder_output_location = autograder_output_location |
| 31 | + self.grading_location = grading_location |
| 32 | + self.file_contents_location = file_contents_location |
| 33 | + self.analytics_location = analytics_location |
| 34 | + self.scoring_location = scoring_location |
| 35 | + self.unlock_location = unlock_location |
| 36 | + |
| 37 | + |
| 38 | +class OkPyMessage: |
| 39 | + def __init__(self, contents): |
| 40 | + self.contents = contents |
| 41 | + |
| 42 | + |
| 43 | +class AutograderOutputMessage(OkPyMessage): |
| 44 | + @staticmethod |
| 45 | + def location(directory): |
| 46 | + return f"{directory}/autograder_output.txt" |
| 47 | + |
| 48 | + def write(self, directory): |
| 49 | + with open(AutograderOutputMessage.location(directory), "w") as f: |
| 50 | + f.write(self.contents) |
| 51 | + |
| 52 | + |
| 53 | +class GradingMessage(OkPyMessage): |
| 54 | + @staticmethod |
| 55 | + def location(directory): |
| 56 | + return f"{directory}/grading.json" |
| 57 | + |
| 58 | + def write(self, directory): |
| 59 | + with open(GradingMessage.location(directory), "w") as f: |
| 60 | + json.dump(self.contents, f, indent=2) |
| 61 | + |
| 62 | + |
| 63 | +class FileContentsMessage(OkPyMessage): |
| 64 | + @staticmethod |
| 65 | + def location(directory): |
| 66 | + # NOTE: a file content message's location is a DIRECTORY rather than a file |
| 67 | + # since there may be multiple source files in a student's backup |
| 68 | + return directory |
| 69 | + |
| 70 | + def write(self, directory): |
| 71 | + for src_file_name, src_file_contents in self.contents.items(): |
| 72 | + with open( |
| 73 | + f"{FileContentsMessage.location(directory)}/{src_file_name}", "w" |
| 74 | + ) as f: |
| 75 | + f.write(str(src_file_contents)) |
| 76 | + |
| 77 | + |
| 78 | +class AnalyticsMessage(OkPyMessage): |
| 79 | + @staticmethod |
| 80 | + def location(directory): |
| 81 | + return f"{directory}/analytics.json" |
| 82 | + |
| 83 | + def write(self, directory): |
| 84 | + with open(AnalyticsMessage.location(directory), "w") as f: |
| 85 | + json.dump(self.contents, f, indent=2) |
| 86 | + |
| 87 | + |
| 88 | +class ScoringMessage(OkPyMessage): |
| 89 | + @staticmethod |
| 90 | + def location(directory): |
| 91 | + return f"{directory}/scoring.json" |
| 92 | + |
| 93 | + def write(self, directory): |
| 94 | + with open(ScoringMessage.location(directory), "w") as f: |
| 95 | + json.dump(self.contents, f, indent=2) |
| 96 | + |
| 97 | + |
| 98 | +class UnlockMessage(OkPyMessage): |
| 99 | + @staticmethod |
| 100 | + def location(directory): |
| 101 | + return f"{directory}/unlock.json" |
| 102 | + |
| 103 | + def write(self, directory): |
| 104 | + with open(GradingMessage.location(directory), "w") as f: |
| 105 | + json.dump(self.contents, f, indent=2) |
| 106 | + |
| 107 | + |
| 108 | +MESSAGE_KIND_TO_CLASS = { |
| 109 | + "autograder_output": AutograderOutputMessage, |
| 110 | + "grading": GradingMessage, |
| 111 | + "file_contents": FileContentsMessage, |
| 112 | + "analytics": AnalyticsMessage, |
| 113 | + "scoring": ScoringMessage, |
| 114 | + "unlock": UnlockMessage, |
| 115 | +} |
0 commit comments