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
|
#!/usr/bin/python2
import yaml
import os
import jinja2
import optparse
from utils import filters
class Renderer:
def __init__(self, output_dir):
self.env = jinja2.Environment(
loader = jinja2.FileSystemLoader('./templates'))
self.env.filters.update(filters)
self.output_dir = output_dir
self.templates = {}
def _render(self, template, filename, **kwargs):
if template not in self.templates:
self.templates[template] = self.env.get_template(template)
with open(os.path.join(self.output_dir, filename), "w+") as output:
output.write(self.templates[template].render(**kwargs))
def host(self, host, hostdata):
print("Rendering: %s" % host)
hostdata['url'] = "%s.html" % host
self._render('host.html', hostdata['url'], **hostdata)
def index(self, data):
print("Rendering: index")
self._render('index.html', 'index.html', hosts=data)
def load_yaml(*args):
with open(os.path.join(*args)) as infile:
return yaml.load(infile)
def main():
basename = os.path.dirname(os.path.realpath(__file__))
optparser = optparse.OptionParser()
optparser.add_option(
"-o", "--output",
help="write rendered hostinfo files DEST_DIR",
metavar="DEST_DIR",
default=os.path.join(basename,"htdocs"))
optparser.add_option(
"-s", "--source",
help="read yaml encoded hostinfo data from SRC_DIR",
metavar="SRC_DIR",
default=os.path.join(basename,"data"))
(options, _ ) = optparser.parse_args()
renderer = Renderer(options.output)
hosts = load_yaml(options.source, "metadata", "hosts")
data = []
for host in sorted(hosts['hosts']):
hostdata = load_yaml(options.source, host)
renderer.host(host, hostdata)
data.append(hostdata)
renderer.index(data)
if __name__ == '__main__':
main()
|