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