Message: Added new templating structure
Commit: 77a54cd438ada1f22eca163e9ca05e4d2aedfe13
Date: Tue Feb 8 19:03:24 2011 -0500
Author: Erik Zawadzki <
e...@cs.cmu.edu>
|
1 |
import re |
|
2 |
|
|
3 |
def template(rmap, **kwargs): |
|
4 |
FILE = 'file' |
|
5 |
STRING = 'string' |
|
6 |
|
|
7 |
assert(FILE in kwargs or STRING in kwargs) |
|
8 |
assert(FILE not in kwargs or STRING not in kwargs) |
|
9 |
|
|
10 |
text = '' |
|
11 |
if FILE in kwargs: |
|
12 |
FH = open(kwargs[FILE]) |
|
13 |
text = FH.read() |
|
14 |
FH.close() |
|
15 |
elif STRING in kwargs: |
|
16 |
text = kwargs[STRING] |
|
17 |
else: |
|
18 |
assert(False) |
|
19 |
|
|
20 |
ph_re = re.compile('@\{(\w+)\}') |
|
21 |
ph_list = ph_re.findall(text) |
|
22 |
|
|
23 |
for ph in ph_list: |
|
24 |
if ph in rmap: |
|
25 |
replace = rmap[ph] |
|
26 |
else: |
|
27 |
replace = '' |
|
28 |
text = re.sub('@\{' + ph + '\}', replace, text) |
|
29 |
return text; |
|
30 |
|
|
31 |
def rewrite(rmap, infile, outfile): |
|
32 |
text = template(rmap, file=infile) |
|
33 |
FH = open(outfile, 'w') |
|
34 |
FH.write(text) |
|
35 |
FH.close() |
| 18 |
18 |
</distribution> |
| 19 |
19 |
</time> |
| 20 |
20 |
</generator> |
|
21 |
|
| 21 |
22 |
<so> |
| 22 |
|
<source>so.cpp</source> |
| 23 |
|
<shared>testso</shared> |
| 24 |
|
<pao> |
| 25 |
|
<function>cwritez.main_wait</function> |
| 26 |
|
</pao> |
|
23 |
<pao_h>templates/pao_h.tmpl</pao_h> |
|
24 |
<pao_cpp>templates/pao_cpp.tmpl</pao_cpp> |
|
25 |
<pao_args> |
|
26 |
<pao_class>TestPaoClass</pao_class> |
|
27 |
<pao_state_datatype>int i;</pao_state_datatype> |
|
28 |
<pao_add_func>this->i++;</pao_add_func> |
|
29 |
<pao_merge_func>this->i += add_agg->i;</pao_merge_func> |
|
30 |
</pao_args> |
|
31 |
<mapper_h>templates/mapper_h.tmpl</mapper_h> |
|
32 |
<mapper_cpp>templates/mapper_cpp.tmpl</mapper_cpp> |
|
33 |
<mapper_args> |
|
34 |
<mapper_class>TestMapperClass</mapper_class> |
|
35 |
<mapper_state_datatype> </mapper_state_datatype> |
|
36 |
<mapper_map_func>sleep(1);</mapper_map_func> |
|
37 |
<mapper_part_func> </mapper_part_func> |
|
38 |
</mapper_args> |
| 27 |
39 |
</so> |
| 28 |
40 |
</document> |
| 1 |
1 |
import xml.dom.minidom as xml |
| 2 |
2 |
from xmlz import * |
| 3 |
3 |
import sys |
| 4 |
|
|
| 5 |
|
|
| 6 |
|
import cwritez |
|
4 |
import MicroTemplate as mt |
| 7 |
5 |
|
| 8 |
6 |
# Main |
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
7 |
if len(sys.argv) != 2: |
| 13 |
8 |
print 'Usage: python generator.py <XML config file>' |
| 14 |
9 |
quit() |
|
10 |
|
| 15 |
11 |
dom = xml.parse(sys.argv[1]) |
| 16 |
|
source_file = getText(dom,['so','source']) |
| 17 |
|
exec_file = getText(dom,['so','shared']) |
| 18 |
|
func = getFunction(dom,['so','pao', 'function']) |
| 19 |
|
FH = open(source_file,'w') |
| 20 |
|
FH.write(func()) |
| 21 |
|
FH.close() |
| 22 |
12 |
|
| 23 |
|
cwritez.compile(source_file, exec_file) |
|
13 |
pao_h_template = getText(dom, ['pao_h']) |
|
14 |
pao_cpp_template = getText(dom, ['pao_cpp']) |
|
15 |
pao_sub = getAttrList(dom, ['pao_args']) |
|
16 |
pao_sub['pao_header'] = 'gen_pao.h' |
|
17 |
mt.rewrite(pao_sub, pao_h_template, 'gen_pao.h') |
|
18 |
mt.rewrite(pao_sub, pao_cpp_template, 'gen_pao.cpp') |
|
19 |
|
|
20 |
mapper_h_template = getText(dom, ['mapper_h']) |
|
21 |
mapper_cpp_template = getText(dom, ['mapper_cpp']) |
|
22 |
mapper_sub = getAttrList(dom, ['mapper_args']) |
|
23 |
mapper_sub['mapper_header'] = 'gen_mapper.h' |
|
24 |
mt.rewrite(mapper_sub, mapper_h_template, 'gen_mapper.h') |
|
25 |
mt.rewrite(mapper_sub, mapper_cpp_template, 'gen_mapper.cpp') |
| 24 |
24 |
list_text = getNodeText(node) |
| 25 |
25 |
return map(float, list_text.split(',')) |
| 26 |
26 |
|
|
27 |
def getChildren(dom, path): |
|
28 |
curr = findNode(dom,path) |
|
29 |
return curr.childNodes |
|
30 |
|
|
31 |
def getAttrList(dom, path): |
|
32 |
curr = findNode(dom,path) |
|
33 |
Attr={} |
|
34 |
for c in curr.childNodes: |
|
35 |
if c.hasChildNodes(): |
|
36 |
Attr[c.nodeName] = getText(dom, path + [c.nodeName]) |
|
37 |
return Attr |
|
38 |
|
| 27 |
39 |
def getFunction(dom,path): |
| 28 |
40 |
node = findNode(dom,path) |
| 29 |
41 |
(module_name, sep, function_name) = getNodeText(node).rpartition('.') |