-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslot.py
executable file
·28 lines (28 loc) · 997 Bytes
/
slot.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
from io import StringIO
class template(object):
indent_dec_keyword = re.compile(r'^(elif|else|except|finally)[^0-9a-zA-Z_]+')
def __init__(self, tmpl):
self.text = self.code = ''
deep, indent = 0, ' ' * 4
w = StringIO()
for l in StringIO(tmpl):
if l[0] != '$':
w.write("{0}_output.append(f{1})\n".format(indent * deep, repr(l[:-1])))
continue
l = l[1:].strip()
if l == 'end':
deep -= 1
w.write(indent * deep + '#end\n')
continue
if template.indent_dec_keyword.match(l): deep -= 1
w.write(indent * deep + l + '\n')
if l[-1] == ':': deep += 1
self.code = w.getvalue()
def render(self, **kwargs):
kwargs["_output"] = []
exec(self.code, {}, kwargs)
self.text = '\n'.join(kwargs["_output"])+'\n'
return self.text