-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_parse.py
161 lines (154 loc) · 4.9 KB
/
query_parse.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
from os import remove, system
f = open("query.txt")
q = []
for s in f.readlines():
q.append(s.strip())
q = ' ' + ' '.join(q) + ' '
f.close()
n = q.find(" name ")
w = q.find(" within ")
i = q.find(" if ")
p = q.find(" print ")
if n == -1 or p == -1:
exit()
if n != -1:
nxt = w if w != -1 else i if i != -1 else p
filename = q[n+6:nxt]
tmp = open("tmp.py", 'w')
print >>tmp,\
"""from tmprenderer import Renderer, __getres
from plasTeX.TeX import TeX
__tex = TeX(file=open("{}"))
__tex.ownerDocument.config['files']['split-level'] = -100
__tex.ownerDocument.config['files']['filename'] = 'test.xml'
__doc = __tex.parse()
__renderer = Renderer()
__renderer.render(__doc)
__output_file = open(\"$$tmp25$$.tmp\", 'w')
print >>__output_file, __getres()
__output_file.close()
""".format(filename)
tmp.close()
tmpr = open("tmprenderer.py", 'w')
print >>tmpr,\
"""import string
from plasTeX.Renderers import Renderer
def __getres():
try:
return __res
except NameError:
return ""
def appendres(x):
global __res
try:
__res += x
except NameError:
__res = x
class Renderer(Renderer):
def default(self, node):
s = []
if len(node.nodeName) == 1 and node.nodeName not in string.letters:
return self.textDefault(node.nodeName)
if node.hasAttributes():
for key, value in node.attributes.items():
if key == 'self':
continue
"""
if w != -1:
nxt = i if i > -1 else p
csci = q[w+7:nxt].find('>')
if csci == -1:
wslicel = q[w+7:nxt].find('[')
if wslicel == -1:
print >>tmpr, """ if node.nodeName == '{}':
appendres(node.source)
""".format(q[w+8:nxt])
else:
wslicer = q[wslicel:nxt].find(']')
slicen = q[w+8+wslicel:wslicel+wslicer]
print >>tmpr, """ target_{0} = {1}
def get_{0}():
try:
return {0}
except NameError:
return 0
def inc_{0}():
global {0}
try:
{0} += 1
except NameError:
{0} = 1
if node.nodeName == '{0}':
if get_{0}() == target_{0}:
appendres(node.source)
inc_{0}()""".format(q[w+8:w+7+wslicel], slicen)
else:
keys = [i.strip() for i in q[w+7:nxt].split('>')]
print >>tmpr, " keys = []"
for key in keys:
print >>tmpr, " keys.append('{}')".format(key)
print >>tmpr, """ def search_keys(node, key):
slicel = keys[key].find('[')
if slicel == -1:
for n in node.childNodes:
if n.name == keys[key]:
if key == len(keys) - 1:
appendres(n.source)
else:
search_keys(n, key + 1)
else:
search_keys(n, key)
else:
slicer = keys[key][slicel:].find(']')
slicen = int(keys[key][slicel+1:slicel+slicer])
try:
target = filter(lambda x : x.nodeName ==
keys[key][:slicel], node.childNodes)[slicen]
if key == len(keys) - 1:
appendres(target.source)
else:
search_keys(target, key + 1)
except IndexError:
for n in node.childNodes:
search_keys(n, key)
"""
wslicel = keys[0].find('[')
if wslicel == -1:
print >>tmpr, """ if node.nodeName == '{}':
search_keys(node, 1)
""".format(keys[0])
else:
wslicer = keys[0][wslicel:].find(']')
slicen = keys[0][wslicel+1:wslicel+wslicer]
print >>tmpr, """ target_{0} = {1}
def get_{0}():
try:
return {0}
except NameError:
return 0
def inc_{0}():
global {0}
try:
{0} += 1
except NameError:
{0} = 1
if node.nodeName == '{0}':
if get_{0}() == target_{0}:
search_keys(node, 1)
inc_{0}()""".format(keys[0][:wslicel], slicen)
print >>tmpr, """ s.append(unicode(node))
return u'\\n'.join(s)
def textDefault(self, node):
return node.replace('&','&').replace('<','<').replace('>','>')
"""
tmpr.close()
system("py tmp.py")
#remove("tmp.py")
#remove("test.xml")
#remove("myrenderer.py")
#remove("myrenderer.pyc")
g = open("$$tmp25$$.tmp")
for j in g.readlines():
print j.strip()
g.close()
remove("$$tmp25$$.tmp")