Add rt-set to change status etc on RT tickets
[user/alex/software/my-snippets.git] / card-access
1 import sys
2 import os
3 import csv
4 import subprocess
5 import ldap
6
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"
9
10 def dictize_line(header, line,):
11     line_dict = {}
12     for key, elem in zip(header, line, ):
13         line_dict[key]=elem
14     return line_dict
15
16
17
18 def get_ldap_data(username, fields):
19     con = ldap.open('ldap.mit.edu')
20     con.simple_bind_s("", "")
21     dn = "dc=mit,dc=edu"
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
25     ret = {}
26     for key in result[0][1]:
27         ret[key] = result[0][1][key][0]
28     return ret
29
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 }
38     return data_dict
39     
40 def format_line(data_dict):
41     return format % data_dict
42     
43
44 def do_produce_card_info(db = sys.stdin):
45     reader = csv.reader(db, )
46     lines = []
47
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))
52
53     for line in reader:
54         line_dict = dictize_line(header, line)
55         #print line_dict
56         lines.append(format_line(get_data_dict(line_dict)))
57     print '\n'.join(lines)
58
59 if __name__== '__main__':
60     do_produce_card_info()