-
Notifications
You must be signed in to change notification settings - Fork 2
/
canvas-wrangler_3.py
executable file
·326 lines (291 loc) · 11.8 KB
/
canvas-wrangler_3.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
#!/usr/bin/env python3
import os
import argparse
import csv
import requests
import pprint
# replace with your own Canvas authentication key
AUTHVAR = 'CANVASPONIES'
# default columns to search for in csv
STUDENT_COL = 'uni'
GRADE_COL = 'grade'
COMMENT_COL = 'comment'
#################################
##### Parser Configurations #####
#################################
# parser object
parser = argparse.ArgumentParser(
description='Upload grades and comments to Canvas')
# grade options
parser.add_argument('-g', '--grade',
default=False, action='store_true',
help='upload grades')
parser.add_argument('-G', '--grade-col',
default=GRADE_COL, type=str,
help='set name for grade header column',
metavar='<header>')
# comment options
parser.add_argument('-c', '--comment',
default=False, action='store_true',
help='upload comments')
parser.add_argument('-C', '--comment-col',
default=COMMENT_COL, type=str,
help='set name for comment header column',
metavar='<header>')
# student options
parser.add_argument('-s', '--sdb',
default='sdb.csv', type=argparse.FileType('rU'),
help='csv spreadsheet containing each student\'s user-id',
metavar='<sdb.csv>')
parser.add_argument('-S', '--student-col',
default=STUDENT_COL, type=str,
help='set name for student header column',
metavar='<header>')
# required positional args
parser.add_argument('course_id',
type=str,
help='course-id from Canvas',
metavar='<course-id>')
parser.add_argument('assignment_id',
type=str,
help='assignment-id from Canvas',
metavar='<assignment-id>')
parser.add_argument('grades',
type=argparse.FileType('rU'),
help='csv spreadsheet containing grades and/or comments',
metavar='<grades.csv>')
# warning log options
parser.add_argument('-L', '--log',
default='', type=str,
help='set filename for warning log',
metavar='<warning.log>')
parser.add_argument('-N', '--no-log',
default=False, action='store_true',
help='do not produce log file for warnings')
# testing options
parser.add_argument('-n', '--no-submit',
default=False, action='store_true',
help='do not submit grades; for testing purposes')
###########################################
##### Parse Args and Preliminary Work #####
###########################################
if __name__=='__main__':
# parse args into args
args = parser.parse_args()
# set assignment id
URL = 'https://courseworks2.columbia.edu/api/v1' \
+'/courses/'+args.course_id \
+'/assignments/'+args.assignment_id \
+'/submissions/update_grades'
# open csv files
grades_name = args.grades.name
grades = csv.reader(args.grades)
sdb_name = args.sdb.name
sdb = csv.reader(args.sdb)
# determine whether or not to submit grades or comments or both
submit_grade = args.grade
submit_comment = args.comment
if not (submit_grade or submit_comment):
submit_grade = submit_comment = True
# set authorization header for POST request
if not AUTHVAR in os.environ.keys():
print('Error: authentication token enviromental variable',
AUTHVAR, 'not set.')
exit(1)
HEADER = {'Authorization': 'Bearer ' + os.environ[AUTHVAR]}
# determine warning log name
if len(args.log) == 0:
log_name = grades_name.rsplit('.', 1)[0] + '-warning.log'
else:
log_name = args.log
####################################
##### Calibrate Column Indices #####
####################################
# populate {UNI: user id} dict from sdb.csv
students = {}
for s in sdb:
students[s[0]] = s[1]
header_col = next(grades)
for i in range(len(header_col)):
if header_col[i].lower() == args.student_col:
uni_col = i
break
try:
uni_col
except NameError:
print('Error: could not find UNI header: ', args.uni_col)
exit(2)
if submit_grade:
for i in range(len(header_col)):
if header_col[i].lower() == args.grade_col:
grade_col = i
break
try:
grade_col
except NameError:
print('Error: could not find grade column header:', args.grade_col)
exit(3)
if submit_comment:
for i in range(len(header_col)):
if header_col[i].lower() == args.comment_col:
comment_col = i
break
try:
comment_col
except NameError:
print('Error: could not find comment column header:',
args.comment_col)
exit(4)
########################
##### Prepare Data #####
########################
post_data = {}
missing_err = []
comment_err = []
grade_err = []
# populate POST request data
# i: line number
# r: row from grades csv
for i, r in enumerate(grades):
if r[uni_col] == "":
continue
if r[uni_col] in students:
cur_uni = r[uni_col]
user = 'grade_data[' + students[r[uni_col]] + ']'
if args.no_submit:
post_data[user + '[uni]'] = cur_uni
if submit_grade:
try:
float(r[grade_col])
post_data[user + '[posted_grade]'] = r[grade_col]
except ValueError:
grade_err.append((i+2, r[uni_col], r[grade_col]))
print('Warning: grade is not numeric')
print('{0}:{1}: {2} '
.format(grades_name, i+2, r[grade_col]))
print('Not submitting grade for', r[uni_col])
print()
if submit_comment:
try:
if not type(r[comment_col]) == str:
r[comment_col].decode('utf-8')
post_data[user + '[text_comment]'] = r[comment_col]
except (UnicodeDecodeError, AttributeError) as e:
comment_err.append((i+2, r[uni_col], r[comment_col]))
print('Warning: comment contains invalid characters')
print(e)
print('{0}:{1}: {2} '
.format(grades_name, i+2, r[comment_col]))
print('Not submitting comment for', r[uni_col])
print()
else:
missing_err.append((i+2, r[uni_col], r[grade_col], r[comment_col]))
print('Warning:', r[uni_col], 'not found in sdb')
print()
########################
##### Log Warnings #####
########################
merrlen, gerrlen, cerrlen = len(missing_err),len(grade_err),len(comment_err)
if not args.no_log:
if (merrlen > 0 or gerrlen > 0 or cerrlen > 0):
err_report = '{0} missing UNIs, {1} non-numeric grades, and ' \
'{2} invalid comments.\n'\
.format(merrlen, gerrlen, cerrlen)
log = open(log_name, 'w')
log.write('=========================================================\n')
log.write('====================== Error Log ========================\n')
log.write('=========================================================\n')
log.write('Gradesheet filename: {0}\n'.format(grades_name))
log.write('Student database filename: {0}\n'.format(sdb_name))
log.write(err_report)
if merrlen > 0:
log.write('\n== Missing UNIs ==')
log.write('\n------------------\n')
for err in missing_err:
log.write('{0}:{1}:'.format(err[0], err[1]))
log.write('\n\tgrade:{0}\n\tcomment:{1}\n'
.format(err[2],err[3]))
if gerrlen > 0:
log.write('\n== Non-numeric Grades ==')
log.write('\n------------------------\n')
for err in grade_err:
log.write('{0}:{1}: {2}\n'.format(err[0], err[1], err[2]))
if cerrlen > 0:
log.write('\n== Invalid Comments ==')
log.write('\n----------------------\n')
for err in comment_err:
log.write('{0}:{1}: {2}\n'.format(err[0], err[1], err[2]))
log.close()
print(err_report)
else:
print('No warnings generated; no warning log created.\n')
else:
print('--no-log: Not creating warning log.\n')
#################################
##### Submission and Report #####
#################################
if args.no_submit:
print(' --no-submit option specified; not submitting to Canvas.')
print('=========================================================')
print('==================== Program Report =====================')
print('=========================================================')
print()
print('== args ==')
print('----------')
pprint.pprint(vars(args))
print
print('== post_data ==')
print('---------------')
pprint.pprint(post_data)
print()
print('=========================================================')
print()
exit(0)
# post request and print(response
try:
res = requests.post(URL, data=post_data, headers=HEADER)
res_code = res.status_code
res = res.json()
except requests.exceptions.ConnectionError:
print('Error: internet connection error')
print('Could not submit grades and comments')
print()
exit(-1)
if res_code == requests.codes.ok:
# success
print('Grades and comments successfully submitted!')
print('=========================================================')
print('================== Submission Report ====================')
print('=========================================================')
print('Course ID:', res['context_id'])
print('Assignment ID:', res['id'])
print()
print('Please wait as Canvas processes this POST request...')
print('Feel free to check its progress at:')
print(res['url'])
print('=========================================================')
print()
exit(0)
else:
print('Error:', res_code)
print('=========================================================')
print('===================== Error Report ======================')
print('=========================================================')
print('Error report ID:', res['error_report_id'])
for error in res['errors']:
print('Error message:', error['message'])
print('Error code:', error['error_code'])
print
print('If that wasn\'t helpful (which it usually isn\'t),')
print('please try the following:')
print('\t* Make sure assignment is published')
print('\t* Remove potential bad unicode characters')
print('\t\t(most commonly smart quotation marks)')
print('\t* Try again later; might actually be a server error?')
print('\t* Try turning your computer off and on again')
print('\t ^^^ please don\'t actually')
print()
print('Also please contact j.hui@columbia.edu about this error')
print('=========================================================')
print()
exit(res_code)