from __future__ import absolute_import import collections import datetime import os import shutil from mako.template import Template from mako.lookup import TemplateLookup class Renderer(object): """Class to render results into html. """ def __init__(self, result): self.result = result def run(self): # make a directory to contain this result now = datetime.datetime.today() dirname = 'fisica-%s' % (now.strftime('%Y%m%dT%H%M%S')) os.mkdir(dirname) # make a resources directory and copy the Bootstrap stuff there shutil.copytree('dist', os.path.join(dirname, 'resources')) # copy the css file there too shutil.copy('bootstrap-3.0.3/examples/navbar-fixed-top/navbar-fixed-top.css', dirname) # where to find the template files mylookup = TemplateLookup(directories=['templates']) # render the simulation steps first for stage in self.result.items(): if 'substages' in stage[1].keys(): for substage in stage[1]['substages'].items(): print 'rendering', stage[0], substage[0] context = {'dirname':dirname, 'renderstage':stage[0], 'rendersubstage':substage[0], 'data':self.result} link = os.path.join(context['dirname'], '%s-%s.html' % (stage[0], substage[0])) with open(link, 'w') as f: template = mylookup.get_template('%s-%s.html' % (stage[0], substage[0])) f.write(template.render(**context)) else: # no substages print 'rendering', stage[0] context = {'mylookup':mylookup, 'dirname':dirname, 'renderstage':stage[0], 'data':self.result} link = os.path.join(context['dirname'], '%s.html' % (stage[0])) with open(link, 'w') as f: template = mylookup.get_template('%s.html' % (stage[0])) f.write(template.render(**context))