-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplater.py
executable file
·308 lines (245 loc) · 8.17 KB
/
templater.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
import re
import operator
import ast
import json
import sys
#Create all predefined reg exp variables that define
#1. Variable - Individual variable substitutions
#2. Blocks - Blocks of code such as loops and conditionals
beginvar = '<*'
endvar = '*>'
beginblock = '<%'
endblock = '%>'
##regex we want = (\<\*.*?\*\> | \<\%.*?\%\>)
regexp = re.compile(r"(%s.*?%s|%s.*?%s)" % (
re.escape(beginvar),
re.escape(endvar),
re.escape(beginblock),
re.escape(endblock)
))
#Create holders for different types of blocks
variableshard = 0
beginblockshard = 1
endblockshard = 2
textshard = 3
#whitespaces holder
spacedout = re.compile('\s+')
operator_lookup_table = {
'<': operator.lt,
'>': operator.gt,
'==': operator.eq,
'!=': operator.ne,
'<=': operator.le,
'>=': operator.ge
}
class TemplateError(Exception):
pass
class TemplateContextError(TemplateError):
def __init__(self, context_var):
self.context_var = context_var
def __str__(self):
return "cannot resolve '%s'" % self.context_var
class TemplateSyntaxError(TemplateError):
def __init__(self, error_syntax):
self.error_syntax = error_syntax
def __str__(self):
return "'%s' seems like invalid syntax" % self.error_syntax
def evaluatexp(expr):
try:
#print(expr)
return 'literal', ast.literal_eval(expr)
except ValueError:
return 'name', expr
def resolve(name, context):
if name.startswith('..'):
context = context.get('..', {})
name = name[2:]
try:
for tok in name.split('.'):
context = context[tok]
return context
except KeyError:
raise TemplateContextError(name)
class _Fragment(object):
def __init__(self, raw_text):
self.raw = raw_text
self.clean = self.clean_fragment()
def clean_fragment(self):
if self.raw[:2] in (beginvar, beginblock):
return self.raw.strip()[2:-2].strip()
return self.raw
@property
def type(self):
raw_start = self.raw[:2]
#print(raw_start)
if raw_start == beginvar:
return variableshard
elif raw_start == beginblock:
return endblockshard if self.clean[:3].lower() == 'end' else beginblockshard
else:
return textshard
#The heart of the code - class to hold node type and each variable type becomes a concrete
#subclass that implements process_shard and render
class _Node(object):
creates_scope = False
def __init__(self, fragment=None):
self.children = []
self.process_fragment(fragment)
def process_fragment(self, fragment):
pass
def enter_scope(self):
pass
def render(self, context):
pass
def exit_scope(self):
pass
def render_children(self, context, children=None):
if children is None:
children = self.children
def render_child(child):
child_html = child.render(context)
return '' if not child_html else str(child_html)
return ''.join(map(render_child, children))
class _ScopableNode(_Node):
creates_scope = True
class _Root(_Node):
def render(self, context):
return self.render_children(context)
#Concrete variable class
class _Variable(_Node):
def process_fragment(self, fragment):
self.name = fragment
def render(self, context):
return resolve(self.name, context)
#Looping
class _Each(_ScopableNode):
def process_fragment(self, fragment):
try:
_, it = spacedout.split(fragment, 1)
self.it = evaluatexp(it)
except ValueError:
raise TemplateSyntaxError(fragment)
def render(self, context):
items = self.it[1] if self.it[0] == 'literal' else resolve(self.it[1], context)
def render_item(item):
return self.render_children({'..': context, 'it' : item})
return ''.join(map(render_item, items))
#If conditionals
class _If(_ScopableNode):
def process_fragment(self, fragment):
bits = fragment.split()[1:]
if len(bits) not in (1, 3):
raise TemplateSyntaxError(fragment)
self.lhs = eval_expression(bits[0])
if len(bits) == 3:
self.op = bits[1]
self.rhs = eval_expression(bits[2])
def render(self, context):
lhs = self.resolve_side(self.lhs, context)
if hasattr(self, 'op'):
op = operator_lookup_table.get(self.op)
if op is None:
raise TemplateSyntaxError(self.op)
rhs = self.resolve_side(self.rhs, context)
exec_if_branch = op(lhs, rhs)
else:
exec_if_branch = operator.truth(lhs)
if_branch, else_branch = self.split_children()
return self.render_children(context,
self.if_branch if exec_if_branch else self.else_branch)
def resolve_side(self, side, context):
return side[1] if side[0] == 'literal' else resolve(side[1], context)
def exit_scope(self):
self.if_branch, self.else_branch = self.split_children()
def split_children(self):
if_branch, else_branch = [], []
curr = if_branch
for child in self.children:
if isinstance(child, _Else):
curr = else_branch
continue
curr.append(child)
return if_branch, else_branch
#Else of the conditionals
class _Else(_Node):
def render(self, context):
pass
class _Text(_Node):
def process_fragment(self, fragment):
self.text = fragment
def render(self, context):
return self.text
class Compiler(object):
def __init__(self, template_string):
self.template_string = template_string
def each_fragment(self):
for fragment in regexp.split(self.template_string):
if fragment:
yield _Fragment(fragment)
def compile(self):
root = _Root()
scope_stack = [root]
for fragment in self.each_fragment():
if not scope_stack:
raise TemplateError('nesting issues')
parent_scope = scope_stack[-1]
if fragment.type == endblockshard:
parent_scope.exit_scope()
scope_stack.pop()
continue
new_node = self.create_node(fragment)
if new_node:
parent_scope.children.append(new_node)
if new_node.creates_scope:
scope_stack.append(new_node)
new_node.enter_scope()
return root
def create_node(self, fragment):
node_class = None
if fragment.type == textshard:
node_class = _Text
elif fragment.type == variableshard:
node_class = _Variable
elif fragment.type == beginblockshard:
cmd = fragment.clean.split()[0]
if cmd.lower() == 'each':
node_class = _Each
#Sample If else conditional classes
elif cmd.lower() == 'if':
node_class = _If
elif cmd.lower() == 'else':
node_class = _Else
if node_class is None:
raise TemplateSyntaxError(fragment)
return node_class(fragment.clean)
class TemplateEngine(object):
def __init__(self, contents):
self.contents = contents
self.root = Compiler(contents).compile()
def render(self, **args):
return self.root.render(args)
def main():
if len(sys.argv)<>4:
print("Usage: python templater.py file.template data.json output.html")
else:
#Get file arguments, comply with big brother
jsonfile = sys.argv[2]
templatefile = sys.argv[1]
outputfile = sys.argv[3]
#Read json
with open(jsonfile) as f:
jsondata = json.loads(f.read())
#Read template
with open(templatefile) as f:
html = f.read()
#perform jedi mind tricks
template = TemplateEngine(html)
#print output
with open(outputfile, 'w') as f:
rendered = template.render(**jsondata)
f.write(rendered)
#print (rendered)
print("Process Completed")
if __name__ == "__main__":
main()