-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrender.py
58 lines (49 loc) · 1.72 KB
/
render.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
# -*- coding: utf-8 -*-
import jinja2
import os
from time import gmtime, strftime
import glob
output_dir = "output_filters/"
template_dir = "templates/"
filter_paths = [
{"path": "filters/", "template": "template.j2", "output": "subscription.txt"},
{
"path": "filters_annoyance/",
"template": "template_annoyance.j2",
"output": "annoyance.txt",
},
{
"path": "filters_test/",
"template": "template_test.j2",
"output": "test_filters.txt",
},
]
def render(tpl_path, context):
path, filename = os.path.split(tpl_path)
loader = jinja2.ChoiceLoader([jinja2.FileSystemLoader([template_dir, "."]),])
env = jinja2.Environment(loader=loader).get_template(filename)
env.globals["version"] = strftime("%Y%m%d%H%M", gmtime())
env.globals["timestamp"] = strftime("%d %b %Y %H:%M UTC", gmtime())
return env.render(context)
def write_file(rendered, path):
with open(os.path.join(path), "w", encoding="utf-8") as file:
print("render:", path)
file.write(rendered)
def main():
os.makedirs(output_dir, exist_ok=True)
for filter_path in filter_paths:
filter_files = [
f.replace("\\", "/")
for f in glob.glob(filter_path["path"] + "**/*.txt", recursive=True)
if "whitelist" not in f
]
whitelist_files = [
f.replace("\\", "/")
for f in glob.glob(filter_path["path"] + "**/*.txt", recursive=True)
if "whitelist" in f
]
file_list = filter_files + whitelist_files
rendered = render(filter_path["template"], {"file_list": file_list})
write_file(rendered, output_dir + filter_path["output"])
if __name__ == "__main__":
main()