1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
"""Jinja2 environment and extensions loading.""" |
3
|
|
|
|
4
|
|
|
from jinja2 import Environment, StrictUndefined |
5
|
|
|
from .exceptions import UnknownExtension |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class ExtensionLoaderMixin(object): |
9
|
|
|
"""Mixin providing sane loading of extensions specified in a given context. |
10
|
|
|
|
11
|
|
|
The context is being extracted from the keyword arguments before calling |
12
|
|
|
the next parent class in line of the child. |
13
|
|
|
""" |
14
|
|
|
|
15
|
|
|
def __init__(self, **kwargs): |
16
|
|
|
"""Initialize the Jinja2 Environment object while loading extensions. |
17
|
|
|
|
18
|
|
|
Does the following: |
19
|
|
|
|
20
|
|
|
1. Establishes default_extensions (currently just a Time feature) |
21
|
|
|
2. Reads extensions set in the cookiecutter.json _extensions key. |
22
|
|
|
3. Attempts to load the extensions. Provides useful error if fails. |
23
|
|
|
""" |
24
|
|
|
context = kwargs.pop('context', {}) |
25
|
|
|
|
26
|
|
|
default_extensions = [ |
27
|
|
|
'jinja2_time.TimeExtension', |
28
|
|
|
'cookiecutter.extensions.JsonifyExtension' |
29
|
|
|
] |
30
|
|
|
extensions = default_extensions + self._read_extensions(context) |
31
|
|
|
|
32
|
|
|
try: |
33
|
|
|
super(ExtensionLoaderMixin, self).__init__( |
34
|
|
|
extensions=extensions, |
35
|
|
|
**kwargs |
36
|
|
|
) |
37
|
|
|
except ImportError as err: |
38
|
|
|
raise UnknownExtension('Unable to load extension: {}'.format(err)) |
39
|
|
|
|
40
|
|
|
def _read_extensions(self, context): |
41
|
|
|
"""Return list of extensions as str to be passed on to the Jinja2 env. |
42
|
|
|
|
43
|
|
|
If context does not contain the relevant info, return an empty |
44
|
|
|
list instead. |
45
|
|
|
""" |
46
|
|
|
try: |
47
|
|
|
extensions = context['cookiecutter']['_extensions'] |
48
|
|
|
except KeyError: |
49
|
|
|
return [] |
50
|
|
|
else: |
51
|
|
|
return [str(ext) for ext in extensions] |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
class StrictEnvironment(ExtensionLoaderMixin, Environment): |
55
|
|
|
"""Create strict Jinja2 environment. |
56
|
|
|
|
57
|
|
|
Jinja2 environment will raise error on undefined variable in template- |
58
|
|
|
rendering context. |
59
|
|
|
""" |
60
|
|
|
|
61
|
|
|
def __init__(self, **kwargs): |
62
|
|
|
"""Set the standard Cookiecutter StrictEnvironment. |
63
|
|
|
|
64
|
|
|
Also loading extensions defined in cookiecutter.json's _extensions key. |
65
|
|
|
""" |
66
|
|
|
super(StrictEnvironment, self).__init__( |
67
|
|
|
undefined=StrictUndefined, |
68
|
|
|
**kwargs |
69
|
|
|
) |
70
|
|
|
|