OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 18 matching lines...) Expand all Loading... |
29 import os | 29 import os |
30 import sys | 30 import sys |
31 | 31 |
32 _current_dir = os.path.dirname(os.path.realpath(__file__)) | 32 _current_dir = os.path.dirname(os.path.realpath(__file__)) |
33 # jinja2 is in chromium's third_party directory | 33 # jinja2 is in chromium's third_party directory |
34 # Insert at front to override system libraries, and after path[0] == script dir | 34 # Insert at front to override system libraries, and after path[0] == script dir |
35 sys.path.insert(1, os.path.join(_current_dir, *([os.pardir] * 4))) | 35 sys.path.insert(1, os.path.join(_current_dir, *([os.pardir] * 4))) |
36 import jinja2 | 36 import jinja2 |
37 | 37 |
38 | 38 |
39 def apply_template(path_to_template, params, filters=None): | 39 def apply_template(path_to_template, params, filters=None, tests=None): |
40 dirname, basename = os.path.split(path_to_template) | 40 dirname, basename = os.path.split(path_to_template) |
41 path_to_templates = os.path.join(_current_dir, 'templates') | 41 path_to_templates = os.path.join(_current_dir, 'templates') |
42 jinja_env = jinja2.Environment( | 42 jinja_env = jinja2.Environment( |
43 loader=jinja2.FileSystemLoader([dirname, path_to_templates]), | 43 loader=jinja2.FileSystemLoader([dirname, path_to_templates]), |
44 keep_trailing_newline=True, # newline-terminate generated files | 44 keep_trailing_newline=True, # newline-terminate generated files |
45 lstrip_blocks=True, # so can indent control flow tags | 45 lstrip_blocks=True, # so can indent control flow tags |
46 trim_blocks=True) # so don't need {%- -%} everywhere | 46 trim_blocks=True) # so don't need {%- -%} everywhere |
47 if filters: | 47 if filters: |
48 jinja_env.filters.update(filters) | 48 jinja_env.filters.update(filters) |
| 49 if tests: |
| 50 jinja_env.tests.update(tests) |
49 template = jinja_env.get_template(basename) | 51 template = jinja_env.get_template(basename) |
50 return template.render(params) | 52 return template.render(params) |
51 | 53 |
52 | 54 |
53 def use_jinja(template_file_name, filters=None): | 55 def use_jinja(template_file_name, filters=None, tests=None): |
54 def real_decorator(generator): | 56 def real_decorator(generator): |
55 def generator_internal(*args, **kwargs): | 57 def generator_internal(*args, **kwargs): |
56 parameters = generator(*args, **kwargs) | 58 parameters = generator(*args, **kwargs) |
57 return apply_template(template_file_name, parameters, filters=filter
s) | 59 return apply_template(template_file_name, parameters, filters=filter
s, tests=tests) |
58 generator_internal.func_name = generator.func_name | 60 generator_internal.func_name = generator.func_name |
59 return generator_internal | 61 return generator_internal |
60 return real_decorator | 62 return real_decorator |
OLD | NEW |