-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlongview.py
executable file
·1300 lines (986 loc) · 44.4 KB
/
longview.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""longview.py: Takes CSV data and generates Long View HTML pages.
Usage: ``./longview.py parameter-file``
"""
# Copyright (c) 2004, The Long Now Foundation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import absolute_import, division, with_statement
import csv
import datetime
import math
import os
import shutil
import sys
from optparse import OptionParser
from tempfile import mkdtemp
import lvhtml
import lvnotify
import lvutils
import sliceMaker
__author__ = "Dan Mosedale, James Home, and Ben Keating"
__maintainer__ = "Ben Keating"
__email__ = "[email protected]"
__version__ = "1.1"
__license__ = "BSD-style"
__status__ = "Beta"
#### CONSTANTS
# must match the widths of the actual image files used
pastArrowImageWidth = 56
futureArrowImageWidth = 67
# generate one navcell per this many pixels of timeline space
idealPixelsPerNavCell = 500
# width in pixels of a single navcell
navCellWidth = 25
# minimum and maximum allowed number of nav cells
minNavCells = 2
maxNavCells = 12
#### GLOBALS
navCellsManuallySpecified = False
pixelsPerNavCell = idealPixelsPerNavCell
# maximum number of discussion posts in a month about any topic
maxPosts = 0
# global parameters
params = {}
# assumed to be ordered and contiguous
timelineSections = []
# assumed to be ordered
navcells = []
# a dictionary of dictionaries of dictionaries
interestData = {}
# an array of extra images to include
extraStaticImages = []
def loadhooks(filename):
"""Execute filename in the longview.py global namespace.
filename -- file to execute. Normally this is the template file.
This allows almost all symbols in longview.py to be overridden.
"""
execfile(filename, globals())
return
def writeNowNavCells(outputDir):
"""Write out the PNGs for the "now" navcell.
outputDir -- toplevel dir where output is being generated
"""
# what horizontal pixel in the timeline does "now" start at?
nowTimelineXCoord = getNowBarStartXCoord()
# let's get this as a percent of the area represented by the navcell
startPixelAsPercentage = (
(nowTimelineXCoord - int(params['TIMELINE.leftmargin'])) %
pixelsPerNavCell) / pixelsPerNavCell
# which pixel in the navcell will this be?
navCellNowStart = int(math.floor(startPixelAsPercentage * navCellWidth))
# draw the "onmouseover" cell
navCellOn = lvutils.NowCellPNG(
os.path.join(outputDir, "img-generated", "nav-now-on.png"), True,
navCellNowStart)
navCellOn.generate()
# draw the "onmouseout" cell
navCellOff = lvutils.NowCellPNG(
os.path.join(outputDir, "img-generated", "nav-now-off.png"), False,
navCellNowStart)
navCellOff.generate()
return
def writePopups(outfileObject, rows):
for row in rows:
# if this is a subitem, or an item with identical start and end dates
# only write out a single date
if row['startDate'] == row['endDate']:
dates = row['endDate'].toString(True, True)
else:
# otherwise write both dates as a range
dates = row['startDate'].toString(True, True) + " - " + \
row['endDate'].toString(True,True)
outfileObject.write(
buildPopup(lvhtml.popupTemplate, row['nodeId'], dates,
row['title'], row['args']))
# if we have subitems, write out the popups for them
subitemLetter = 97 # 'a'
for subitem in row['subitems']:
if subitem.has_key('isNotification'):
template = lvhtml.notifyTemplate
else:
template = lvhtml.popupTemplate
outfileObject.write(
buildPopup(template, row['nodeId'] + chr(subitemLetter),
subitem['date'].toString(True, True),
row['title'], subitem['args']))
subitemLetter += 1
return
def buildPopup(template, nodeId, dateString, title, args):
"""Interpolate relevant values into the popup HTML.
template -- HTML template to use for the popup
nodeId -- the node this popup is for
dateString -- the dates represented by the popup, in string form
args -- any additional values from data file to be interpolated
"""
import textwrap
import re
popupHtml = re.sub(r'\\n', "\n", template)
popupHtml = re.sub("%d", dateString, popupHtml)
popupHtml = re.sub("%n", str(nodeId), popupHtml)
popupHtml = re.sub("%t", title, popupHtml)
argNum = 1
for arg in args:
popupHtml = re.sub("%" + str(argNum), textwrap.fill(arg, 71),
popupHtml)
argNum += 1
return popupHtml
def writeLabels(outfileObject, rows):
outfileObject.write("""<div id="labels">
<table class="labelstable" cellpadding="0" cellspacing="0" border="0">
""")
for row in rows:
outfileObject.write(
lvhtml.buildLabel(row['nodeId'], row['title'], row['link']))
outfileObject.write("</table>\n</div>\n\n")
return
def generateNavCellList():
"""Automatically populate the nav cell list if it doesn't exist."""
global navcells
global minNavCells
global maxNavCells
# This code generates navcells based partly on the assumption that the
# the area represented by the navcell starts at the anchor the navcell
# links to, and ends just before the anchor linked to by the next navcell
# in the list. Because of the way browsers (or Mozilla Firefox, at least)
# implement scrolling to anchors, this is probably not the best assumption.
# I suspect we could do better by acting as though the linked-to anchor
# represents the center of the cell.
# figure out number of navcells
numNavCells = int(math.floor(totalWidth / idealPixelsPerNavCell))
# set upper and lower bounds on the number of navcells
if numNavCells < minNavCells:
numNavCells = minNavCells
elif numNavCells > maxNavCells:
numNavCells = maxNavCells
pixelsPerNavCell = math.floor(totalWidth / numNavCells)
# start at the left margin
nextIdealStartPoint = int(params['TIMELINE.leftmargin'])
# iterate through the timeline sections
for section in timelineSections:
# iterate through all the anchors
date = section['startDate']
while date <= section['endDate']:
# if this is the appropriate anchor (ie it's the closest
# anchor to the right of (or at) the ideal pixel)...
if getStartXCoordFromDate(date) >= nextIdealStartPoint:
# generate navcell with this anchor (we'll modify the
# 'now' cell later)
navcells.append({'date': lvutils.LVDate(date),
'now': False})
# bump up nextIdealStartPoint
nextIdealStartPoint = nextIdealStartPoint + pixelsPerNavCell
# prepare for the next iteration
date = date + section['interval']
# mark the "now" cell as such
i = numNavCells - 1
while i >= 0:
if getnowdate() >= navcells[i]['date']:
navcells[i]['now'] = True
break
i -= 1
# iterate through the navcell list, setting the location on each cell
for i in range(len(navcells)):
if i == len(navcells) - 1:
end = timelineSections[-1]['endDate']
else:
end = navcells[i+1]['date'] \
- int(params['TIMELINE.resolutioninmonths'])
navcells[i]['location'] = getNavCellLocation(navcells[i]['date'], end)
# figure out the last date anchor
for section in timelineSections:
date = section['startDate']
while date <= section['endDate']:
date = date + section['interval']
# because of the way anchors work (they only guarantee that the anchor in
# question will be on screen) we really want the last navcell to
# point to the far right of the timeline.
navcells[numNavCells-1]['date'] = date - section['interval']
return
def getNavCellLocation(startDate, endDate):
"""Returns location ("top" or "bottom") for a given navcell to point to.
The decision is made based on which whether more timeline bars are visible
in the top or the bottom section of the timeline between the given dates.
startDate -- LVDate of the start date
endDate -- LVDate of the end date
"""
lastTopRow = int(math.floor(len(rows) / 2))
# iterate through top timeline bars to count how many intersect with the
# timespan of this navcell
topRowsVisible = 0
for i in range(0, lastTopRow):
if rows[i]['startDate'] < endDate and rows[i]['endDate'] > startDate:
topRowsVisible += 1
# iterate through bottom timeline bars
bottomRowsVisible = 0
for i in range(lastTopRow+1, len(rows)):
if rows[i]['startDate'] < endDate and rows[i]['endDate'] > startDate:
bottomRowsVisible += 1
# return whichever half has more
if topRowsVisible >= bottomRowsVisible:
return "top"
else:
return "bottom"
def writeDateTable(outfileObject, tableClass, namePrefix):
outfileObject.write("""
<table class="%s" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ycell">
<img src="img-static/no.gif" width="5" height="1" alt="" border="0" /></td>\n
""" % tableClass)
for section in timelineSections:
# print out all the labels in this section
date = section['startDate']
while date <= section['endDate']:
label = date.toString(monthSeparator="/")
name = date.toString(monthSeparator="_")
outfileObject.write("""<td class="ycell">
<a name="%s%s">%s</a><br />
<img src="img-static/no.gif" width="%d" height="1" alt="" border="0" /></td>\n
""" % (namePrefix, name, label, int(params['TIMELINE.intervalsize'])))
date = date + section['interval']
outfileObject.write("</tr>\n</table>")
return
def getSectionFromDate(date):
for section in timelineSections:
if date >= section['startDate'] and date <= section['endDate']:
return section
warning = "warning: date %d is not contained in any section\n" % date
sys.stderr.write(warning)
raise IndexError, warning
def getStartXCoordFromDate(date):
"""Return the starting X pixel for a given date"""
section = getSectionFromDate(date)
return int(round(section['startPixel'] + (date - section['startDate']) \
* section['pixelsPerMonth']))
def getNowBarStartXCoord():
"""Return the starting X coordinate for the now bar"""
return getStartXCoordFromDate(getnowdate())
def getBarWidth(startDate, endDate):
"""Return the width, in pixels, of this timeline bar"""
endSection = getSectionFromDate(endDate)
width = getStartXCoordFromDate(endDate) \
+ int(params['TIMELINE.resolutioninmonths']) \
* endSection['pixelsPerMonth'] - getStartXCoordFromDate(startDate)
return max(int(round(width)), int(params['TIMELINE.minbarwidth']))
def buildTimelineBar(nodeId, startDate, endDate, link, imagePath, args):
"""Return the HTML for a timeline bar
nodeId -- node in string form of the associated popup
startDate -- starting date of the bar as an LVDate
startDate -- ending date of the bar as an LVDate
link -- clickthrough URL. may be zero-length, meaning no clickthrough
imagePath -- pathname of the image to be used for this bar
args -- any additional arguments from the data file
"""
startPixel = getStartXCoordFromDate(startDate)
width = getBarWidth(startDate, endDate)
if len(link) == 0:
onClick = 'onclick="return false;" '
href = 'javascript:;'
else:
onClick = ''
href = link
return lvhtml.timelineCell % (startPixel, href, nodeId, nodeId, onClick,
nodeId, imagePath, width, getbarheight())
def writeTimelineBars(outfileObject, rows, outputDir, imageSubDir):
"""Write out the timeline bars, both HTML and PNGs.
outfileObject -- file handle to write to
rows -- the list of timeline bars
outputDir -- directory to write output to
imageSubDir -- the subdir of outputDir to write any generated images to
"""
tableWidth = getBarWidth(gettimelinestartdate(), gettimelineenddate()) \
+ int(params['TIMELINE.leftmargin'])
outfileObject.write("""<table id="datatable" cellpadding="0" cellspacing="0" border="0" width="%dpx">\n""" % tableWidth)
colorNum = 2
futureFirstPixel = getNowBarStartXCoord() + getnowbarwidth()
for row in rows:
# alternate the colors of the bars
if colorNum == 1:
colorNum = 2
else:
colorNum = 1
# calculate the last pixel in this bar; we need to see if this
# overlaps with the future area, in which case we need to render
# this bar as an image.
startPixel = getStartXCoordFromDate(row['startDate'])
barLastPixel = getBarWidth(row['startDate'], row['endDate']) \
+ startPixel - 1
outfileObject.write('<tr>\n<td class="data" nowrap="nowrap">\n')
if len(row['subitems']) > 0 or barLastPixel >= futureFirstPixel or \
interestData.has_key(row['nodeId']):
writeImageBar(outfileObject, row, outputDir, imageSubDir,
colorNum)
else:
try:
outfileObject.write(
buildTimelineBar(row['nodeId'], row['startDate'],
row['endDate'], row['link'],
"%s/color%d.png" %
(imageSubDir, colorNum), row['args']))
except IndexError:
sys.stderr.write("; timeline bar skipped")
outfileObject.write("</td>\n</tr>\n\n")
outfileObject.write("</table>\n")
return
def writeSubItems(outfileObject, row, barImage):
"""Write all the subitem diamonds and area elements for this row
outfileObject -- file object to write to
row -- from the global rows[] array
barImage -- SlicedImage object for this event bar
"""
barWidth = getBarWidth(row['startDate'], row['endDate'])
startPixel = getStartXCoordFromDate(row['startDate'])
# we do want an imagemap if there are subitems
outfileObject.write('<map name="node%smap" id="node%smap">\n' %
(row['nodeId'], row['nodeId']))
# generate an area for each subitem
subitemLetter = 97 # 'a'
for subitem in row['subitems']:
x1 = getStartXCoordFromDate(subitem['date']) - startPixel
# if the diamond would be off the right end of the bar, nudge it
# to the left just enough
if x1 > barWidth - int(params['EVENTBAR.diamondwidth']):
x1 -= int(params['EVENTBAR.diamondwidth'])
x2 = x1 + int(params['EVENTBAR.diamondwidth'])
# generate the HTML for this <area>
outfileObject.write(
lvhtml.buildAreaElement("rect", row['nodeId'] + chr(subitemLetter),
x1, 0, x2, getbarheight() - 1))
# draw the diamond into the bar
barImage.drawDiamond(x1)
subitemLetter += 1
# imagemap default area
outfileObject.write(
lvhtml.buildAreaElement("default", row['nodeId'], 0, 0, barWidth,
getbarheight()))
# imagemap footer
outfileObject.write('</map>\n')
# html for the bar itself
outfileObject.write(lvhtml.generatedBar %
(startPixel, row['nodeId'], barWidth,
getbarheight(), row['nodeId']))
return
def writeInterestSlices(barImage, row):
"""Write the interest slices for this into the given image
barImage -- image to render slices into
row -- data for this event bar
"""
# build a slice for every month in the bar until now
leapPixels = 0
month = row['startDate']
while month <= min(getnowdate(), row['endDate']):
# need this to for figuring out month width
sliceSection = getSectionFromDate(month)
# keep track of the amount of pixels lost to rounding errors
fraction, integer = math.modf(sliceSection['pixelsPerMonth'])
if fraction < .5:
leapPixels += fraction
else:
leapPixels -= 1-fraction
# if the number of pixels lost/gained >= 1, compensate
paddingPixels = 0
if leapPixels >= 1.0:
paddingPixels = 1
leapPixels -= 1.0
elif leapPixels <= -1.0:
paddingPixels = -1
leapPixels += 1.0
# if we're at the last interest slice, which is drawn over the
# now bar, we may need to add some pixels of padding to
# compensate for the fact that the now bar may be bigger than
# it ought to because of the minimum bar width
if month == min(getnowdate(), row['endDate']):
paddingPixels += int(round((getnowbarwidth() -
getnowbarvirtualwidth()))) - 1
if not interestData[row['nodeId']].has_key(month):
# build "no-data" slice
barImage.addSlice(
{'height': getbarheight(),
'width': int(round(sliceSection['pixelsPerMonth']))
+ paddingPixels, 'lowerSize': 1.0,
'lowerColor': params['EVENTBAR.nodatacolor'],
'dividerColor': params['EVENTBAR.nodatacolor'],
'upperColor': params['EVENTBAR.nodatacolor'],
'saturation': 1.0, 'brightness': 1.0})
month += 1
continue
interestSlice = interestData[row['nodeId']][month]
# figure out the ratio of "no" votes, avoiding zero division
if interestSlice['noVotes'] == 0 and interestSlice['yesVotes'] == 0:
noRatio = 0.5 # should perhaps actually do a different bg
elif interestSlice['noVotes'] == 0:
noRatio = 0.0
elif interestSlice['yesVotes'] == 0:
noRatio = 1.0
else:
noRatio = interestSlice['noVotes'] / \
(interestSlice['noVotes']+interestSlice['yesVotes'])
# figure out relative discussion intensity
if not interestSlice.has_key('discussionPosts'):
relativePosts = 0.5 # if there's no data, fake it
elif interestSlice['discussionPosts'] == 0:
relativePosts = 0.0
else:
global maxPosts
relativePosts = interestSlice['discussionPosts'] / maxPosts
barImage.addSlice(
{'height': getbarheight(),
'width': int(round(sliceSection['pixelsPerMonth'])) \
+ paddingPixels, 'lowerSize': noRatio,
'lowerColor': params['EVENTBAR.nocolor'],
'dividerColor': params['EVENTBAR.nocolor'],
'upperColor': params['EVENTBAR.yescolor'],
'saturation': float(params['EVENTBAR.saturation']),
'brightness': 1 - relativePosts})
month +=1
return
def writeImageBar(outfileObject, row, outputDir, imageSubDir,
colorNum):
"""Write the HTML and PNG for a bar image, possibly with subitem popups
outfileObject -- file handle to be written to
row -- dictionary of the properties of this row
outputDir -- directory where all output is written
imageSubDir -- subdirectory of outputDir to write generated images to
colorNum -- which of the bar colors (either 1 or 2) is the background
"""
startPixel = getStartXCoordFromDate(row['startDate'])
barWidth = getBarWidth(row['startDate'], row['endDate'])
# do we need a future section?
futureAbsoluteStartXCoord = getNowBarStartXCoord() + getnowbarwidth()
if futureAbsoluteStartXCoord <= startPixel + barWidth:
futureStartXCoord = max(futureAbsoluteStartXCoord - startPixel, 0)
else:
futureStartXCoord = None
# create the image
barImage = sliceMaker.SlicedImage(barWidth, getbarheight())
barImage.diamondWidth = int(params['EVENTBAR.diamondwidth'])
barImage.diamondLeftMargin = int(params['EVENTBAR.diamondleftmargin'])
barImage.diamondVerticalMargin = int(
params['EVENTBAR.diamondverticalmargin'])
# if this bar has interest data, use slicemaker to construct it
if (interestData.has_key(row['nodeId'])):
writeInterestSlices(barImage, row)
else: # there's no interest data, just add a single slice
if futureStartXCoord is None:
nonFutureSliceWidth = barWidth
else:
nonFutureSliceWidth = futureStartXCoord
barImage.addSlice(
{'height': getbarheight(), 'width': nonFutureSliceWidth,
'lowerSize': 1.0,
'lowerColor': params['EVENTBAR.color%d' % colorNum],
'dividerColor': params['EVENTBAR.color%d' % colorNum],
'upperColor': params['EVENTBAR.color%d' % colorNum],
'saturation': 1.0, 'brightness': 1.0})
# add future slice, if necessary
if futureStartXCoord is not None:
barImage.addSlice({'height': getbarheight(),
'width': barWidth - futureStartXCoord,
'lowerSize': 1.0,
'lowerColor': params['EVENTBAR.futurecolor'],
'dividerColor': params['EVENTBAR.futurecolor'],
'upperColor': params['EVENTBAR.futurecolor'],
'saturation': 1.0,
'brightness': 1.0})
if len(row['subitems']) > 0:
writeSubItems(outfileObject, row, barImage)
else:
outfileObject.write(
buildTimelineBar(row['nodeId'], row['startDate'], row['endDate'],
row['link'],
os.path.join(imageSubDir, row['nodeId'] + ".png"),
row['args']))
if params.has_key('EVENTBAR.nowbarontop'):
if params['EVENTBAR.nowbarontop']:
barImage.drawFilledSlice(getNowBarStartXCoord() - startPixel,
getnowbarwidth(),
params['TIMELINE.nowbarcolor'])
# write out the PNG
barImage.generate(os.path.join(outputDir, imageSubDir,
row['nodeId'] + ".png"))
return
def writeTimelineFrame(rows, outputDir):
"""Write the HTML for the timeline frame."""
timelineFile = open(os.path.join(outputDir, "timeline.html"), "wt")
timelineFile.write(lvhtml.timelineTop % gettitle())
writePopups(timelineFile, rows)
writeDateTable(timelineFile, "ytabletop", "")
timelineFile.write(lvhtml.timelinePastNowTable %
(getNowBarStartXCoord() + int(round(getnowbarwidth()/2))
- pastArrowImageWidth,
pastArrowImageWidth, futureArrowImageWidth))
writeTimelineBars(timelineFile, rows, outputDir, "img-generated")
writeDateTable(timelineFile, "ytablebottom", "b")
timelineFile.write("\n\n<br /><br /><br /><br /><br /><br />\n\n")
timelineFile.write('<div id="mysteriousfuture">\n\n\n</div>\n\n\n')
writeLabels(timelineFile, rows)
timelineFile.write(lvhtml.timelineBottom)
timelineFile.close()
return
def writeTimelineBackground(outputDir):
"""Write the background PNG for the timeline.
outputDir -- output directory for the HTML. The PNG will be written to
outputDir/img-generated/timeline-bg.html.
"""
# width of the image is the starting pixel of the last month displayed,
# plus enough pixels to represent that month itself
endDate = timelineSections[len(timelineSections)-1]['endDate']
width = getStartXCoordFromDate(endDate) + \
int(round(getSectionFromDate(endDate)['pixelsPerMonth']
* int(params['TIMELINE.resolutioninmonths'])))
bg = lvutils.Background(width, getbarheight() + 1, getNowBarStartXCoord())
bg.nowColor = params['TIMELINE.nowbarcolor']
if params.has_key('TIMELINE.backgroundstipplecolor'):
bg.stippleColor = params['TIMELINE.backgroundstipplecolor']
bg.dividerWidth = int(params['TIMELINE.minbarwidth'])
bg.nowWidth = getnowbarwidth()
bg.generate(os.path.join(outputDir, "img-generated", "timeline-bg.png"))
return
def writeIndex(outputDir):
"""Write the index (main page) for the HTML frameset
outputDir -- directory to write it into
"""
indexFile = open(os.path.join(outputDir, "index.html"), "wt")
indexFile.write(lvhtml.indexHtml % (gettitle(),
params['TIMELINE.topframeheight'],
getnowcellanchor()))
indexFile.close()
return
def writeHeaderFrame(outputDir):
"""Write the HTML page used in the header frame
outputDir -- directory to write it into
"""
headerFile = open(outputDir + "/header.html", "wt")
headerFile.write(lvhtml.headerTop %
(gettitle(), params['TIMELINE.title'],
timelineSections[0]['startDate'].toString(),
timelineSections[len(timelineSections)-1]
['endDate'].toString(),
timelineSections[0]['startDate'].toString()))
# write out the navcells in the navbar
cellcount = 1
for navcell in navcells:
if navcell['location'].lower() == 'bottom':
anchorPrefix = 'b'
else:
anchorPrefix = ''
if navcell['now']:
onImage = "img-generated/nav-now-on.png"
offImage = "img-generated/nav-now-off.png"
cellName = "navnow"
else:
onImage = "img-static/nav-on.gif"
offImage = "img-static/nav-off.gif"
cellName = "nav" + str(cellcount)
# since this is an anchor name, we need to use "_" instead of "/"
dateString = navcell['date'].toString(monthSeparator="_")
headerFile.write(
lvhtml.buildnavcell(anchorPrefix, dateString, onImage, offImage,
cellName))
cellcount += 1
headerFile.write(lvhtml.headerBottom %
timelineSections[len(timelineSections)-1]
['endDate'].toString())
headerFile.close()
return
def readDataFile(dataFile):
rows = []
reader = csv.reader(open(dataFile, "rt"))
for row in reader:
if row[0] != "":
# check enddate to see if it's the special "?", which
# represents now, but converts differently to a string
if row[2] == "?":
endDate = lvutils.LVDate(getnowdate())
endDate.ongoing = True
else:
endDate = lvutils.LVDate(row[2])
# a regular row
rows.append({'nodeId': row[0], 'startDate': lvutils.LVDate(row[1]),
'endDate': endDate, 'link': row[3],
'title': row[4], 'args': row[5:], 'subitems': []})
else:
# a sub-item
rows[-1]['subitems'].append({'date': lvutils.LVDate(row[1]),
'link': row[2], 'args': row[3:]})
return rows
def readInterestFile(interestFile):
"""Read a config file into the interestData global
interestFile -- name of the file to read interest data from
"""
global interestData
global maxPosts
reader = csv.reader(open(interestFile, "rt"))
for row in reader:
if not interestData.has_key(row[0]):
interestData[row[0]] = {}
interestData[row[0]][lvutils.LVDate(row[1])] = \
{'yesVotes': int(row[2]),
'noVotes': int(row[3])}
# only add the discussion post info if it's there
if len(row) == 5:
interestData[row[0]][lvutils.LVDate(row[1])]['discussionPosts'] = \
int(row[4])
# useful later for calculating relative discussion intensity
if int(row[4]) > maxPosts:
maxPosts = int(row[4])
return
def readParamFile(paramFile):
"""Read a config file of parameters and return them as a dict"""
global params
nowCellSpecified = False
reader = csv.reader(open(paramFile, "rt"))
for row in reader:
# a few things get special cases
if row[0] == 'TIMELINE.section':
interval = lvutils.LVDate(row[3])
if row[2].lower() == "ongoing":
endDate = getnowdate() + interval * 2
else:
endDate = lvutils.LVDate(row[2])
timelineSections.append({'startDate': lvutils.LVDate(row[1]),
'endDate': endDate,
'interval': interval})
elif row[0] == 'TIMELINE.navcell':
if len(row) == 4 and row[3].lower() == 'now':
if nowCellSpecified:
sys.stderr.write(
"warning: more than one 'now' navcell was specified")
now = True
nowCellSpecified = True
else:
now = False
navcells.append({'date': lvutils.LVDate(row[1]),
'location': row[2],
'now': now})
global navCellsManuallySpecified
navCellsManuallySpecified = True
elif row[0] in ('EVENTBAR.color1', 'EVENTBAR.color2',
'EVENTBAR.futurecolor', 'EVENTBAR.nocolor',
'EVENTBAR.yescolor', 'EVENTBAR.nodatacolor',
'TIMELINE.backgroundstipplecolor',
'TIMELINE.nowbarcolor'):
# colors are assumed to be a hex rgb triple
params[row[0]] = (int(row[1], 16), int(row[2], 16),
int(row[3], 16))
elif row[0] == 'TIMELINE.5digityears':
lvutils.alwaysPrint5DigitYears = bool(eval(row[1]))
params[row[0]] = bool(eval(row[1]))
elif row[0] == 'EVENTBAR.nowbarontop':
params[row[0]] = bool(eval(row[1]))
elif row[0] == 'TIMELINE.labelresolution':
if row[1] == "months":
lvutils.dateResolutionDefaultsToYears = False
params[row[0]] = row[1]
elif row[0] == 'TIMELINE.hookfile':
loadhooks(row[1])
params[row[0]] = row[1]
elif row[0] == 'TIMELINE.staticimage':
extraStaticImages.append({'srcName': row[1], 'destName': row[2]})
# everything else is stuffed directly into the params dict
else:
params[row[0]] = row[1]
if navCellsManuallySpecified and not nowCellSpecified:
sys.stderr.write("warning: no 'now' navcell was specified")
def doNotifications():
"""Send out any pending notifications.
Reads the notifications CSV file, and sends out any pending notifications.
"""
# notification is optional
if not params.has_key('NOTIFY.datafile'):
return
# hooks for using a different class as the notifier
if params.has_key('NOTIFY.hookfile'):
lvnotify.init(params['NOTIFY.hookfile'])
notifierClass = params['NOTIFY.notifierclass']
else:
notifierClass = lvnotify.DefaultNotifier
# set up the default notifier
notifier = notifierClass(params['NOTIFY.datafile'], params['NOTIFY.from'],
params['NOTIFY.smtpserver'],
params['TIMELINE.5digityears'])
# override a few defaults, if requested
if params.has_key('NOTIFY.notificationclass'):
notifier.notificationClass = params['NOTIFY.notificationclass']
if params.has_key('NOTIFY.subject'):
notifier.subject = params['NOTIFY.subject']
# iterate through the pending notifications
for notification in notifier.getPendingNotifications():
try:
notifier.notify(notification)
except Exception, ex:
# something went wrong; write out a warning
sys.stderr.write("%s: warning: failed to send notification: "
"'%s': %s\n" %
(sys.argv[0], notification.text, ex));
# we're finished, write out the new version of the datafile
notifier.updateDataFile()
# add notification subitems HTML
for notification in notifier.notifications:
if notification.row is not None:
# find the row that this notification belongs to
rowNum = None
for i in range(len(rows)):
if rows[i]['nodeId'] == str(notification.row):
rowNum = i
break
if rowNum is None:
raise Exception, "Notification points to non-existent row"
rows[rowNum]['subitems'].append(
{'date': notification.date, 'link': '',
'isNotification': True,
'args': [rows[rowNum]['nodeId'], notification.text,