| 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 inspect | |
| 9 import os | |
| 10 import sys | |
| 11 | |
| 12 _current_dir = os.path.dirname(os.path.realpath(__file__)) | |
| 13 # jinja2 is in the third_party directory (current directory is | |
| 14 # mojo/public/bindings/pylib/generate). | |
| 15 # Insert at front to override system libraries, and after path[0] == script dir | |
| 16 sys.path.insert(1, os.path.join(_current_dir, | |
| 17 os.pardir, | |
| 18 os.pardir, | |
| 19 os.pardir, | |
| 20 os.pardir, | |
| 21 os.pardir, | |
| 22 'third_party')) | |
| 23 import jinja2 | |
| 24 | |
| 25 | |
| 26 def ApplyTemplate(base_dir, path_to_template, params, filters=None): | |
| 27 template_directory, template_name = os.path.split(path_to_template) | |
| 28 path_to_templates = os.path.join(base_dir, template_directory) | |
| 29 loader = jinja2.FileSystemLoader([path_to_templates]) | |
| 30 jinja_env = jinja2.Environment(loader=loader, keep_trailing_newline=True) | |
| 31 if filters: | |
| 32 jinja_env.filters.update(filters) | |
| 33 template = jinja_env.get_template(template_name) | |
| 34 return template.render(params) | |
| 35 | |
| 36 | |
| 37 def UseJinja(path_to_template, filters=None): | |
| 38 # Get the directory of our caller's file. | |
| 39 base_dir = os.path.dirname(inspect.getfile(sys._getframe(1))) | |
| 40 def RealDecorator(generator): | |
| 41 def GeneratorInternal(*args, **kwargs): | |
| 42 parameters = generator(*args, **kwargs) | |
| 43 return ApplyTemplate(base_dir, path_to_template, parameters, | |
| 44 filters=filters) | |
| 45 GeneratorInternal.func_name = generator.func_name | |
| 46 return GeneratorInternal | |
| 47 return RealDecorator | |
| OLD | NEW |