7 #columns = [ 'timestamp', 'first', 'last', 'email', 'constituency', 'year', 'major', 'cell', 'mitid', 'retreat', 'whynot', 'project', 'othermit', ]
8 format = "%(first_with_initial)s;%(last_canonical)s;%(mitid)s"
10 def dictize_line(header, line,):
12 for key, elem in zip(header, line, ):
18 def get_ldap_data(username, fields):
19 con = ldap.open('ldap.mit.edu')
20 con.simple_bind_s("", "")
22 result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields)
23 if len(result) > 1: print "WARNING: More than one result returned for %s" % username
24 if len(result) < 1: print "WARNING: Only one result returned for %s" % username
26 for key in result[0][1]:
27 ret[key] = result[0][1][key][0]
30 def get_data_dict(line_dict):
31 email = line_dict['email']
32 username = email.replace('@mit.edu', '')
33 ldap = get_ldap_data(username, [ 'cn' , 'sn' , 'givenName' ])
34 first = ldap['givenName'].replace(' ', '_').upper()
35 last = ldap['sn'].replace(' ', '_').upper()
36 mitid = line_dict['mitid']
37 data_dict = { 'first_with_initial': first, 'last_canonical': last, 'mitid': mitid }
40 def format_line(data_dict):
41 return format % data_dict
44 def do_produce_card_info(db = sys.stdin):
45 reader = csv.reader(db, )
48 header = reader.next()
49 line_dict = dictize_line(header, header, )
50 data_dict = { 'first_with_initial': 'FIRST_M', 'last_canonical': 'LASTNAME', 'mitid': 'MIT ID' }
51 lines.append(format_line(data_dict))
54 line_dict = dictize_line(header, line)
56 lines.append(format_line(get_data_dict(line_dict)))
57 print '\n'.join(lines)
59 if __name__== '__main__':
60 do_produce_card_info()