1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
|
3
|
|
|
# -*- coding: utf-8 -*- |
4
|
|
|
|
5
|
|
|
# https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters |
6
|
|
|
# https://www.webforefront.com/django/usebuiltinjinjafilters.html |
7
|
|
|
|
8
|
1 |
|
import re |
9
|
1 |
|
import sys |
10
|
1 |
|
import os |
11
|
1 |
|
import csv |
12
|
|
|
|
13
|
1 |
|
filters = dict( |
14
|
|
|
|
15
|
|
|
format_dict = ( |
16
|
|
|
""" |
17
|
|
|
Given a dict as value, return a formatted string as specified in format. |
18
|
|
|
e.g. url | urlsplit | format_dict("{scheme}://{hostname}:8080/{path}/") |
19
|
|
|
""", |
20
|
|
|
lambda v, f: f.format(**v) |
21
|
|
|
), |
22
|
|
|
|
23
|
|
|
format_list = ( |
24
|
|
|
""" |
25
|
|
|
Given a list as value, return a formatted string as specified in format. |
26
|
|
|
e.g. url | urlsplit | format_list("{0}://{1}") |
27
|
|
|
""", |
28
|
|
|
lambda v, f: f.format(*v) |
29
|
|
|
), |
30
|
|
|
|
31
|
|
|
keys = ( |
32
|
|
|
""" |
33
|
|
|
Return the keys of a dict passed in |
34
|
|
|
""", |
35
|
|
|
lambda d: d.keys() |
36
|
|
|
), |
37
|
|
|
|
38
|
|
|
values = ( |
39
|
|
|
""" |
40
|
|
|
Return the values of a dict passed in |
41
|
|
|
""", |
42
|
|
|
lambda d: d.values() |
43
|
|
|
), |
44
|
|
|
|
45
|
|
|
items = ( """ |
46
|
|
|
Select items specified by indexes from the list passed in |
47
|
|
|
e.g. range(1,10) | items(0,2,5,-3,-2) |
48
|
|
|
""", |
49
|
|
|
lambda *n: list(n[0][x] for x in n[1:]) |
50
|
|
|
), |
51
|
|
|
|
52
|
|
|
wrap = ( """ |
53
|
|
|
Wrap value with "parantheses" specified in format (default "()") |
54
|
|
|
""", |
55
|
|
|
lambda v, t='()': '{}{}{}'.format(t[0], v, t[1]) |
56
|
|
|
), |
57
|
|
|
|
58
|
|
|
to_csv = ( """ |
59
|
|
|
Split a string delimited by commas and return items in a list |
60
|
|
|
""", |
61
|
|
|
lambda v: list(csv.reader(v))[0] # magic number is for single line of text |
62
|
|
|
), |
63
|
|
|
|
64
|
|
|
to_url = ( """ |
65
|
|
|
Take values from a dict passed in and |
66
|
|
|
return a string formatted in the form of a URL |
67
|
|
|
""", |
68
|
|
|
lambda v, f='https://{hostname}': |
69
|
|
|
f.format(**v) ), |
70
|
|
|
) |
71
|
|
|
|
72
|
1 |
|
if 'USE_ANSIBLE_SUPPORT' in os.environ.keys(): |
73
|
1 |
|
from .ansible import FilterModule |
74
|
1 |
|
filters.update(FilterModule().filters()) |
75
|
|
|
|
76
|
1 |
|
for k,v in filters.items(): |
77
|
|
|
setattr(sys.modules[__name__], k, v[1]) |
78
|
|
|
|