1
|
1 |
|
from __future__ import absolute_import |
|
|
|
|
2
|
1 |
|
from __future__ import print_function |
3
|
|
|
|
4
|
1 |
|
import os.path |
5
|
1 |
|
import jinja2 |
6
|
|
|
|
7
|
1 |
|
from .utils import required_key |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
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
|
1 |
|
def __init__(self, encoding='utf-8'): |
21
|
1 |
|
self.encoding = encoding |
22
|
|
|
|
23
|
1 |
|
def get_source(self, environment, template): |
24
|
1 |
|
if not os.path.isabs(template): |
25
|
|
|
raise jinja2.TemplateNotFound(template) |
26
|
|
|
|
27
|
1 |
|
template_file = jinja2.utils.open_if_exists(template) |
28
|
1 |
|
if template_file is None: |
29
|
|
|
raise jinja2.TemplateNotFound(template) |
30
|
1 |
|
try: |
31
|
1 |
|
contents = template_file.read().decode(self.encoding) |
32
|
|
|
finally: |
33
|
1 |
|
template_file.close() |
34
|
|
|
|
35
|
1 |
|
mtime = os.path.getmtime(template) |
36
|
|
|
|
37
|
1 |
|
def uptodate(): |
|
|
|
|
38
|
1 |
|
try: |
39
|
1 |
|
return os.path.getmtime(template) == mtime |
40
|
|
|
except OSError: |
41
|
|
|
return False |
42
|
1 |
|
return contents, template, uptodate |
43
|
|
|
|
44
|
|
|
|
45
|
1 |
|
def _get_jinja_environment(substitutions_dict): |
46
|
1 |
|
if _get_jinja_environment.env is None: |
47
|
1 |
|
bytecode_cache = None |
48
|
1 |
|
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
|
1 |
|
_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
|
1 |
|
return _get_jinja_environment.env |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
_get_jinja_environment.env = None |
69
|
|
|
|
70
|
|
|
|
71
|
1 |
|
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
|
1 |
|
template = _get_jinja_environment(substitutions_dict).get_template(filename) |
78
|
1 |
|
all_symbols = template.make_module(substitutions_dict).__dict__ |
79
|
1 |
|
symbols_to_export = dict() |
80
|
1 |
|
for name, symbol in all_symbols.items(): |
81
|
1 |
|
if name.startswith("_"): |
82
|
1 |
|
continue |
83
|
1 |
|
symbols_to_export[name] = symbol |
84
|
1 |
|
return symbols_to_export |
85
|
|
|
|
86
|
|
|
|
87
|
1 |
|
def process_file(filepath, substitutions_dict): |
|
|
|
|
88
|
1 |
|
filepath = os.path.abspath(filepath) |
89
|
1 |
|
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.