forked from handofkwll/fisica
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.py
More file actions
85 lines (65 loc) · 2.88 KB
/
Copy pathrenderer.py
File metadata and controls
85 lines (65 loc) · 2.88 KB
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
from __future__ import absolute_import
import collections
import os
import shutil
from extern import mako
from mako.template import Template
from mako.lookup import TemplateLookup
import templates
import templates.resources as resources
class Renderer(object):
"""Class to render results into html.
"""
def __init__(self, result):
self.result = result
def copy_resources(self, dirname):
outdir = os.path.join(dirname, 'resources')
# shutil.copytree complains if the output directory exists
if os.path.exists(outdir):
shutil.rmtree(outdir)
# copy all uncompressed non-python resources to output directory
src = os.path.dirname(resources.__file__)
dst = outdir
ignore_fn = shutil.ignore_patterns('*.zip','*.py','*.pyc','CVS*')
shutil.copytree(src,
dst,
symlinks=False,
ignore=ignore_fn)
# # unzip fancybox to output directory
# infile = os.path.join(src, 'fancybox.zip')
# z = zipfile.ZipFile(infile, 'r')
# z.extractall(outdir)
def run(self):
# make a directory to contain this result
runtime = self.result['runtime']
dirname = 'fisica-%s' % runtime
os.mkdir(dirname)
# make a resources directory and copy the Bootstrap stuff there
self.copy_resources(dirname)
# where to find the template files
templates_dir = os.path.dirname(templates.__file__)
mylookup = TemplateLookup(directories=[templates_dir])
# render the simulation steps first
for stage in self.result.items():
if stage[0] in ['runtime']:
# not a proper stage, do not render
pass
elif isinstance(stage[1], dict) and '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))