# Copyright 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # Based on third_party/WebKit/Source/build/scripts/template_expander.py. import os.path import sys _current_dir = os.path.dirname(os.path.realpath(__file__)) # jinja2 is in chromium's third_party directory # Insert at front to override system libraries, and after path[0] == script dir sys.path.insert( 1, os.path.join(_current_dir, *([os.pardir] * 7 + ['third_party']))) import jinja2 def ApplyTemplate(mojo_generator, path_to_template, params, **kwargs): loader = jinja2.ModuleLoader(os.path.join( mojo_generator.bytecode_path, "%s.zip" % mojo_generator.GetTemplatePrefix( ))) final_kwargs = dict(mojo_generator.GetJinjaParameters()) final_kwargs.update(kwargs) jinja_env = jinja2.Environment(loader=loader, keep_trailing_newline=True, **final_kwargs) jinja_env.globals.update(mojo_generator.GetGlobals()) jinja_env.filters.update(mojo_generator.GetFilters()) template = jinja_env.get_template(path_to_template) return template.render(params) def UseJinja(path_to_template, **kwargs): def RealDecorator(generator): def GeneratorInternal(*args, **kwargs2): parameters = generator(*args, **kwargs2) return ApplyTemplate(args[0], path_to_template, parameters, **kwargs) GeneratorInternal.func_name = generator.func_name return GeneratorInternal return RealDecorator def PrecompileTemplates(generator_modules, output_dir): for module in generator_modules.values(): generator = module.Generator(None) jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader([os.path.join( os.path.dirname(module.__file__), generator.GetTemplatePrefix())])) jinja_env.filters.update(generator.GetFilters()) jinja_env.compile_templates( os.path.join(output_dir, "%s.zip" % generator.GetTemplatePrefix()), extensions=["tmpl"], zip="stored", py_compile=True, ignore_errors=False)