-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.py
132 lines (103 loc) · 3.51 KB
/
build.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
import csv
import sys
from jinja2 import Environment, FileSystemLoader
def build_template(env, template_name, **kwargs):
print "Building %s..." % template_name
template = env.get_template(template_name)
with open(template_name, "w") as f:
f.write(template.render(**kwargs))
def parse_csv(filename):
with open(filename, 'rU') as f:
return list(csv.DictReader(f))
def build_index(env):
sponsors = parse_csv("data/prev_sponsors.csv")
sponsor_classes = dict()
sponsor_classes['mezzanine'] = []
sponsor_classes['series-a'] = []
sponsor_classes['ipo'] = []
levels = [{'name': 'mezzanine', 'h': 3},
{'name': 'series-a', 'h': 4},
{'name': 'ipo', 'h': 5}]
for sponsor in sponsors:
sponsor_classes[sponsor['level']].append(sponsor)
competitions = parse_csv("data/competitions.csv")
build_template(env, 'index.html',
sponsor_classes=sponsor_classes,
sponsors=sponsors,
levels=levels,
competitions=competitions,
)
def build_history(env):
build_template(env, 'history.html')
def build_sponsorship(env):
build_template(env, 'sponsorship.html')
def build_faq(env):
build_template(env, 'faq.html')
def build_rules(env):
build_template(env, 'rules.html')
def build_about(env):
build_template(env, 'about.html')
def build_press(env):
stories = parse_csv("data/press.csv")
level_order = dict()
level_order['2010 Fall'] = 5
level_order['2011 Spring'] = 4
level_order['2011 Fall'] = 3
level_order['2012 Spring'] = 2
level_order['2012 Fall'] = 1
level_order['2013 Spring'] = 0
story_classes = dict()
story_classes[0] = []
story_classes[1] = []
story_classes[2] = []
story_classes[3] = []
story_classes[4] = []
story_classes[5] = []
for story in stories:
story_classes[level_order[story['competition']]].append(story)
build_template(env, 'press.html',
story_classes=story_classes,
)
def build_venue(env):
build_template(env, 'venue.html')
def main():
env = Environment(loader=FileSystemLoader(searchpath="./templates"))
build_index(env)
build_press(env)
build_about(env)
build_history(env)
build_venue(env)
build_rules(env)
build_faq(env)
build_sponsorship(env)
print "Templates built."
if __name__ == "__main__":
main()
if len(sys.argv) > 1 and sys.argv[1] == '--watch':
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class JinjaEventHandler(FileSystemEventHandler):
"""
Naive recompiler.
Rebuilds all templates if anything changes in /templates.
"""
def on_modified(self, event):
print "Recompiling templates..."
super(JinjaEventHandler, self).on_created(event)
if event.src_path.endswith("/templates"):
main()
# Start watching for any changes
event_handler = JinjaEventHandler()
observer = Observer()
observer.schedule(event_handler, path="./templates")
observer.start()
print "Watching ./templates for changes..."
print "Press Ctrl+C to stop."
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
print "Process killed"
observer.join()