-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.py
281 lines (238 loc) · 8.61 KB
/
cli.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
import os
import re
import sys
import traceback
import readline
from typing import NamedTuple, List
from PyInquirer import prompt
from pygments import highlight
from pygments.formatters.terminal import TerminalFormatter
from pygments.lexers.python import PythonLexer
import argparse
parser = argparse.ArgumentParser(description='Enter project endpoint')
parser.add_argument("proj", help="Run 'python3 cli.py <proj>', where <proj> is one of the following: hog cats ants scheme")
args = parser.parse_args()
proj = args.proj
from analyzer import get_problems, Comment
from finalizing import grade
from ok_interface import get_backup_ids, get_backup_code, submit_comment, submit_grade
from colorama import Fore, Style
from templates import template_completer, templates
import argparse
import config
parser = argparse.ArgumentParser(description='Enter project endpoint')
parser.add_argument("proj", help="Run 'python3 cli.py <proj>', where <proj> is one of the following: hog cats ants scheme")
args = parser.parse_args()
config.proj = args.proj
class Grade(NamedTuple):
score: int
message: str
comments: List[Comment]
def clear():
os.system("cls" if os.name == "nt" else "clear")
def display_code_with_accepted_and_potential_comments(
name, problem, accepted_comments, curr_comment=None
):
clear()
print(f"Problem: {name}")
highlighted_code = highlight(problem.code, PythonLexer(), TerminalFormatter())
for i, line in enumerate(highlighted_code.split("\n")):
line_num = problem.initial_line_number + i
if line_num in accepted_comments or (
curr_comment and line_num == curr_comment.line_num
):
print()
print(f"{Fore.GREEN}{line_num} {Style.RESET_ALL}{line}")
if line_num in accepted_comments or (
curr_comment and line_num == curr_comment.line_num
):
indent_level = len(line) - len(line.strip()) + 3
if line_num in accepted_comments:
for accepted_comment in accepted_comments[line_num]:
print(
Fore.MAGENTA
+ " " * indent_level
+ "# "
+ accepted_comment.comment
)
if curr_comment and line_num == curr_comment.line_num:
print(
Fore.RED
+ Style.BRIGHT
+ " " * indent_level
+ "# "
+ curr_comment.comment
)
print()
print()
def complete(comment):
if comment.fields:
print("Please provide supplementary information:")
field_vals = {}
for field in comment.fields:
q = {"type": "input", "name": "field", "message": field + ":"}
response = wrapped_prompt(q)
field_vals[field] = response["field"]
complete_text = comment.comment.format(**field_vals)
q = {
"type": "input",
"name": "final",
"message": "Final message",
"default": complete_text,
}
response = wrapped_prompt(q)
return Comment(comment.line_num, response["final"])
def add_comment(accepted_comments, new_comment):
if not new_comment:
return
if new_comment.line_num not in accepted_comments:
accepted_comments[new_comment.line_num] = []
accepted_comments[new_comment.line_num].append(new_comment)
class Interrupt(Exception):
def __init__(self, cmd):
super()
self.cmd = cmd
def wrapped_prompt(q):
ret = prompt([q])
if not ret:
receive_command()
return ret
def wrapped_input(q):
try:
ret = input(q)
except KeyboardInterrupt:
return receive_command()
return ret
def receive_command():
inp = input(
f"\n\n"
f"cancel = cancel this comment\n"
f"clear = clear all question comments\n"
f"reset = reset all student comments\n"
f"? {Style.BRIGHT}{Fore.RED}command: {Style.RESET_ALL}"
)
raise Interrupt(inp)
def main():
readline.parse_and_bind("tab: complete")
readline.set_completer_delims("")
print("cli.py main")
for id in get_backup_ids():
try:
code = get_backup_code(id)
problems = get_problems(code)
except Exception:
print(
f"{Fore.RED}An exception occurred while processing backup id #{id}",
file=sys.stderr,
)
traceback.print_exc(file=sys.stderr)
print(f"{Style.RESET_ALL}")
continue
grade = grade_backup(problems)
for comment in grade.comments:
print(comment)
assert not comment.fields, "fields not substituted!"
submit_comment(id, comment.line_num, comment.comment)
submit_grade(id, grade.score, grade.message)
def grade_backup(problems):
comments = []
try:
for name, problem in problems.items():
comments.extend(grade_problem(name, problem))
score, message = grade(comments)
print(message)
q = {
"type": "confirm",
"name": "ok",
"message": "Does this grade look reasonable?",
}
response = wrapped_prompt(q)
return Grade(score, message, comments)
except Interrupt as e:
if e.cmd == "reset":
return grade_backup(problems)
raise
def grade_problem(name, problem):
readline.set_completer(template_completer(name))
try:
accepted_comments = {}
for comment in problem.comments:
try:
display_code_with_accepted_and_potential_comments(
name, problem, accepted_comments, comment
)
print(f"{Fore.CYAN}Potential comment: {Style.RESET_ALL}")
print(
f"{Fore.GREEN}{comment.line_num}{Style.RESET_ALL} {comment.comment}"
)
q = {
"type": "confirm",
"name": "ok",
"message": "Add comment",
"default": True,
}
response = wrapped_prompt(q)
if response["ok"]:
add_comment(accepted_comments, complete(comment))
except Interrupt as e:
if e.cmd == "cancel":
continue
raise
while True:
try:
display_code_with_accepted_and_potential_comments(
name, problem, accepted_comments
)
response = wrapped_input(
f"? {Style.BRIGHT} Custom comment type: {Style.RESET_ALL}"
)
if not response:
q = {
"type": "confirm",
"name": "ok",
"message": "Go to next question?",
"default": True,
}
response = wrapped_prompt(q)
if response["ok"]:
break
continue
if response not in templates:
print(
f"{Fore.RED} Template {response} not found! {Style.RESET_ALL}"
)
continue
text = templates[response]
q = {"type": "input", "name": "line_num", "message": "Line number:"}
response = wrapped_prompt(q)
try:
line_num = int(response["line_num"])
except ValueError:
print(
f"{Fore.RED} Expected a number, received {response['line_num']} not found! {Style.RESET_ALL}"
)
continue
if text:
fields = list(set(re.findall(r"{(.*?)}", text)))
comment = Comment(line_num, text, fields)
add_comment(accepted_comments, complete(comment))
else:
q = {"type": "input", "name": "text", "message": "Comment:"}
response = wrapped_prompt(q)
comment = Comment(line_num, response["text"], [])
add_comment(accepted_comments, comment)
except Interrupt as e:
if e.cmd == "cancel":
continue
raise
print()
return list(sum(accepted_comments.values(), []))
except Interrupt as e:
if e.cmd == "clear":
return grade_problem(name, problem)
raise
if __name__ == "__main__":
try:
main()
except:
print(f"{Style.RESET_ALL}")