-
Notifications
You must be signed in to change notification settings - Fork 0
/
wwconvert.py
120 lines (94 loc) · 3.36 KB
/
wwconvert.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
#!/usr/bin/python
"""
wwconvert.py
Convert various UGA class roll data formats to standard WebWork .lst
for import.
"""
from __future__ import print_function
import csv
# Format of WebWork .lst for student import is
# [password],[lastname],[firstname],C,,,, [username]@uga.edu,[username]
def studentdict_to_webwork(sdict):
"""
studentdict_to_webwork(sdict)
Given a dict with keys {password, lastname, firstname, username},
return a valid row for WebWork lst import. If data is incomplete,
return None.
"""
try:
return ("{password},{lastname},{firstname}"
",C,,{section},,\t{username}@uga.edu,{username}").format(**sdict)
except KeyError:
return None
def uga_email_to_username(email):
"""
uga_email_to_username(email)
Given a UGA email "[email protected]", return myid. We are probably
guaranteed not to have any "\@" or other confusing characters.
"""
# TODO: Make more robust but don't reinvent wheel...
return email.split("@")[0]
def athena_fullname_to_lastname(fullname):
"""
athena_fullname_to_lastname(fullname)
Given fullname Lastname, Firstname, M., return the student's
last name, usually "Lastname"
"""
return fullname.split(",")[0]
def iterate_athena_csv_file(csvfile):
"""
iterate_athena_csv_file(csvfile)
Yields iterates of {password, lastname, firstname, section, myid}, one
per student, for conversion to WebWork format
"""
# First row of Athena CSV file is header row
# Important columns of Athena CSV file are (0-indexed):
# 1: Preferred first name; we use this as firstname
# 2: Last, First, M.; we grab up to the comma as lastname
# 3: UGA ID; we use this as password
# 9: Email in fmt [username]@uga.edu; grepped appropriately
# 10: Section ID (CRN)
athreader = csv.reader(csvfile)
header = athreader.next() # Grab the header row
for row in athreader:
yield {
'password': row[3],
'lastname': athena_fullname_to_lastname(row[2]),
'firstname': row[1],
'section': row[10],
'username': uga_email_to_username(row[9]),
}
def convert_athena_csv_file(f):
"""
convert_athena_csv_file(f)
Given an Athena CSV file, return a string in WebWork
.lst format that we expect.
"""
lstdata = []
for student in iterate_athena_csv_file(f):
lststudent = studentdict_to_webwork(student)
lstdata.append(lststudent)
return lstdata
def save_webwork_lst(lstdata, ofile):
"""
save_webwork_lst(lstdata, ofile)
Save webwork list data (lines in a list, lstdata) to ofile
(possibly stdout)
"""
for line in lstdata:
print(line, file=ofile)
if __name__ == "__main__":
# Run as a script
import argparse
import sys
parser = argparse.ArgumentParser(
description="Convert various UGA data formats to WW .lst")
parser.add_argument("-o", "--outfile", type=argparse.FileType('w'),
help="File to save to. Default: stdout",
default=sys.stdout)
parser.add_argument("file", nargs="?", type=argparse.FileType('rb'),
help="File to parse.",
default=sys.stdin)
args = parser.parse_args()
lstdata = convert_athena_csv_file(args.file)
save_webwork_lst(lstdata, args.outfile)