-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplate.py
131 lines (106 loc) · 3.62 KB
/
Template.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
120
121
122
123
124
125
126
127
128
129
130
131
"""
UEBuild Tools - Version Information Updater for Unreal Engine
Copyright (C) 2024 IT-Hock
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/>.
"""
# Loads a *.tpl file and replaces all instances of {{variable}} with the value of the variable in the script.
import logging
import re
class Template:
"""
A class used to handle template files.
...
Attributes
----------
filename : str
the path to the template file
template : str
the content of the template file
variables : dict
the variables to be replaced in the template
Methods
-------
load():
Loads the template from the file.
set_variables(variables):
Sets the variables to be replaced in the template.
set_variable(variable, value):
Sets a single variable to be replaced in the template.
replace():
Replaces all instances of {{variable}} in the template with the value of the variable.
write(filename):
Writes the replaced template to a new file.
"""
filename = ""
template = ""
output = ""
options = {}
variables = {}
def __init__(self, filename):
"""
Constructs a new Template object.
Parameters
----------
filename : str
The path to the template file.
"""
self.filename = filename
self.template = ""
self.variables = {}
self.load()
def load(self):
"""
Loads the template from the file.
"""
logging.debug(f"Loading {self.filename}")
with open(self.filename, 'r') as file:
self.template = file.read()
def set_variables(self, variables):
"""
Sets the variables to be replaced in the template.
Parameters
----------
variables : dict
The variables to be replaced in the template.
"""
for key, value in variables.items():
self.set_variable(key, value)
def set_variable(self, variable, value):
"""
Sets a single variable to be replaced in the template.
Parameters
----------
variable : str
The variable to be replaced.
value : str
The value to replace the variable with.
"""
logging.debug(f"Setting {variable} to {value}")
self.variables[variable] = value
def replace(self):
"""
Replaces all instances of {{variable}} in the template with the value of the variable.
"""
self.output = self.template
for key, value in self.variables.items():
logging.debug(f"Replacing {{{{{key}}}}} with {value}")
self.output = re.sub(r'{{\s*' + key + r'\s*}}', value, self.output)
def write(self, filename):
"""
Writes the replaced template to a new file.
Parameters
----------
filename : str
The path to the new file.
"""
with open(filename, 'w') as file:
file.write(self.output)