• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_expander.py
7
8import os
9import sys
10
11_current_dir = os.path.dirname(os.path.realpath(__file__))
12# jinja2 is in the third_party directory.
13# Insert at front to override system libraries, and after path[0] == script dir
14sys.path.insert(1, os.path.join(_current_dir,
15                                os.pardir,
16                                os.pardir,
17                                os.pardir,
18                                os.pardir,
19                                'third_party'))
20import jinja2
21
22
23def ApplyTemplate(path_to_template, params, filters=None):
24  template_directory, template_name = os.path.split(path_to_template)
25  path_to_templates = os.path.join(_current_dir, template_directory)
26  loader = jinja2.FileSystemLoader([path_to_templates])
27  jinja_env = jinja2.Environment(loader=loader, keep_trailing_newline=True)
28  if filters:
29    jinja_env.filters.update(filters)
30  template = jinja_env.get_template(template_name)
31  return template.render(params)
32
33
34def UseJinja(path_to_template, filters=None):
35  def RealDecorator(generator):
36    def GeneratorInternal(*args, **kwargs):
37      parameters = generator(*args, **kwargs)
38      return ApplyTemplate(path_to_template, parameters, filters=filters)
39    GeneratorInternal.func_name = generator.func_name
40    return GeneratorInternal
41  return RealDecorator
42