OLD | NEW |
| (Empty) |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 # Based on: | |
6 # http://src.chromium.org/viewvc/blink/trunk/Source/build/scripts/template_expan
der.py | |
7 | |
8 import imp | |
9 import inspect | |
10 import os.path | |
11 import sys | |
12 | |
13 # Disable lint check for finding modules: | |
14 # pylint: disable=F0401 | |
15 | |
16 try: | |
17 imp.find_module("jinja2") | |
18 except ImportError: | |
19 # Assume this file is in tools/pylib/bindings/mojom/generate. | |
20 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), | |
21 os.pardir, os.pardir, os.pardir, os.pardir, | |
22 os.pardir, "third_party")) | |
23 import jinja2 | |
24 | |
25 | |
26 def ApplyTemplate(mojo_generator, base_dir, path_to_template, params, | |
27 filters=None, **kwargs): | |
28 template_directory, template_name = os.path.split(path_to_template) | |
29 path_to_templates = os.path.join(base_dir, template_directory) | |
30 loader = jinja2.FileSystemLoader([path_to_templates]) | |
31 final_kwargs = dict(mojo_generator.GetJinjaParameters()) | |
32 final_kwargs.update(kwargs) | |
33 jinja_env = jinja2.Environment(loader=loader, keep_trailing_newline=True, | |
34 **final_kwargs) | |
35 jinja_env.globals.update(mojo_generator.GetGlobals()) | |
36 if filters: | |
37 jinja_env.filters.update(filters) | |
38 template = jinja_env.get_template(template_name) | |
39 return template.render(params) | |
40 | |
41 | |
42 def UseJinja(path_to_template, **kwargs): | |
43 # Get the directory of our caller's file. | |
44 base_dir = os.path.dirname(inspect.getfile(sys._getframe(1))) | |
45 def RealDecorator(generator): | |
46 def GeneratorInternal(*args, **kwargs2): | |
47 parameters = generator(*args, **kwargs2) | |
48 return ApplyTemplate(args[0], base_dir, path_to_template, parameters, | |
49 **kwargs) | |
50 GeneratorInternal.func_name = generator.func_name | |
51 return GeneratorInternal | |
52 return RealDecorator | |
OLD | NEW |