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