Message: Renaming, plus fixed the generator
Commit: d2ed4a35b950925cd0e8dcf8d9fd389ce17258d5
Date: Tue Apr 26 18:48:35 2011 -0400
Author: Erik Zawadzki <
e...@cs.cmu.edu>
| 1 |
|
import re |
| 2 |
|
|
| 3 |
|
# Constant |
| 4 |
|
MAX_RECURSION_DEPTH = 25 |
| 5 |
|
|
| 6 |
|
def template(rmap, **kwargs): |
| 7 |
|
|
| 8 |
|
tmp_symb_id = 0 |
| 9 |
|
|
| 10 |
|
# 1) Deal with the input. Could be either a string or file |
| 11 |
|
FILE = 'file' |
| 12 |
|
STRING = 'string' |
| 13 |
|
|
| 14 |
|
assert(FILE in kwargs or STRING in kwargs) |
| 15 |
|
assert(FILE not in kwargs or STRING not in kwargs) |
| 16 |
|
|
| 17 |
|
text = '' |
| 18 |
|
if FILE in kwargs: |
| 19 |
|
FH = open(kwargs[FILE]) |
| 20 |
|
text = FH.read() |
| 21 |
|
FH.close() |
| 22 |
|
elif STRING in kwargs: |
| 23 |
|
text = kwargs[STRING] |
| 24 |
|
else: |
| 25 |
|
assert(False) |
| 26 |
|
|
| 27 |
|
# 2) Replace until we cannot replace no more. |
| 28 |
|
symb_re = re.compile('\@\{(\w+?)\}') |
| 29 |
|
repeat_re = re.compile('\@\@\[(.+?)\]') |
| 30 |
|
for i in xrange(MAX_RECURSION_DEPTH): |
| 31 |
|
new_text = text |
| 32 |
|
|
| 33 |
|
# 2.1) Get occurances list |
| 34 |
|
symb_list = symb_re.findall(text) |
| 35 |
|
repeat_list = repeat_re.findall(text) |
| 36 |
|
# 2.3) Process all repeated symbols |
| 37 |
|
for rep in repeat_list: |
| 38 |
|
# 2.3.1) Parse the rule |
| 39 |
|
(rule, body) = rep.split(':') |
| 40 |
|
if '@' in body: |
| 41 |
|
continue |
| 42 |
|
body = body.split(',') |
| 43 |
|
replace = [] |
| 44 |
|
# 2.3.2) Build the unrolling |
| 45 |
|
for i in xrange(len(body)): |
| 46 |
|
tmp_symb = 'TMP_{0}_{1}'.format(tmp_symb_id, i) |
| 47 |
|
tmp_symb_id += 1 |
| 48 |
|
rmap[tmp_symb] = body[i] |
| 49 |
|
replace.append(re.sub('@', '@{' + tmp_symb + '}', rule)) |
| 50 |
|
# 2.3.3) Substitute |
| 51 |
|
new_text = re.sub(re.escape('@@[' + rep + ']'),\ |
| 52 |
|
'\n'.join(replace),\ |
| 53 |
|
new_text) |
| 54 |
|
# 2.2) Process all simple sybmols |
| 55 |
|
for symb in symb_list: |
| 56 |
|
if symb in rmap: |
| 57 |
|
replace = rmap[symb] |
| 58 |
|
else: |
| 59 |
|
print "Cannot find definition for @{" + symb + "}" |
| 60 |
|
replace = '#BAD SYMBOL#' |
| 61 |
|
new_text = re.sub('@\{' + symb + '\}', replace, new_text) |
| 62 |
|
|
| 63 |
|
# Unchanged? |
| 64 |
|
if new_text == text: |
| 65 |
|
return text |
| 66 |
|
text = new_text |
| 67 |
|
return text + '\n#MAX DEPTH#'; |
| 68 |
|
|
| 69 |
|
def rewrite(rmap, infile, outfile): |
| 70 |
|
text = template(rmap, file=infile) |
| 71 |
|
FH = open(outfile, 'w') |
| 72 |
|
FH.write(text) |
| 73 |
|
FH.close() |
| 1 |
|
import xml.dom.minidom as xml |
| 2 |
|
|
| 3 |
|
def findNode(dom,path_list): |
| 4 |
|
current = dom |
| 5 |
|
for n in path_list: |
| 6 |
|
current = current.getElementsByTagName(n).item(0) |
| 7 |
|
if(not current): |
| 8 |
|
print "Could not find node corresponding to {0}.".format(path_list) |
| 9 |
|
assert(current) |
| 10 |
|
return current |
| 11 |
|
|
| 12 |
|
def getNodeText(node): |
| 13 |
|
if node.firstChild.nodeType == node.TEXT_NODE: |
| 14 |
|
return str(node.firstChild.data) |
| 15 |
|
return '' |
| 16 |
|
|
| 17 |
|
def getText(dom, path): |
| 18 |
|
node = findNode(dom,path) |
| 19 |
|
return getNodeText(node) |
| 20 |
|
|
| 21 |
|
def getNumber(dom, path): |
| 22 |
|
node = findNode(dom,path) |
| 23 |
|
return int(getNodeText(node)) |
| 24 |
|
|
| 25 |
|
def getNumberList(dom, path): |
| 26 |
|
node = findNode(dom,path) |
| 27 |
|
list_text = getNodeText(node) |
| 28 |
|
return map(float, list_text.split(',')) |
| 29 |
|
|
| 30 |
|
def getChildren(dom, path): |
| 31 |
|
curr = findNode(dom,path) |
| 32 |
|
return curr.childNodes |
| 33 |
|
|
| 34 |
|
def getAttrList(dom, path): |
| 35 |
|
curr = findNode(dom,path) |
| 36 |
|
Attr={} |
| 37 |
|
for c in curr.childNodes: |
| 38 |
|
if c.hasChildNodes(): |
| 39 |
|
Attr[c.nodeName] = getText(dom, path + [c.nodeName]) |
| 40 |
|
return Attr |
| 41 |
|
|
| 42 |
|
def getFunction(dom,path): |
| 43 |
|
node = findNode(dom,path) |
| 44 |
|
text = getNodeText(node) |
| 45 |
|
try: |
| 46 |
|
(module_name, sep, function_name) = text.rpartition('.') |
| 47 |
|
module = __import__(module_name) |
| 48 |
|
func = getattr(module, function_name) |
| 49 |
|
except ValueError: |
| 50 |
|
func = eval(text) |
| 51 |
|
return func |
| 1 |
1 |
import xml.dom.minidom as xml |
| 2 |
|
from XMLWrap import * |
|
2 |
from xmlwrap import * |
| 3 |
3 |
import random |
| 4 |
4 |
import subprocess, os, sys |
| 5 |
5 |
|
|
6 |
# Main function for generating |
|
7 |
|
|
8 |
def generate(filename, key_dist, value_dist, time_dist, N, sort=None): |
|
9 |
FH = open(filename, 'w') |
|
10 |
for d_id in xrange(N): |
|
11 |
sampled_key = key_dist.sample() |
|
12 |
sampled_value = value_dist.sample(sampled_key.k) |
|
13 |
sampled_time = time_dist.sample(sampled_key.k, sampled_value.v) |
|
14 |
data = DataPoint(sampled_key, sampled_value, sampled_time) |
|
15 |
data.write(FH) |
|
16 |
FH.close() |
|
17 |
if sort: |
|
18 |
subprocess.call('sort {0} -o {0}'.format(filename), shell=True) |
|
19 |
|
|
20 |
# Useful classes |
|
21 |
|
| 6 |
22 |
class Key(object): |
| 7 |
23 |
def __init__(self, k): |
| 8 |
24 |
self.k = k |
| 19 |
35 |
def __init__(self, func, *args): |
| 20 |
36 |
self.F = func |
| 21 |
37 |
self.args = args |
| 22 |
|
def sample(self,v): |
|
38 |
def sample(self): |
| 23 |
39 |
return self.F(*self.args) |
| 24 |
40 |
def __str__(self): |
| 25 |
41 |
return '{0}({1})'.format(self.F, str(self.args)) |
| 26 |
42 |
|
| 27 |
43 |
class KeyDistro(Distro): |
| 28 |
|
def __init__(self, func, *args): |
| 29 |
|
self.F = func |
| 30 |
|
self.args = args |
| 31 |
|
def sample(self,v): |
|
44 |
def sample(self): |
| 32 |
45 |
return Key(self.F(*self.args)) |
| 33 |
46 |
|
| 34 |
47 |
class ValueDistro(Distro): |
| 35 |
48 |
def __init__(self, func, *args): |
| 36 |
49 |
self.F = func |
| 37 |
50 |
self.args = args |
| 38 |
|
def sample(self,k,v): |
| 39 |
|
return Key(self.F(k,*self.args)) |
|
51 |
def sample(self,k): |
|
52 |
return Value(self.F(k,*self.args)) |
| 40 |
53 |
|
| 41 |
54 |
class TimeDistro(Distro): |
| 42 |
|
pass |
|
55 |
def __init__(self, func, *args): |
|
56 |
self.F = func |
|
57 |
self.args = args |
|
58 |
def sample(self,k,v): |
|
59 |
return self.F(k,v,*self.args) |
| 43 |
60 |
|
| 44 |
61 |
|
| 45 |
62 |
class DataPoint(object): |
| 53 |
70 |
return '{0}; {1}, {2}'.format(self.key, self.value, self.time) |
| 54 |
71 |
def __str__(self): |
| 55 |
72 |
return self.on_disk_str() |
| 56 |
|
|
| 57 |
|
def generate(filename, key_dist, value_dist, time_dist, N, sort=None): |
| 58 |
|
FH = open(filename, 'w') |
| 59 |
|
for d_id in xrange(N): |
| 60 |
|
value = Value(v_id) |
| 61 |
|
sampled_key = key_dist.sample(value) |
| 62 |
|
sampled_time = time_dist.sample(value) |
| 63 |
|
data = DataPoint(sampled_key, value, sampled_time) |
| 64 |
|
data.write(FH) |
| 65 |
|
FH.close() |
| 66 |
|
if sort: |
| 67 |
|
subprocess.call('sort {0} -o {0}'.format(filename), shell=True) |
|
73 |
|
| 68 |
74 |
|
| 69 |
75 |
#XML |
| 70 |
76 |
|
|
1 |
import re |
|
2 |
|
|
3 |
# Constant |
|
4 |
MAX_RECURSION_DEPTH = 25 |
|
5 |
|
|
6 |
def template(rmap, **kwargs): |
|
7 |
|
|
8 |
tmp_symb_id = 0 |
|
9 |
|
|
10 |
# 1) Deal with the input. Could be either a string or file |
|
11 |
FILE = 'file' |
|
12 |
STRING = 'string' |
|
13 |
|
|
14 |
assert(FILE in kwargs or STRING in kwargs) |
|
15 |
assert(FILE not in kwargs or STRING not in kwargs) |
|
16 |
|
|
17 |
text = '' |
|
18 |
if FILE in kwargs: |
|
19 |
FH = open(kwargs[FILE]) |
|
20 |
text = FH.read() |
|
21 |
FH.close() |
|
22 |
elif STRING in kwargs: |
|
23 |
text = kwargs[STRING] |
|
24 |
else: |
|
25 |
assert(False) |
|
26 |
|
|
27 |
# 2) Replace until we cannot replace no more. |
|
28 |
symb_re = re.compile('\@\{(\w+?)\}') |
|
29 |
repeat_re = re.compile('\@\@\[(.+?)\]') |
|
30 |
for i in xrange(MAX_RECURSION_DEPTH): |
|
31 |
new_text = text |
|
32 |
|
|
33 |
# 2.1) Get occurances list |
|
34 |
symb_list = symb_re.findall(text) |
|
35 |
repeat_list = repeat_re.findall(text) |
|
36 |
# 2.3) Process all repeated symbols |
|
37 |
for rep in repeat_list: |
|
38 |
# 2.3.1) Parse the rule |
|
39 |
(rule, body) = rep.split(':') |
|
40 |
if '@' in body: |
|
41 |
continue |
|
42 |
body = body.split(',') |
|
43 |
replace = [] |
|
44 |
# 2.3.2) Build the unrolling |
|
45 |
for i in xrange(len(body)): |
|
46 |
tmp_symb = 'TMP_{0}_{1}'.format(tmp_symb_id, i) |
|
47 |
tmp_symb_id += 1 |
|
48 |
rmap[tmp_symb] = body[i] |
|
49 |
replace.append(re.sub('@', '@{' + tmp_symb + '}', rule)) |
|
50 |
# 2.3.3) Substitute |
|
51 |
new_text = re.sub(re.escape('@@[' + rep + ']'),\ |
|
52 |
'\n'.join(replace),\ |
|
53 |
new_text) |
|
54 |
# 2.2) Process all simple sybmols |
|
55 |
for symb in symb_list: |
|
56 |
if symb in rmap: |
|
57 |
replace = rmap[symb] |
|
58 |
else: |
|
59 |
print "Cannot find definition for @{" + symb + "}" |
|
60 |
replace = '#BAD SYMBOL#' |
|
61 |
new_text = re.sub('@\{' + symb + '\}', replace, new_text) |
|
62 |
|
|
63 |
# Unchanged? |
|
64 |
if new_text == text: |
|
65 |
return text |
|
66 |
text = new_text |
|
67 |
return text + '\n#MAX DEPTH#'; |
|
68 |
|
|
69 |
def rewrite(rmap, infile, outfile): |
|
70 |
text = template(rmap, file=infile) |
|
71 |
FH = open(outfile, 'w') |
|
72 |
FH.write(text) |
|
73 |
FH.close() |
| 1 |
1 |
import xml.dom.minidom as xml |
| 2 |
|
from XMLWrap import * |
|
2 |
from xmlwrap import * |
| 3 |
3 |
import sys |
| 4 |
|
import MicroTemplate as mt |
|
4 |
import microtemplate as mt |
| 5 |
5 |
|
| 6 |
6 |
if len(sys.argv) != 2: |
| 7 |
7 |
print 'Usage: python generator.py <XML config file>' |
| 11 |
11 |
dom = xml.parse(sys.argv[1]) |
| 12 |
12 |
|
| 13 |
13 |
# 2) Generate the PAO file |
| 14 |
|
# 2.1) Get file names |
|
14 |
# 2.1) Get file names3 |
| 15 |
15 |
pao_h_tmpl = getText(dom, ['pao_h_tmpl']) |
| 16 |
16 |
pao_cpp_tmpl = getText(dom, ['pao_cpp_tmpl']) |
| 17 |
17 |
pao_h_gen = getText(dom, ['pao_h_gen']) |
|
1 |
import xml.dom.minidom as xml |
|
2 |
|
|
3 |
def findNode(dom,path_list): |
|
4 |
current = dom |
|
5 |
for n in path_list: |
|
6 |
current = current.getElementsByTagName(n).item(0) |
|
7 |
if(not current): |
|
8 |
return None |
|
9 |
return current |
|
10 |
|
|
11 |
def getNodeText(node): |
|
12 |
if not node: |
|
13 |
return None |
|
14 |
if node.firstChild.nodeType == node.TEXT_NODE: |
|
15 |
return str(node.firstChild.data) |
|
16 |
return '' |
|
17 |
|
|
18 |
def getText(dom, path): |
|
19 |
node = findNode(dom,path) |
|
20 |
return getNodeText(node) |
|
21 |
|
|
22 |
def getNumber(dom, path): |
|
23 |
node = findNode(dom,path) |
|
24 |
return int(getNodeText(node)) |
|
25 |
|
|
26 |
def getNumberList(dom, path): |
|
27 |
node = findNode(dom,path) |
|
28 |
if not node: |
|
29 |
return [] |
|
30 |
list_text = getNodeText(node) |
|
31 |
return map(float, list_text.split(',')) |
|
32 |
|
|
33 |
def getChildren(dom, path): |
|
34 |
curr = findNode(dom,path) |
|
35 |
return curr.childNodes |
|
36 |
|
|
37 |
def getAttrList(dom, path): |
|
38 |
curr = findNode(dom,path) |
|
39 |
Attr={} |
|
40 |
for c in curr.childNodes: |
|
41 |
if c.hasChildNodes(): |
|
42 |
Attr[c.nodeName] = getText(dom, path + [c.nodeName]) |
|
43 |
return Attr |
|
44 |
|
|
45 |
def getFunction(dom,path): |
|
46 |
node = findNode(dom,path) |
|
47 |
text = getNodeText(node) |
|
48 |
try: |
|
49 |
(module_name, sep, function_name) = text.rpartition('.') |
|
50 |
module = __import__(module_name) |
|
51 |
func = getattr(module, function_name) |
|
52 |
except ValueError: |
|
53 |
func = eval(text) |
|
54 |
return func |