|
1
|
2 |
|
from __future__ import absolute_import |
|
|
|
|
|
|
2
|
2 |
|
from __future__ import print_function |
|
3
|
|
|
|
|
4
|
2 |
|
import os.path |
|
5
|
2 |
|
import jinja2 |
|
6
|
|
|
|
|
7
|
2 |
|
from .utils import required_key |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
2 |
|
class AbsolutePathFileSystemLoader(jinja2.BaseLoader): |
|
|
|
|
|
|
11
|
|
|
"""Loads templates from the file system. This loader insists on absolute |
|
12
|
|
|
paths and fails if a relative path is provided. |
|
13
|
|
|
|
|
14
|
|
|
>>> loader = AbsolutePathFileSystemLoader() |
|
15
|
|
|
|
|
16
|
|
|
Per default the template encoding is ``'utf-8'`` which can be changed |
|
17
|
|
|
by setting the `encoding` parameter to something else. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
2 |
|
def __init__(self, encoding='utf-8'): |
|
21
|
2 |
|
self.encoding = encoding |
|
22
|
|
|
|
|
23
|
2 |
|
def get_source(self, environment, template): |
|
24
|
2 |
|
if not os.path.isabs(template): |
|
25
|
|
|
raise jinja2.TemplateNotFound(template) |
|
26
|
|
|
|
|
27
|
2 |
|
template_file = jinja2.utils.open_if_exists(template) |
|
28
|
2 |
|
if template_file is None: |
|
29
|
|
|
raise jinja2.TemplateNotFound(template) |
|
30
|
2 |
|
try: |
|
31
|
2 |
|
contents = template_file.read().decode(self.encoding) |
|
32
|
|
|
finally: |
|
33
|
2 |
|
template_file.close() |
|
34
|
|
|
|
|
35
|
2 |
|
mtime = os.path.getmtime(template) |
|
36
|
|
|
|
|
37
|
2 |
|
def uptodate(): |
|
|
|
|
|
|
38
|
2 |
|
try: |
|
39
|
2 |
|
return os.path.getmtime(template) == mtime |
|
40
|
|
|
except OSError: |
|
41
|
|
|
return False |
|
42
|
2 |
|
return contents, template, uptodate |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
2 |
|
def _get_jinja_environment(substitutions_dict): |
|
46
|
2 |
|
if _get_jinja_environment.env is None: |
|
47
|
2 |
|
bytecode_cache = None |
|
48
|
2 |
|
if substitutions_dict.get("jinja2_cache_enabled") == "true": |
|
49
|
|
|
bytecode_cache = jinja2.FileSystemBytecodeCache( |
|
50
|
|
|
required_key(substitutions_dict, "jinja2_cache_dir") |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
|
|
# TODO: Choose better syntax? |
|
|
|
|
|
|
54
|
2 |
|
_get_jinja_environment.env = jinja2.Environment( |
|
55
|
|
|
block_start_string="{{%", |
|
56
|
|
|
block_end_string="%}}", |
|
57
|
|
|
variable_start_string="{{{", |
|
58
|
|
|
variable_end_string="}}}", |
|
59
|
|
|
comment_start_string="{{#", |
|
60
|
|
|
comment_end_string="#}}", |
|
61
|
|
|
loader=AbsolutePathFileSystemLoader(), |
|
62
|
|
|
bytecode_cache=bytecode_cache |
|
63
|
|
|
) |
|
64
|
|
|
|
|
65
|
2 |
|
return _get_jinja_environment.env |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
2 |
|
_get_jinja_environment.env = None |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
2 |
|
def extract_substitutions_dict_from_template(filename, substitutions_dict): |
|
|
|
|
|
|
72
|
|
|
""" |
|
73
|
|
|
Treat the given filename as a jinja2 file containing macro definitions, |
|
74
|
|
|
and export definitions that don't start with _ as a name->macro dictionary. |
|
75
|
|
|
During macro compilation, symbols from substitutions_dict may be used in those definitions. |
|
76
|
|
|
""" |
|
77
|
2 |
|
template = _get_jinja_environment(substitutions_dict).get_template(filename) |
|
78
|
2 |
|
all_symbols = template.make_module(substitutions_dict).__dict__ |
|
79
|
2 |
|
symbols_to_export = dict() |
|
80
|
2 |
|
for name, symbol in all_symbols.items(): |
|
81
|
2 |
|
if name.startswith("_"): |
|
82
|
2 |
|
continue |
|
83
|
2 |
|
symbols_to_export[name] = symbol |
|
84
|
2 |
|
return symbols_to_export |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
2 |
|
def process_file(filepath, substitutions_dict): |
|
|
|
|
|
|
88
|
2 |
|
filepath = os.path.abspath(filepath) |
|
89
|
2 |
|
template = _get_jinja_environment(substitutions_dict).get_template(filepath) |
|
90
|
|
|
return template.render(substitutions_dict) |
|
91
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.