|
| 1 | +""" |
| 2 | +Utility classes for analysis persistence. |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from enum import Enum, auto, unique |
| 8 | +from typing import List |
| 9 | + |
| 10 | +Comments = List[Enum] |
| 11 | +PylintComments = List[str] |
| 12 | + |
| 13 | + |
| 14 | +@unique |
| 15 | +class Status(Enum): |
| 16 | + """ |
| 17 | + Status of the exercise under analysis. |
| 18 | + """ |
| 19 | + |
| 20 | + APPROVE_AS_OPTIMAL = auto() |
| 21 | + APPROVE_WITH_COMMENT = auto() |
| 22 | + DISAPPROVE_WITH_COMMENT = auto() |
| 23 | + REFER_TO_MENTOR = auto() |
| 24 | + |
| 25 | + def __str__(self): |
| 26 | + return self.name.lower() |
| 27 | + |
| 28 | + def __repr__(self): |
| 29 | + return f"{self.__class__.__name__}.{self.name}" |
| 30 | + |
| 31 | + |
| 32 | +class AnalysisEncoder(json.JSONEncoder): |
| 33 | + """ |
| 34 | + Simple encoder that will punt an Enum out as its string. |
| 35 | + """ |
| 36 | + |
| 37 | + def default(self, obj): |
| 38 | + if isinstance(obj, Enum): |
| 39 | + return str(obj) |
| 40 | + return json.JSONEncoder.default(self, obj) |
| 41 | + |
| 42 | + |
| 43 | +class Analysis(dict): |
| 44 | + """ |
| 45 | + Represents the current state of the analysis of an exercise. |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self, status, comment, pylint_comment, approve=False): |
| 49 | + super(Analysis, self).__init__( |
| 50 | + status=status, comment=comment, pylint_comment=pylint_comment |
| 51 | + ) |
| 52 | + self._approved = approve |
| 53 | + |
| 54 | + @property |
| 55 | + def status(self) -> Status: |
| 56 | + """ |
| 57 | + The current status of the analysis. |
| 58 | + """ |
| 59 | + return self["status"] |
| 60 | + |
| 61 | + @property |
| 62 | + def comment(self) -> Comments: |
| 63 | + """ |
| 64 | + The list of comments for the analysis. |
| 65 | + """ |
| 66 | + return self["comment"] |
| 67 | + |
| 68 | + @property |
| 69 | + def pylint_comment(self) -> PylintComments: |
| 70 | + """ |
| 71 | + The list of pylint comments for the analysis. |
| 72 | + """ |
| 73 | + return self["pylint_comment"] |
| 74 | + |
| 75 | + @property |
| 76 | + def approved(self): |
| 77 | + """ |
| 78 | + Is this analysis _considered_ approve-able? |
| 79 | + Note that this does not imply an approved status, but that the exercise |
| 80 | + has hit sufficient points that a live Mentor would likely approve it. |
| 81 | + """ |
| 82 | + return self._approved |
| 83 | + |
| 84 | + @classmethod |
| 85 | + def approve_as_optimal(cls, comment=None, pylint_comment=None): |
| 86 | + """ |
| 87 | + Create an Anaylsis that is approved as optimal. |
| 88 | + """ |
| 89 | + return cls( |
| 90 | + Status.APPROVE_AS_OPTIMAL, comment or [], pylint_comment or [], approve=True |
| 91 | + ) |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def approve_with_comment(cls, comment, pylint_comment=None): |
| 95 | + """ |
| 96 | + Create an Analysis that is approved with comment. |
| 97 | + """ |
| 98 | + return cls( |
| 99 | + Status.APPROVE_WITH_COMMENT, comment, pylint_comment or [], approve=True |
| 100 | + ) |
| 101 | + |
| 102 | + @classmethod |
| 103 | + def disapprove_with_comment(cls, comment, pylint_comment=None): |
| 104 | + """ |
| 105 | + Create an Analysis that is disapproved with comment. |
| 106 | + """ |
| 107 | + return cls(Status.DISAPPROVE_WITH_COMMENT, comment, pylint_comment or []) |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def refer_to_mentor(cls, comment, pylint_comment=None, approve=False): |
| 111 | + """ |
| 112 | + Create an Analysis that should be referred to a mentor. |
| 113 | + """ |
| 114 | + return cls( |
| 115 | + Status.REFER_TO_MENTOR, comment, pylint_comment or [], approve=approve |
| 116 | + ) |
| 117 | + |
| 118 | + def dump(self, path: Path): |
| 119 | + """ |
| 120 | + Dump's the current state to analysis.json. |
| 121 | + As a convenience returns the Anaylsis itself. |
| 122 | + """ |
| 123 | + with open(path, "w") as dst: |
| 124 | + json.dump(self, dst, indent=4, cls=AnalysisEncoder) |
| 125 | + return self |
0 commit comments