-
Notifications
You must be signed in to change notification settings - Fork 11
/
build_site.py
52 lines (45 loc) · 1.35 KB
/
build_site.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
from jinja2 import Environment, FileSystemLoader
import os, sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import argparse
from config import *
env = Environment(loader=FileSystemLoader(TEMPLATES_FOLDER))
all_templates = os.listdir(TEMPLATES_FOLDER)
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
#self.write_template()
file_changed = os.path.basename(event.src_path)
if file_changed not in EXCLUDE_LIST:
print file_changed + " changed"
self.write_template(file_changed)
else:
# recuilt all files
for fn in all_templates:
if fn not in EXCLUDE_LIST: # don't need to rebuild excludes
print fn + " changed"
self.write_template(fn)
# write parsed template to current folder
def write_template(self, fn):
template = env.get_template(fn)
parsed_template = template.render()
with open(fn, 'wb') as fh:
fh.write(parsed_template)
fh.close()
def print_hi():
print "hi"
if __name__ == "__main__":
print "monitoring files for changes"
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='templates', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
# templates = ['index.html', 'lecture1.html']
# map(write_template, templates)