-
Notifications
You must be signed in to change notification settings - Fork 0
/
place_comments.py
69 lines (49 loc) · 2.37 KB
/
place_comments.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
#!/usr/local/bin/python3
# Imports
import codepost
from helpers import parse_test_output, find_function_definition
# Set some required variables
course_name = '<COURSE NAME>'
course_period = '<COURSE PERIOD>'
assignment_name = '<ASSIGNMENT NAME>'
test_output_file = 'tests.txt'
student_code_file = 'homework.py'
codepost.configure_api_key(api_key='<YOUR API KEY>')
###################################################################################################
# try to find course
course_list = codepost.course.list_available(name=course_name, period=course_period)
if len(course_list) == 0:
raise Exception("Couldn't find course with name %s and period %s" % (course_name, course_period))
this_course = course_list[0]
# try to find assignment
this_assignment = this_course.assignments.by_name(name=assignment_name)
if this_assignment is None:
raise Exception("ERROR: couldn't find assignment with name %s in specified course" % (assignment_name))
# retrieve list of assignment's submissions
submissions = this_assignment.list_submissions()
# loop through submissions
for submission in submissions:
# get files corresponding to this submission
test_file = submission.files.by_name(name=test_output_file)
student_code = submission.files.by_name(name=student_code_file)
if (test_file is not None) and (student_code is not None):
# the 'code' of the test output file is the test output
tests_by_function = parse_test_output(test_file.code)
# loop over the functions which were tested at least once
for function_name in tests_by_function.keys():
# use helper function to figure out where the function corresponding to function_name
# was defined
where_to_place = find_function_definition(function_name, student_code.code)
if where_to_place is not None:
test_text = (" \n").join(tests_by_function[function_name])
test_text_escaped = test_text.replace('[', '`[').replace(']', ']`')
# construct codePost comment
comment = {
'text': test_text_escaped,
'file': student_code.id,
'pointDelta': 0,
'rubricComment': None,
**where_to_place,
}
# post the comment to codePost
codepost.comment.create(**comment)