-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_lists.py
executable file
·269 lines (209 loc) · 7.11 KB
/
merge_lists.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python3
# Merge the two Ligerbots email lists
import sys
import csv
import getopt
import re
import datetime
addMembers = {}
delMembers = {}
def usage(msg=None):
if msg:
print(msg, file=sys.stderr)
print("merge_lists.py <spreadsheet_csv> <db_dump_csv>", file=sys.stderr)
print("Flags:", file=sys.stderr)
print(" -G Compare to Google email groups", file=sys.stderr)
print("Output to stdout", file=sys.stderr)
sys.exit(1)
return
def formatPhone(instr):
m = re.match(r'^(?P<ac>\d{3})(?P<ex>\d{3})(?P<n>\d{4})$', instr)
if m:
return '%s-%s-%s' % (m.group('ac'), m.group('ex'), m.group('n'))
m = re.match(r'^\((?P<ac>\d{3})\)[ -]*(?P<ex>\d{3})[ -]*(?P<n>\d{4})$', instr)
if m:
return '%s-%s-%s' % (m.group('ac'), m.group('ex'), m.group('n'))
return instr
def mergeGroups(entry, newE):
oldEmail = entry.get('Email', None)
newEmail = newE.get('Email', None)
if not newEmail:
return
school = newE['School']
if not school and entry.get('School', None):
school = entry.get('School', None)
if entry.get('Groups', None) and oldEmail:
oldGroups = set([x.lower().strip() for x in entry['Groups'].split(',')])
else:
oldGroups = set()
newGroups = set([x.lower().strip() for x in newE['Groups'].split(',')])
if 'head_coach' in oldGroups:
newGroups = set(('head_coach',))
# Add to groups
if oldEmail != newEmail:
toadd = newGroups
else:
toadd = newGroups.difference(oldGroups)
for g in toadd:
g2 = g
if g in ('student', 'parent'):
g2 = '%s_%s' % (g, school)
memlist = addMembers.setdefault(g2, [])
memlist.append(newEmail)
# Delete from groups
if oldEmail != newEmail:
todel = oldGroups
else:
todel = oldGroups.difference(newGroups)
for g in todel:
g2 = g
if g in ('student', 'parent'):
g2 = '%s_%s' % (g, school)
memlist = delMembers.setdefault(g2, [])
memlist.append(oldEmail)
return
def mergeEntry(fields, entry, newE):
if not entry.get('Email', None):
newE['SignupDate'] = datetime.date.today().strftime('%Y-%m-%d')
mergeGroups(entry, newE)
# if n: print('groups:', entry)
for f in fields:
if newE.get(f, None):
entry[f] = newE[f]
# if n: print('merged:', entry)
return
def compareSheetWeb(docFile, dbFile):
# load the main dictionary from docFile
f = open(docFile)
csvIn = csv.reader(f)
fields = next(csvIn)
f.seek(0, 0)
csvIn = csv.DictReader(f)
refEntries = [r for r in csvIn]
f.close()
# create a few indices of the existing users
usernames = {}
emails = {}
for e in refEntries:
if e.get('Email', None):
e['Email'] = e['Email'].lower()
e['HasWebAccount'] = ''
if e['Lastname'] and e['Firstname']:
usernames[(e['Lastname'], e['Firstname'])] = e
if e['Email']:
emails[e['Email']] = e
# Now, go through the other file and merge in any changes
f = open(dbFile)
csvIn = csv.DictReader(f)
for newE in csvIn:
for k, v in newE.items():
newE[k] = v.strip()
if newE[k] == 'NULL':
newE[k] = None
if k == 'Email':
newE[k] = newE[k].lower()
oldE = emails.get(newE['Email'], None)
if oldE is None:
key = (newE['Lastname'], newE['Firstname'])
oldE = usernames.get(key, None)
if oldE is None:
oldE = {}
refEntries.append(oldE)
oldE['HasWebAccount'] = 1
mergeEntry(fields, oldE, newE)
# clean up some fields. Make sure to go through the whole list
for entry in refEntries:
if entry.get('Phone', None):
entry['Phone'] = formatPhone(entry['Phone'])
if entry.get('Emergency_Phone', None):
entry['Emergency_Phone'] = formatPhone(entry['Emergency_Phone'])
if re.match(r'^\d{4}$', entry['Zipcode']):
entry['Zipcode'] = '0' + entry['Zipcode']
if re.match(r'^\d', entry['Zipcode']):
entry['Zipcode'] = "'" + entry['Zipcode']
# Done. Output the list
csvOut = csv.DictWriter(sys.stdout, fieldnames=fields, extrasaction='ignore')
# need to output header
csvOut.writeheader()
for e in refEntries:
csvOut.writerow(e)
print('Add lists:', file=sys.stderr)
for k, v in sorted(addMembers.items()):
print(k, "\t", ', '.join(v), file=sys.stderr)
print('', file=sys.stderr)
print('Delete lists:', file=sys.stderr)
for k, v in sorted(delMembers.items()):
print(k, "\t", ', '.join(v), file=sys.stderr)
return
def compareGoogleSheet(googleFile, docFile):
# load the Group membership data
groups = {}
f = open(googleFile)
csvIn = csv.DictReader(f)
for row in csvIn:
grp = row['Group'].strip().lower()
grpList = groups.get(grp, None)
if not grpList:
grpList = []
groups[grp] = grpList
grpList.append(row['Email'].strip().lower())
f.close()
# Now, go through the other file and merge in any changes
f = open(docFile)
csvIn = csv.DictReader(f)
for row in csvIn:
email = row['Email'].strip().lower()
if not email:
continue
school = row['School'].strip().lower()
isParent = re.search(r'parent', row['Groups'], re.IGNORECASE)
for grp in row['Groups'].split(','):
grp = grp.strip()
if grp in ('student', 'parent'):
grpName = '%s_%s' % (grp, school)
grpName = grpName.lower()
elif grp == 'mentor':
if isParent:
grpName = 'mentor_parent'
else:
grpName = 'mentor_other'
else:
grpName = grp
if grpName in ('exec', 'alumni', 'alum'):
continue
grpList = groups.get(grpName, None)
if not grpList:
print('Unknown group "%s" for "%s"' % (grpName, email), file=sys.stderr)
continue
if email in grpList:
grpList.remove(email)
else:
x = addMembers.get(grpName, None)
if not x:
x = []
addMembers[grpName] = x
x.append(email)
print('Add lists:')
for k, v in sorted(addMembers.items()):
print(k, "\t", ', '.join(v))
print
print('Delete lists:')
for k, v in sorted(groups.items()):
if v:
print(k, "\t", ', '.join(v))
# ------------------------------------------------------------
if __name__ == '__main__':
try:
optlist, pargs = getopt.getopt(sys.argv[1:], 'G')
except Exception:
usage('Unknown option')
if len(pargs) != 2:
usage()
# convert opts to something useful!!!
opts = {}
for x in optlist:
opts[x[0]] = x[1]
if '-G' in opts:
compareGoogleSheet(pargs[0], pargs[1])
else:
compareSheetWeb(pargs[0], pargs[1])