2015-02-26 20:26:39 +01:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
|
|
|
|
import os
|
2016-01-21 22:24:32 +01:00
|
|
|
import sys
|
|
|
|
import SocketServer
|
2016-04-22 22:29:08 +02:00
|
|
|
import inotify.adapters
|
2016-01-21 22:24:32 +01:00
|
|
|
|
2015-02-26 20:26:39 +01:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2016-01-21 22:24:32 +01:00
|
|
|
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
2016-04-22 22:29:08 +02:00
|
|
|
from multiprocessing import Process
|
2016-01-21 22:24:32 +01:00
|
|
|
|
2016-04-22 22:29:08 +02:00
|
|
|
from constants import CONTENT_DIRECTORY, OUTPUT_DIRECTORY, TEMPLATE_DIRECTORY, SERVE_PORT, SERVE_BIND_ADDRESS
|
2015-02-26 23:56:19 +01:00
|
|
|
|
2015-02-26 20:26:39 +01:00
|
|
|
class Verdandi(object):
|
|
|
|
|
2015-02-26 23:56:19 +01:00
|
|
|
output_directory = OUTPUT_DIRECTORY
|
2015-02-26 20:26:39 +01:00
|
|
|
modules = []
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-04-22 22:29:08 +02:00
|
|
|
self.jinja_env = Environment(loader=FileSystemLoader(TEMPLATE_DIRECTORY))
|
2015-02-26 20:26:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
def send_message(self, message):
|
|
|
|
results = []
|
|
|
|
|
|
|
|
for module in self.modules:
|
|
|
|
results += module.process_message(message)
|
2015-02-26 23:56:19 +01:00
|
|
|
|
2015-02-26 20:26:39 +01:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
2015-02-26 23:56:19 +01:00
|
|
|
def pass_messages(self):
|
2015-02-26 20:26:39 +01:00
|
|
|
messages = self.send_message(None)
|
|
|
|
|
|
|
|
while len(messages) > 0:
|
|
|
|
message = messages.pop()
|
|
|
|
messages = messages + self.send_message(message)
|
2015-02-26 23:56:19 +01:00
|
|
|
|
|
|
|
|
2016-01-21 22:24:32 +01:00
|
|
|
def collect_assets(self):
|
2015-10-17 17:57:26 +02:00
|
|
|
for module in self.modules:
|
|
|
|
module.collect_assets(self.output_directory)
|
|
|
|
|
|
|
|
|
2015-02-26 20:26:39 +01:00
|
|
|
def render(self):
|
|
|
|
if not os.path.exists(self.output_directory):
|
2015-02-26 23:56:19 +01:00
|
|
|
os.mkdir(self.output_directory)
|
2015-02-26 20:26:39 +01:00
|
|
|
for module in self.modules:
|
|
|
|
module.render(self.output_directory, self.jinja_env)
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
2016-04-22 22:29:08 +02:00
|
|
|
self.generate_output()
|
|
|
|
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'serve':
|
|
|
|
self.serve()
|
|
|
|
|
|
|
|
|
|
|
|
def generate_output(self):
|
2015-02-26 20:26:39 +01:00
|
|
|
self.pass_messages()
|
2015-10-17 17:57:26 +02:00
|
|
|
self.collect_assets()
|
2015-02-26 20:26:39 +01:00
|
|
|
self.render()
|
2016-01-21 22:24:32 +01:00
|
|
|
|
2016-04-22 22:29:08 +02:00
|
|
|
|
|
|
|
def serve(self):
|
|
|
|
|
|
|
|
def has_create_or_modify(watch):
|
|
|
|
result = False
|
|
|
|
for event in watch.event_gen():
|
|
|
|
if event == None:
|
|
|
|
break
|
|
|
|
|
|
|
|
result = result or ('IN_CREATE' in event[1])
|
|
|
|
result = result or ('IN_DELETE' in event[1])
|
|
|
|
result = result or ('IN_MODIFY' in event[1])
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
content_watch = inotify.adapters.InotifyTree(CONTENT_DIRECTORY)
|
|
|
|
template_watch = inotify.adapters.InotifyTree(TEMPLATE_DIRECTORY)
|
|
|
|
|
|
|
|
|
|
|
|
def serve():
|
2016-01-21 22:24:32 +01:00
|
|
|
os.chdir(self.output_directory)
|
2016-01-25 16:13:00 +01:00
|
|
|
httpd = SocketServer.TCPServer((SERVE_BIND_ADDRESS, SERVE_PORT), SimpleHTTPRequestHandler)
|
|
|
|
print "Serving under %s:%d" % (SERVE_BIND_ADDRESS, SERVE_PORT)
|
2016-01-21 22:24:32 +01:00
|
|
|
httpd.serve_forever()
|
2016-04-22 22:29:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
server_process = Process(target=serve)
|
|
|
|
server_process.start()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
if has_create_or_modify(content_watch) or has_create_or_modify(template_watch):
|
|
|
|
self.generate_output()
|