-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrading_protocol.py
230 lines (190 loc) · 7.58 KB
/
grading_protocol.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
import csv
class LadokDialect(csv.Dialect):
delimiter = ";"
quotechar = '"'
escapechar = None
doublequote = True
skipinitialspace = False
lineterminator = "\r\n"
quoting = csv.QUOTE_ALL
header_personnummer = "Personal identity number"
header_gitlab_username = "Chalmers GitLab username"
header_name = "Name"
header_grade = "Grade"
header_examination_date = "Examination date"
def report_groups_headers(course):
def f():
yield header_personnummer
yield header_gitlab_username
yield header_name
for lab in course.labs.values():
yield lab.name
return list(f())
def write_group_membership_report(course):
"""
Argument path is interpreted relative to course.dir
"""
with (course.dir / "groups.csv").open("w") as file:
writer = csv.DictWriter(file, report_headers(course), dialect=LadokDialect)
writer.writeheader()
for canvas_user in course.canvas_course.students:
gitlab_username = course.gitlab_username_by_canvas_id(canvas_user.id)
if gitlab_username is None:
continue
def f(lab):
# pylint: disable=cell-var-from-loop
g = lab.group_by_gitlab_username.get(gitlab_username)
if g is None:
return None
return g.id
entry = {
header_personnummer: canvas_user.sis_user_id,
header_gitlab_username: gitlab_username,
header_name: canvas_user.sortable_name,
} | {lab.name: f(lab) for lab in course.labs.values()}
writer.writerow(entry)
def report_headers(course):
def f():
yield header_personnummer
yield header_gitlab_username
yield header_name
for lab in course.labs.values():
yield lab.name
yield header_grade
yield header_examination_date
return list(f())
def read_report_skeleton(course, path):
"""
Argument path is interpreted relative to course.dir
"""
with (course.dir / path).open() as file:
# Restrict headers.
reader = csv.DictReader(
file, fieldnames=[header_personnummer, header_name], dialect=LadokDialect
)
# Ignore header row.
it = iter(reader)
next(it)
return list(it)
def write_report(course, path, entries):
"""
Argument path is interpreted relative to course.dir
"""
with (course.dir / path).open("w") as file:
writer = csv.DictWriter(file, report_headers(course), dialect=LadokDialect)
writer.writeheader()
for entry in entries:
writer.writerow(entry)
def prepare_requested_entries(
course, requested_entries, by_gitlab_username, examination_date=""
):
"""
Takes a list of skeleton entries (containing at least a value for header_personnummer)
and returns a corresponding list of entries filled from by_gitlab_username.
Arguments:
* course: Instance of Course.
* requested_entries:
The given list of skeleton entries.
Each entry is a map with a value for header_personnummer.
If it contains a value for header_name, this is passed along in the result.
* by_gitlab_username:
As returned by Course.grading_report_with_summary.
This method mutates this map by popping used entries.
* examination_date:
Value to use for the key header_examination_date.
"""
def f():
for entry in requested_entries:
personnummer = entry[header_personnummer].replace("-", "")
canvas_user = course.canvas_course.user_by_sis_id(personnummer)
if canvas_user is None:
print(f"WARNING: No Canvas user found for personnumber {personnummer}")
continue
gitlab_username = course.gitlab_username_by_canvas_id(canvas_user.id)
if gitlab_username is None:
print(
f"WARNING: No Chalmers GitLab user found Canvas user {canvas_user.name}"
)
continue
value = by_gitlab_username.pop(gitlab_username, ({}, None))
name = entry.get(header_name)
if name is None:
name = canvas_user.sortable_name
yield {
header_personnummer: entry[header_personnummer],
header_gitlab_username: gitlab_username,
header_name: entry[header_name],
} | course.grading_report_format_value(value) | {
header_examination_date: examination_date,
}
return list(f())
def prepare_remaining_entries(course, by_gitlab_username):
"""
Returns a list of entries for the given grading report (with summary).
Typically used on the map remaining after processing the requested entries with prepare_requested_entries.
Arguments:
* course: Instance of Course.
* by_gitlab_username:
As returned by Course.grading_report_with_summary (and potentially modified by prepare_requested_entries).
"""
def f(x):
(gitlab_username, value) = x
canvas_user = course.canvas_user_by_gitlab_username.get(gitlab_username)
return (
{
header_personnummer: canvas_user.sis_user_id if canvas_user else "",
header_gitlab_username: gitlab_username,
header_name: canvas_user.sortable_name if canvas_user else "",
}
| course.grading_report_format_value(value)
| {
header_examination_date: "",
}
)
return list(map(f, by_gitlab_username.items()))
def report_course(
course, xs, path_extra, update_lab=True, summary=None, examination_date=""
):
"""
Prepare the assignment protocol for this course.
Arguments:
* course: Instance of Course.
* xs:
List of pairs (requests, filled_out) of paths, interpreted relative to course.dir.
The first path is an input file:
a tab-separated CSV file containing requests (at least a column for header_personnummer).
The second path is an output file:
a tab-separated CSV file containing the filled out requests.
* update_lab:
Whether to update the lab in this function.
TODO: make possible with less updates.
* path_extra:
Path to an output file:
a tab-separated CSV file containing lab grades for students not appearing in the input files requests.
"""
# Make sure all requests are processed.
for lab in course.labs.values():
lab.setup_request_handlers()
lab.parse_response_issues()
if update_lab:
lab.repo_fetch_all()
lab.parse_request_tags(False)
lab.process_requests()
by_gitlab_username = course.grading_report_with_summary(
strict=False, summary=summary
)
for path_requests, path_filled_out in xs:
requested_entries = read_report_skeleton(course, path_requests)
filled_out_entries = prepare_requested_entries(
course,
requested_entries,
by_gitlab_username,
examination_date=examination_date,
)
write_report(course, path_filled_out, filled_out_entries)
extra_entries = prepare_remaining_entries(course, by_gitlab_username)
write_report(course, path_extra, extra_entries)
# Example invocation:
# section_names = ['Chalmers DAT038', 'Chalmers DAT525', 'Chalmers TDA417']
# section_names = ['Chalmers DAT495', 'GU DIT182']
# report_course(c, [(section_name + '.in.csv', section_name + '.csv') for section_name in section_names], 'report_extra.csv'])