forked from manojgudi/sandhi
-
Notifications
You must be signed in to change notification settings - Fork 6
/
expand_template.py
33 lines (28 loc) · 988 Bytes
/
expand_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
# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
import os
import sys
from Cheetah.Template import Template
MAX_ARGS = 11
def expand(t, n):
out = list()
for i in range(n): out.append(t.replace('%d', str(i)))
return ', '.join(out)
def cleanup(code):
code = code.replace('template <>', 'inline')
code = code.replace(', >', '>')
code = code.replace(', )', ')')
code = code.replace('\\#', '#')
return code
if __name__ == '__main__':
in_path = sys.argv[1]
out_path = sys.argv[2]
tmpl = open(in_path, 'r').read()
for key in ['define', 'include', 'if', 'endif', 'else', 'ifdef', 'ifndef', 'pragma']:
tmpl = tmpl.replace('#%s'%key, '\\#%s'%key)
code = str(Template(tmpl, dict(
MAX_ARGS=MAX_ARGS, expand=expand,
)))
code = cleanup(code)
if not os.path.exists(out_path) or open(out_path, 'r').read() != code:
print 'write code to', out_path
open(out_path, 'w').write(code)