/
makelocationreport.py
executable file
·474 lines (398 loc) · 17.9 KB
/
makelocationreport.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python3
"""Create the proposed relocation report"""
import tmutil, sys, os, csv, re
from tmutil import distance_on_unit_sphere
from makemap import Bounds
from simpleclub import Club
from datetime import datetime
import tmglobals
myglobals = tmglobals.tmglobals()
minimalhead = """
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Summary Proposed Alignment</title>
<style type="text/css">
body {font-family: Arial; font-size: 10pt;}
.area {width: 100%; page-break-inside: avoid; display: block; clear: both;}
.divname {font-size: 200%; font-weight: bold; text-align: center; margin-bottom: 20px;}
.areaname {font-size: 150%; font-weight: bold; text-align: center;}
.division {display: block; float: none; position: relative; page-break-inside: avoid; }
.notfirst {break-before: page !important; page-break-before: always !important;}
.areatable {font-size: 10pt; border: 1px solid black; border-collapse: collapse;}
.myrow td {vertical-align: top; padding-left: 3px; padding-right: 3px; border: 1px solid black; border-collapse: collapse}
th {font-weight: bold; border: 1px solid black; border-collapse: collapse;}
.ghost {background-color: #C0C0C0;}
.myrow {border: 1px solid black; border-collapse: collapse;}
.cnum {text-align: right; width: 10%; padding-left: 2px; padding-right: 2px;}
.cname {text-align: left; width: 80%; font-weight: bold; padding-left: 2px;}
.from {text-align: left; width: 5%; padding-left: 2px; padding-right: 2px;}
.gone {font-style: italic; background-color: #E0E0E0}
.area {
width: 95%;
margin: auto;
padding: 10px;
}
@media all and (min-width: 768px) {
.areapair {clear: both; margin-top: 30px;}
.left {
width: 45%; float: left; }
.right {
width: 45%; margin-left: 50%; }
}
@media all and (max-width: 767px) {
.left {margin-top: 20px;}
.right {margin-top: 20px;}
}
@media not print {
.divname { border-top: 2px solid black; padding-top: 10px; }
}
@media print {
.division {border: none;}
}
.clearfix:after {
content: "";
display: table;
clear: both;
.clearfix:before {
content: "";
display: table;
clear: both;}
}
</style>
</head>
<body>
"""
global parms
def inform(*args, **kwargs):
""" Print information to 'file' unless suppressed by the -quiet option.
suppress is the minimum number of 'quiet's that need be specified for
this message NOT to be printed. """
suppress = kwargs.get('suppress', 1)
file = kwargs.get('file', sys.stderr)
if parms.quiet < suppress:
print(' '.join(args), file=file)
### Insert classes and functions here. The main program begins in the "if" statement below.
def computeMaxDistance(locations):
"""locations is an array of clubname/lat/long tuples"""
maxd = 0
maxc1 = ''
maxc2 = ''
for i in range(len(locations)):
(c1, lat1, lng1) = locations[i]
for (c2, lat2, lng2) in locations[i+1:]:
try:
d = distance_on_unit_sphere(float(lat1), float(lng1), float(lat2), float(lng2))
if (d > maxd):
maxd = d
maxc1 = c1
maxc2 = c2
except ValueError:
pass
return (maxd, maxc1, maxc2)
def makeareamap(area, locations):
b = Bounds()
mapbase="https://maps.googleapis.com/maps/api/staticmap?key=" + parms.staticmapsapikey + "&"
mapparts = []
clubinfo = []
marker = 'A'
for (clubname, latitude, longitude) in locations:
b.extend(latitude, longitude)
mapparts.append('markers=label:%s%%7C%s,%s' % (marker, latitude, longitude))
marker = chr(ord(marker)+1)
# mapparts.append("center=%s" % (b.centercoords()))
mapparts.append("size=640x640&scale=2") # As large as possible, at least for now
return '%s%s' % (mapbase, '&'.join(mapparts))
def openarea(outfile, area, color):
ret = []
ret.append('<div class="area clearfix">\n')
ret.append('<h4>Area %s (%%.2f miles between %%s and %%s)</h4>\n' % area)
ret.append('<div class="areainfo">')
ret.append('<table class="areatable">\n')
ret.append('<thead>\n')
ret.append('<tr>\n')
if parms.map:
ret.append('<th class="marker">ID</th>')
ret.append('<th class="from">From</th>')
ret.append('<th class="cnum">Number</th><th class="cname">Name</th>')
if parms.color:
ret.append('<th class="members">Members</th><th class="goals">Goals</th>')
ret.append('<th class="loc">Location</th><th class="mtg">Time</th>\n')
ret.append('</tr>\n')
ret.append('</thead><tbody>\n')
return ret
def closearea(outfile, text, locations, gonefrom):
# This is gross and relies on knowing what "openarea" does.
if len(locations) > 1:
maxdistinfo = computeMaxDistance(locations)
if maxdistinfo[0] == 0.0:
text[1] = re.sub(r'\(.*\)', '', text[1]) # Remove distance
else:
text[1] = text[1] % computeMaxDistance(locations)
else:
text[1] = re.sub(r'\(.*\)', '', text[1]) # Remove distance
outfile.write(''.join(text))
outfile.write('</tbody></table>\n')
if gonefrom:
outfile.write('<p>Club%s leaving area:</p>\n' % ('' if len(gonefrom) == 1 else 's'))
outfile.write('<ul class="gonelist">\n')
for club in sorted(gonefrom, key=lambda c: c.clubname.lower()):
outfile.write('<li>%s' % club.clubname)
if club.eligibility == 'Suspended':
outfile.write(' <b>(Suspended)</b>')
elif club.newarea and club.newdivision.strip():
outfile.write(' (To %s%s)' % (club.newdivision, club.newarea))
else:
outfile.write(' (Moved out of Division 101)')
outfile.write('</li>\n')
outfile.write('</ul>\n')
outfile.write('</div>\n')
if len(locations) > 1 and parms.map:
outfile.write('<div class="areamap">')
outfile.write('<img src="%s" width="640px">' % makeareamap(text[1].split()[2], locations))
outfile.write('</div>')
outfile.write('</div>\n')
def opendiv(outfile, division):
outfile.write('<div class="division"><h3>Division %s</h3>\n' % division)
def closediv(outfile):
outfile.write('</div>\n')
if __name__ == "__main__":
import tmparms
parms = tmparms.tmparms()
parms.add_argument('--quiet', '-q', action='count', default=0)
parms.add_argument('--infile', default='d101align.csv')
parms.add_argument('--outfile', default='d101-locations-by-area.html')
parms.add_argument('--outdir', default=None, help='Where to put the output file')
parms.add_argument('--color', action='store_true')
parms.add_argument('--map', action='store_true')
parms.add_argument('--minimal', default='d101minimal.html')
# Do global setup
myglobals.setup(parms)
conn = myglobals.conn
curs = myglobals.curs
# Your main program begins here.
# Write the header of the output file
outfile = open(os.path.join(parms.outdir, parms.outfile),'w')
outfile.write("""
<html>
<head>
<title>Detailed Proposed Alignment</title>
<style type="text/css">
body {font-family: Arial }
.divname {font-size: 200%; font-weight: bold; text-align: center; margin-bottom: 20px;}
.area {margin-bottom: 12pt; width: 100%; page-break-inside: avoid; display: block; clear: both;}
.division {border: none; break-before: always !important; display: block; float: none; position: relative; page-break-inside: avoid; page-break-after: always !important;}
.areatable {font-size: 10pt; border: 1px solid black; border-collapse: collapse; page-break-inside:avoid; break-inside: avoid; -webkit-column-break-inside: avoid;}
.myrow td {vertical-align: top; padding-left: 3px; padding-right: 3px; border: 1px solid black;}
th {font-weight: bold; border: 1px solid black; border-collaps: collapse;}
.ghost {background-color: #C0C0C0;}
.myrow {border: 1px solid black; border-collapse: collapse;}
.cnum {text-align: right; width: 7%;}
.cname {text-align: left; width: 15%; font-weight: bold;}
.color {text-align: left; width: 5%;}
.marker {text-align: left; width: 5%; font-weight: bold;}
.from {text-align: center; width: 5%;}
.red {background-color: red;}
.yellow {background-color: yellow;}
.green {background-color: lightgreen;}
.goals {text-align: right; width: 5%;}
.members {text-align: right; width: 5%;}
.mtg {text-align: left; width: 25%;}
.loc {text-align: left; width: 30%;}
.area {
width: 95%;
margin: auto;
padding: 10px;
}
""")
if parms.map:
outfile.write("""
.areainfo {
width: 45%;
float: left;
}
.areamap {
margin-left: 50%;
}
""")
else:
outfile.write("""
.areainfo { width: 95%; float: left;}
""")
outfile.write("""
.clearfix:after {
content: "";
display: table;
clear: both;
}
</style>
</head>
<body>
""")
# Get club information from the database as of today
clubs = Club.getClubsOn(curs, date=datetime.today().strftime('%y-%m-%d'))
# Now, add relevant club performance information. If there are clubs in the
# performance data which aren't in the master list from TMI, note it and add
# them anyway.
perffields = ['clubnumber', 'clubname', 'district', 'area', 'division', 'eligibility', 'color', 'membase', 'activemembers', 'goalsmet']
curs.execute("""SELECT clubnumber, clubname, district, area, division, clubstatus
AS eligibility, color, membase, activemembers, goalsmet FROM clubperf
WHERE id IN (SELECT clubperf_id FROM lastfor WHERE tmyear = %s) AND district = %s""",
(myglobals.tmyear, parms.district))
for info in curs.fetchall():
clubnum = Club.stringify(info[0])
try:
club = clubs[clubnum]
club.addvalues(info, perffields)
except KeyError:
# print('Club %s (%d) not in current CLUBS table, patching in.' % (info[1], info[0]))
clubs[clubnum] = Club(info, perffields)
clubs[clubnum].charterdate = ''
# Now patch in suspension dates
curs.execute("SELECT clubnumber, suspenddate FROM distperf WHERE entrytype = 'L' and district = %s", (parms.district,))
for (clubnum, suspenddate) in curs.fetchall():
if clubnum in clubs:
clubs[clubnum].addvalues(['suspended'],[suspenddate])
# And read in the alignment.
reader = csv.DictReader(open(parms.infile, 'r'))
alignfields = ['newarea', 'newdivision', 'likelytoclose', 'meetingday', 'meetingtime', 'place', 'address', 'city',
'state', 'zip', 'latitude', 'longitude', 'onlineonly']
for row in reader:
newarea = row['newarea']
if not newarea:
newarea = '@'
row['newarea'] = newarea[1:]
row['newdivision'] = newarea[0]
try:
club = clubs[club.stringify(row['clubnumber'])]
except KeyError:
print('club %s (%s) is new.' % (row['clubname'], row['clubnumber']))
clubvalues = [row['clubnumber'], row['clubname'], ' ', ' ', parms.district, 0, 0, 'Prospective', '']
clubfields = ['clubnumber', 'clubname', 'area', 'division', 'district',
'activemembers', 'goalsmet', 'eligibility', 'color']
club = Club(clubvalues, fieldnames=clubfields)
clubs[club.stringify(row['clubnumber'])] = club
alignvalues = [row[p] for p in alignfields]
club.updatevalues(alignvalues, alignfields)
# Assign clubs to their new areas. If they've changed, also assign to the area they're leaving
gonefrom = {}
newareas = {}
divs = {}
for c in list(clubs.values()):
old = c.division + c.area
try:
new = c.newdivision + c.newarea
except AttributeError:
new = ' '
c.newdivision = ' '
c.newarea = ' '
if old != new:
c.was = '(from %s)' % old
else:
c.was = ' '
if old not in gonefrom:
gonefrom[old] = []
if new not in newareas:
newareas[new] = []
try:
if c.eligibility != 'Suspended':
newareas[new].append(c)
if old != new:
gonefrom[old].append(c)
if c.newdivision not in divs:
divs[c.newdivision] = {}
divs[c.newdivision][new] = new
else:
gonefrom[old].append(c)
except AttributeError:
print('no eligibility for', c.clubname)
for div in sorted(divs.keys()):
if div.strip():
areas = sorted(divs[div].keys())
opendiv(outfile, div)
for area in areas:
locations = []
accum = openarea(outfile, area, parms.color)
for c in sorted(newareas[area], key=lambda x:int(x.clubnumber)):
marker = 'A'
row = c.__dict__
outrow = []
row['closing'] = '<br />(probably closing)' if row['likelytoclose'] else ''
outrow.append('<tr class="myrow%s">' % (' ghost' if row['likelytoclose'] else '') )
if parms.map:
outrow.append(' <td class="marker">%s</marker>' % marker)
oldarea = c.division + c.area
newarea = c.newdivision + c.newarea
outrow.append(' <td class="from">%s</td>' % (oldarea if oldarea != newarea else ''))
outrow.append(' <td class="cnum">{clubnumber}</td><td class="cname%s">{clubname}{closing}</td>' % (' {color}' if parms.color else ''))
if parms.color:
outrow.append(' <td class="members">{activemembers}</td>\n')
outrow.append(' <td class="goals">{goalsmet}</td>\n')
if row['onlineonly'].lower().strip().startswith('y'):
outrow.append(' <td class="loc"><b>Online Club</b><br />{place}<br />{address}<br />{city}, {state} {zip}</td>')
else:
outrow.append(' <td class="loc">{place}<br />{address}<br />{city}, {state} {zip}</td>')
locations.append((row['clubname'], row['latitude'], row['longitude']))
outrow.append(' <td class="mtg"><b>{meetingday}</b><br />{meetingtime}</td>')
outrow.append('</tr>')
accum.append(('\n'.join(outrow)).format(**row))
marker = chr(ord(marker)+1)
closearea(outfile, accum, locations, gonefrom.get(area,[]))
closediv(outfile)
outfile.write("</body></html>\n")
outfile.close()
# And now, create the mimimal version of the report
outfile = open(os.path.join(parms.outdir,parms.minimal), 'w')
outfile.write(minimalhead)
firstdiv = True
for div in sorted(divs.keys()):
if div.strip():
outfile.write('<div class="clearfix division%s">\n' % ('' if firstdiv else ' notfirst'))
firstdiv = False
outfile.write('<p class="divname">Division %s</p>' % div)
areas = sorted(divs[div].keys())
# Now, we write areas out two at a time
left = True
for area in areas:
if left:
outfile.write('<div class="areapair clearfix">\n')
locations = []
outfile.write('<div class="areadiv %s">\n' % ('left' if left else 'right'))
outfile.write('<table class="areatable"><thead>\n')
outfile.write('<tr><th class="areaname" colspan="3">Area %s</th></tr>' % area)
outfile.write('<tr>\n')
outfile.write('<th class="from">From</th>')
outfile.write('<th class="cnum">Number</th><th class="cname">Name</th>')
outfile.write('</tr>\n')
outfile.write('</thead><tbody>\n')
for c in sorted(newareas[area], key=lambda x:int(x.clubnumber)):
row = c.__dict__
outrow = []
outrow.append('<tr class="myrow">')
oldarea = c.division + c.area
newarea = c.newdivision + c.newarea
outrow.append(' <td class="from">%s</td>' % (oldarea if oldarea != newarea else ''))
outrow.append(' <td class="cnum">{clubnumber}</td><td class="cname">{clubname}</td>')
outrow.append('</tr>')
outfile.write(('\n'.join(outrow)).format(**row))
# Now, process clubs which moved away or were
gonelist = gonefrom.get(area,[])
if gonelist:
for club in sorted(gonelist, key=lambda c:c.clubname.lower()):
outfile.write('<tr><td class="gone" colspan="3">%s' % club.clubname)
if club.eligibility == 'Suspended':
outfile.write(' <b>(Suspended)</b>')
elif club.newarea.strip() and club.newdivision.strip():
outfile.write(' (To %s%s)' % (club.newdivision, club.newarea))
else:
outfile.write(' (Moved out of Division 101)')
outfile.write('</td></tr>\n')
outfile.write('</tbody></table>\n')
outfile.write('</div><!--areadiv-->\n')
if not left:
outfile.write('</div><!--areapair-->\n')
left = not left
# Finished with this division
outfile.write('</div>\n')
outfile.write("</body></html>\n")
outfile.close()