-
Notifications
You must be signed in to change notification settings - Fork 2
/
report.py
56 lines (48 loc) · 1.58 KB
/
report.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
"""Result reporting.
This handles the CLI interface for reporting results.
"""
import click
import json
@click.command(short_help="writes results to either stdout or an output file")
@click.option(
"--format",
type=click.Choice({"json", "markdown", "gradescope"}),
default="markdown",
help="output format to use",
)
@click.option(
"--output", type=click.File("w"), default="-", help="optional file to write output to"
)
def report(format, output):
""" Report in the default format to stdout. """
with open(".grade", "r") as fp:
grades = json.load(fp)
if format == "markdown":
score, max_score = (
sum((t["score"] for t in grades["tests"])),
sum((t["max_score"] for t in grades["tests"])),
)
output.write(
"\n\n".join(
[
f"# Grade Results",
f"## Autograder Score: {score}/{max_score}",
*[
"\n".join(
[
f"### {test['name']} {test['score']}/{test['max_score']}",
"",
f"{test['output'] if 'output' in test else ''}",
]
)
for test in grades["tests"]
],
]
)
)
elif format == "gradescope":
output.write(json.dumps(grades))
elif format == "json":
output.write(json.dumps(grades, indent=4, sort_keys=True))
else:
print(str(grades))