+import sys
+import os
+import csv
+import subprocess
+import ldap
+
+#columns = [ 'timestamp', 'first', 'last', 'email', 'constituency', 'year', 'major', 'cell', 'mitid', 'retreat', 'whynot', 'project', 'othermit', ]
+format = "%(first_with_initial)s;%(last_canonical)s;%(mitid)s"
+
+def dictize_line(header, line,):
+ line_dict = {}
+ for key, elem in zip(header, line, ):
+ line_dict[key]=elem
+ return line_dict
+
+
+
+def get_ldap_data(username, fields):
+ con = ldap.open('ldap.mit.edu')
+ con.simple_bind_s("", "")
+ dn = "dc=mit,dc=edu"
+ result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields)
+ if len(result) > 1: print "WARNING: More than one result returned for %s" % username
+ if len(result) < 1: print "WARNING: Only one result returned for %s" % username
+ ret = {}
+ for key in result[0][1]:
+ ret[key] = result[0][1][key][0]
+ return ret
+
+def get_data_dict(line_dict):
+ email = line_dict['email']
+ username = email.replace('@mit.edu', '')
+ ldap = get_ldap_data(username, [ 'cn' , 'sn' , 'givenName' ])
+ first = ldap['givenName'].replace(' ', '_').upper()
+ last = ldap['sn'].replace(' ', '_').upper()
+ mitid = line_dict['mitid']
+ data_dict = { 'first_with_initial': first, 'last_canonical': last, 'mitid': mitid }
+ return data_dict
+
+def format_line(data_dict):
+ return format % data_dict
+
+
+def do_produce_card_info(db = sys.stdin):
+ reader = csv.reader(db, )
+ lines = []
+
+ header = reader.next()
+ line_dict = dictize_line(header, header, )
+ data_dict = { 'first_with_initial': 'FIRST_M', 'last_canonical': 'LASTNAME', 'mitid': 'MIT ID' }
+ lines.append(format_line(data_dict))
+
+ for line in reader:
+ line_dict = dictize_line(header, line)
+ #print line_dict
+ lines.append(format_line(get_data_dict(line_dict)))
+ print '\n'.join(lines)
+
+if __name__== '__main__':
+ do_produce_card_info()