9 #columns = [ 'timestamp', 'first', 'last', 'email', 'constituency', 'year', 'major', 'cell', 'mitid', 'retreat', 'whynot', 'project', 'othermit', ]
10 format = "%(first_with_initial)s;%(last_canonical)s;%(mitid)s"
12 def dictize_line(header, line,):
14 for key, elem in zip(header, line, ):
20 def get_ldap_data(username, fields):
21 con = ldap.open('ldap.mit.edu')
22 con.simple_bind_s("", "")
24 result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields)
25 if len(result) > 1: print "WARNING: More than one result returned for %s" % username
26 if len(result) < 1: print "WARNING: Only one result returned for %s" % username
28 for key in result[0][1]:
29 ret[key] = result[0][1][key][0]
32 def get_data_dict(line_dict):
33 email = line_dict['email']
34 username = email.lower().replace('@mit.edu', '')
35 ldap = get_ldap_data(username, [ 'cn' , 'sn' , 'givenName' ])
36 first = ldap['givenName'].replace(' ', '_').upper()
37 last = ldap['sn'].replace(' ', '_').upper()
38 mitid = line_dict['mitid']
39 data_dict = { 'first_with_initial': first, 'last_canonical': last, 'mitid': mitid }
42 def format_line(data_dict):
43 return format % data_dict
46 def do_produce_card_info(db = sys.stdin):
47 reader = csv.reader(db, )
50 header = reader.next()
51 line_dict = dictize_line(header, header, )
52 data_dict = { 'first_with_initial': 'FIRST_M', 'last_canonical': 'LASTNAME', 'mitid': 'MIT ID' }
53 lines.append(format_line(data_dict))
56 line_dict = dictize_line(header, line)
58 lines.append(format_line(get_data_dict(line_dict)))
59 print '\n'.join(lines)
61 if __name__== '__main__':
62 do_produce_card_info()