-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodona_command.py
333 lines (262 loc) · 11.4 KB
/
dodona_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""report judge's results to Dodona using Dodona commands (partial JSON output)"""
import json
import sys
from abc import ABC
from enum import Enum
from types import SimpleNamespace, TracebackType
from typing import Union, Dict, Type, Optional
class ErrorType(str, Enum):
"""Dodona error type"""
INTERNAL_ERROR = "internal error"
COMPILATION_ERROR = "compilation error"
MEMORY_LIMIT_EXCEEDED = "memory limit exceeded"
TIME_LIMIT_EXCEEDED = "time limit exceeded"
OUTPUT_LIMIT_EXCEEDED = "output limit exceeded"
RUNTIME_ERROR = "runtime error"
WRONG = "wrong"
WRONG_ANSWER = "wrong answer"
CORRECT = "correct"
CORRECT_ANSWER = "correct answer"
def __str__(self):
return str(self)
class MessagePermission(str, Enum):
"""Dodona permission for a message"""
STUDENT = "student"
STAFF = "staff"
ZEUS = "zeus"
def __str__(self):
return str(self)
class MessageFormat(str, Enum):
"""Dodona format for a message"""
PLAIN = "plain"
TEXT = "text"
HTML = "html"
MARKDOWN = "markdown"
CALLOUT = "callout"
CALLOUT_INFO = "callout-info"
CALLOUT_WARNING = "callout-warning"
CALLOUT_DANGER = "callout-danger"
CODE = "code"
SQL = "sql"
def __str__(self):
return str(self)
class AnnotationSeverity(str, Enum):
"""Dodona severity of an annotation"""
ERROR = "error"
WARNING = "warning"
INFO = "info"
def __str__(self):
return str(self)
class DodonaException(Exception):
"""exception that will automatically create a message and set the correct status when thrown
When thrown inside a Dodona 'with' block, an error message will be created on the current
Dodona object (eg. Test, Context ...). Blocks that extend the DodonaCommandWithAccepted class
will have their accepted field set to True (if CORRECT or CORRECT_ANSWER) and to False otherwise.
If the block also extends DodonaCommandWithStatus, its status is updated with this exception's
status. The outer Judgement block will silently catch the exception and the process will exit
with a 0 exitcode.
"""
def __init__(
self,
status: Dict[str, str],
*args,
**kwargs,
):
super().__init__()
self.status = status
self.message = Message(*args, **kwargs) if len(args) > 0 or len(kwargs) > 0 else None
class DodonaCommand(ABC):
"""abstract class, parent of all Dodona commands
This class provides all shared functionality for the Dodona commands. These commands
should be used in a Python 'with' block.
Example:
>>> with Judgement() as judgement:
... with Tab():
... pass
A JSON message will be printed to stdout when entering the 'with' block. The contents of
the message are the parameters passed to the constructor to the class.
When exiting the 'with' block, a close JSON message will be printed to stdout. The contents
of that message are set dynamically on the object that was returned when entering.
Example:
>>> with Tab(
... title="example tab",
... ) as tab:
... tab.badgeCount = 43
When entering the 'with' block, prints:
{
"command": "start-tab",
"title": "example tab"
}
When exiting the 'with' block, prints:
{
"command": "close-tab",
"badgeCount": 43
}
"""
def __init__(self, **kwargs):
self.start_args = SimpleNamespace(**kwargs)
self.close_args = SimpleNamespace()
def name(self) -> str:
"""name used in start and close messages, defaults to the lowercase version of the classname"""
return self.__class__.__name__.lower()
def start_msg(self) -> dict:
"""start message that is printed as JSON to stdout when entering the 'with' block"""
return {"command": f"start-{self.name()}", **self.start_args.__dict__}
def close_msg(self) -> dict:
"""close message that is printed as JSON to stdout when exiting the 'with' block"""
return {"command": f"close-{self.name()}", **self.close_args.__dict__}
@staticmethod
def __print_command(result: Union[None, dict]) -> None:
"""print the provided to stdout as JSON
:param result: dict that will be JSON encoded and printed to stdout
"""
if result is None:
return
json.dump(result, sys.stdout, indent=1, sort_keys=True)
sys.stdout.write("\n") # Next JSON fragment should be on new line
def __enter__(self) -> SimpleNamespace:
"""print the start message when entering the 'with' block"""
self.__print_command(self.start_msg())
return self.close_args
# pylint: disable=no-self-use
def handle_dodona_exception(self, exception: DodonaException) -> bool:
"""handle a DodonaException
This function returns a boolean that is True if the exception should
not get propagated to parent codeblocks. This should only be True
for the most outer block (Judgement), so that all levels of Dodona
objects can update their status and success parameters.
This function can be overwritten by child classes, these overwrites
should still call this function.
This function prints a Dodona message and removes the message from
the exception, so it is not also printed by the parent 'with' blocks.
:param exception: exception thrown in the enclosed 'with' block
:return: if True, the exception is not propagated
"""
# Add an error message
if exception.message is not None:
with exception.message:
pass
exception.message = None
return False
def __exit__(
self,
exc_type: Type[BaseException],
exc_val: BaseException,
exc_tb: TracebackType,
) -> bool:
"""print the close message when exiting the 'with' block & handle enclosed exceptions
If a DodonaException was thrown in the enclosed 'with' block, the 'handle_dodona_exception'
function is called. This function can be overwritten by child classes. If 'handle_dodona_exception'
returns True, this function also returns True and the error is not propagated.
:return: if True, the exception is not propagated
"""
if isinstance(exc_val, DodonaException):
handled = self.handle_dodona_exception(exc_val)
else:
handled = False
self.__print_command(self.close_msg())
return handled
class DodonaCommandWithAccepted(DodonaCommand):
"""abstract class, parent of all Dodona commands that have an accepted field"""
def handle_dodona_exception(self, exception: DodonaException) -> bool:
"""update the accepted parameter based on the exception status"""
if not hasattr(self.close_args, "accepted"):
accepted = exception.status["enum"] == ErrorType.CORRECT or exception.status[
"enum"] == ErrorType.CORRECT_ANSWER
self.close_args.accepted = accepted
return super().handle_dodona_exception(exception)
class DodonaCommandWithStatus(DodonaCommandWithAccepted):
"""abstract class, parent of all Dodona commands that have a status field"""
def handle_dodona_exception(self, exception: DodonaException) -> bool:
"""update the status of the object"""
self.close_args.status = exception.status
return super().handle_dodona_exception(exception)
class Judgement(DodonaCommandWithStatus):
"""Dodona Judgement"""
def handle_dodona_exception(self, exception: DodonaException) -> bool:
"""return True to prevent the exception from crashing Python and causing a non-zero exit code"""
super().handle_dodona_exception(exception)
return True
class Tab(DodonaCommand):
"""Dodona Tab"""
def __init__(self, title: str, **kwargs):
super().__init__(title=title, **kwargs)
class Context(DodonaCommandWithAccepted):
"""Dodona Context"""
class TestCase(DodonaCommandWithAccepted):
"""Dodona TestCase"""
def __init__(self, *args, **kwargs):
"""create TestCase
If a single positional argument is passed, this is assumed to be the message.
Example:
>>> with TestCase("This is the message"):
... pass
>>> with TestCase({
... "format": MessageFormat.SQL,
... "description": "This is the message"
... }):
... pass
If keyword arguments are passed, these are assumed to be the message's content.
Example:
>>> with TestCase(
... format=MessageFormat.SQL,
... description="This is the message"
... ):
... pass
"""
if len(args) == 1:
super().__init__(description=args[0])
else:
super().__init__(description=kwargs)
class Test(DodonaCommandWithStatus):
"""Dodona Test"""
def __init__(self, description: str, expected: str, **kwargs):
super().__init__(description=description, expected=expected, **kwargs)
class Message(DodonaCommand):
"""Dodona Message"""
def __init__(self, *args, **kwargs):
"""create Message
If a single positional argument is passed, this is assumed to be the message.
Example:
>>> with Message("This is the message"):
... pass
>>> with Message({
... "format": MessageFormat.SQL,
... "description": "This is the message"
... }):
... pass
If keyword arguments are passed, these are assumed to be the message's content.
Example:
>>> with Message(
... format=MessageFormat.SQL,
... description="This is the message"
... ):
... pass
"""
if len(args) == 1:
super().__init__(message=args[0])
else:
super().__init__(message=kwargs)
def start_msg(self) -> dict:
"""print the "append-message" command and parameters when entering the 'with' block"""
return {"command": "append-message", **self.start_args.__dict__}
def close_msg(self) -> None:
"""don't print anything when exiting the 'with' block"""
class Annotation(DodonaCommand):
"""Dodona Annotation"""
def __init__(self, row: int, text: str, **kwargs):
super().__init__(row=row, text=text, **kwargs)
def start_msg(self) -> dict:
"""print the "annotate-code" command and parameters when entering the 'with' block"""
return {"command": "annotate-code", **self.start_args.__dict__}
def close_msg(self) -> None:
"""don't print anything when exiting the 'with' block"""
class SafeAnnotation(Annotation):
"""Annotation that isn't displayed for negative line numbers"""
def __init__(self, row: int, text: str, **kwargs):
super().__init__(row=row, text=text, **kwargs)
def start_msg(self) -> Optional[dict]:
"""If the row number was less than 0, don't print the annotation"""
if self.start_args.row < 0:
return None
return super().start_msg()