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
|
#!/usr/bin/env python3
"""
fill.py - fill data to templates
Originally designed for use in generating https://fcm.andrewyu.org,
expands {{name}} in templates to the value defined in data under
-name-.
Python 3.8+ required!
Copyright (C) 2022 Andrew Yu <andrew@andrewyu.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from sys import argv, stderr
import re
def log(text):
print(text, file=stderr, end="")
if len(argv) < 3:
log(f"[-] not enough arguments\n[H] usage: {argv[0]} data template\n")
exit(1)
data_filename = argv[1]
template_filename = argv[2]
log(f"[*] filling out {template_filename} against {data_filename}...")
with open(data_filename, 'r') as data_file:
data_text = data_file.read()
with open(template_filename, 'r') as template_file:
while (template_line := template_file.readline()) != '': # until EOF
search_result = re.search("{{.*}}", template_line) # insertion point
if search_result is None:
print(template_line, end="")
continue
search_result_span = search_result.span()
variable_placer = template_line[search_result_span[0]:search_result_span[1]]
variable_name = variable_placer[2:-2]
variable_name_length = len(variable_name)
variable_result = re.search(r"\\begin variable_name.*\\end variable_name".replace("variable_name", variable_name), data_text, flags=re.S)
if variable_result is None:
log(f" error\n[-] data file {data_filename} does not define {variable_name} as required by {template_filename}\n")
exit(2)
variable_result_span = variable_result.span()
variable_data = data_text[variable_result_span[0]:variable_result_span[1]][8+variable_name_length:-6-variable_name_length] # 8 and 6 are legnth of "\\begin \n" and "\\end \n"
print(template_line.replace(variable_placer, variable_data), end="")
template_file.close()
data_file.close()
log(f" done\n")
|