-
Notifications
You must be signed in to change notification settings - Fork 0
/
j2f5.py
119 lines (93 loc) · 3.66 KB
/
j2f5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*-
import sys
from os import path
from argparse import ArgumentError
from j2cli import main as j2cli_main
"""
support reading templating start/end strings from the user supplied configuration
alter_context is called before j2_environment_params:
1. Read and save context (based on user supplied configuration) in alter_context
2. Read start/end strings from context in j2_environment_params
https://github.com/kolypto/j2cli/blob/26a67e9419d96b7f92871e8b93dba00306c5df0b/j2cli/cli.py#L183-L186
"""
_context = dict()
# constants
_TEMPLATING_NAMESPACE = "templating_settings"
def ansible_filters():
_ansible_filters = dict()
try:
from ansible.plugins.filter import core as ansible_core_filters
from ansible.plugins.filter import urls as ansible_urls_filters
from ansible.plugins.filter import urlsplit as ansible_urlsplit_filters
from ansible.plugins.filter import mathstuff as ansible_mathstuff_filters
_ansible_filters.update(ansible_core_filters.FilterModule().filters())
_ansible_filters.update(ansible_urls_filters.FilterModule().filters())
_ansible_filters.update(ansible_urlsplit_filters.FilterModule().filters())
_ansible_filters.update(ansible_mathstuff_filters.FilterModule().filters())
except ImportError:
pass
return _ansible_filters
def alter_context(context):
"""modify jinja2 context"""
# register context in _context for later use in j2_environment_params
global _context
_context = context
# start other modifications below
return context
def extra_filters():
"""extra jinja2 filters"""
_extra_filters = dict()
# load ansible filters when ansible is available
_extra_filters.update(ansible_filters())
return _extra_filters
def j2_environment_params():
"""jinja2 environment parameters"""
custom_params = dict(
# use this dict to further customize or overwrite params
)
# read templating parameters from _context
templating_params = dict(
# Custom block start/end strings
block_start_string=_context.get(_TEMPLATING_NAMESPACE, {}).get(
"block_start_string"
),
block_end_string=_context.get(_TEMPLATING_NAMESPACE, {}).get(
"block_end_string"
),
# Custom variable start/end strings
variable_start_string=_context.get(_TEMPLATING_NAMESPACE, {}).get(
"variable_start_string"
),
variable_end_string=_context.get(_TEMPLATING_NAMESPACE, {}).get(
"variable_end_string"
),
# Custom comment start/end strings
comment_start_string=_context.get(_TEMPLATING_NAMESPACE, {}).get(
"comment_start_string"
),
comment_end_string=_context.get(_TEMPLATING_NAMESPACE, {}).get(
"comment_end_string"
),
# Remove whitespace around blocks
trim_blocks=True,
lstrip_blocks=True,
# Keep \n at the end of a file
keep_trailing_newline=True,
)
# remove None's
templating_params = {k: v for (k, v) in templating_params.items() if v is not None}
templating_params.update(custom_params)
return templating_params
def main():
"""wrap j2 command line and automatically add j2f5.py via --customize"""
if "--customize" in sys.argv:
raise ArgumentError(
None,
"j2f5 sets --customize, hence it is not allowed as an argument. Use j2 directly to use your own customization.",
)
# inject args which will be read by j2cli main()
sys.argv.insert(1, "--customize")
sys.argv.insert(2, path.abspath(__file__))
j2cli_main()
if __name__ == "__main__":
main()