blob: f0dc818c0c0e6fcd79bc8405412634e1e32f55bf [file] [log] [blame]
David Klempnerf94838b2015-02-02 16:56:46 -08001#!/usr/bin/python2.7
Craig Tillerc2c79212015-02-16 12:00:01 -08002# Copyright 2015, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Nicolas Nobleddef2462015-01-06 18:08:25 -080031
32"""Simple Mako renderer.
33
34Just a wrapper around the mako rendering library.
35
36"""
37
38import getopt
39import imp
40import os
41import sys
42
43
44from mako.lookup import TemplateLookup
45from mako.runtime import Context
46from mako.template import Template
47import simplejson
48import bunch
49
50
51# Imports a plugin
52def import_plugin(name):
53 _, base_ex = os.path.split(name)
54 base, _ = os.path.splitext(base_ex)
55
56 with open(name, 'r') as plugin_file:
57 plugin_code = plugin_file.read()
58 plugin_module = imp.new_module(base)
59 exec plugin_code in plugin_module.__dict__
60 return plugin_module
61
62
63def out(msg):
64 print >> sys.stderr, msg
65
66
67def showhelp():
68 out('mako-renderer.py [-o out] [-m cache] [-d dict] [-d dict...] template')
69
70
71def main(argv):
72 got_input = False
73 module_directory = None
74 dictionary = {}
75 json_dict = {}
76 got_output = False
77 output_file = sys.stdout
78 plugins = []
79
80 try:
81 opts, args = getopt.getopt(argv, 'hm:d:o:p:')
82 except getopt.GetoptError:
83 out('Unknown option')
84 showhelp()
85 sys.exit(2)
86
87 for opt, arg in opts:
88 if opt == '-h':
89 out('Displaying showhelp')
90 showhelp()
91 sys.exit()
92 elif opt == '-o':
93 if got_output:
94 out('Got more than one output')
95 showhelp()
96 sys.exit(3)
97 got_output = True
98 output_file = open(arg, 'w')
99 elif opt == '-m':
100 if module_directory is not None:
101 out('Got more than one cache directory')
102 showhelp()
103 sys.exit(4)
104 module_directory = arg
105 elif opt == '-d':
106 dict_file = open(arg, 'r')
107 bunch.merge_json(json_dict, simplejson.loads(dict_file.read()))
108 dict_file.close()
109 elif opt == '-p':
110 plugins.append(import_plugin(arg))
111
112 for plugin in plugins:
113 plugin.mako_plugin(json_dict)
114
115 for k, v in json_dict.items():
116 dictionary[k] = bunch.to_bunch(v)
117
118 ctx = Context(output_file, **dictionary)
119
120 for arg in args:
121 got_input = True
122 template = Template(filename=arg,
123 module_directory=module_directory,
124 lookup=TemplateLookup(directories=['.']))
125 template.render_context(ctx)
126
127 if not got_input:
128 out('Got nothing to do')
129 showhelp()
130
131 output_file.close()
132
133if __name__ == '__main__':
134 main(sys.argv[1:])