Repository navigation
Expand file tree
/
Copy pathpythonpythonparser.py
More file actions
executable file
·71 lines (57 loc) · 2.42 KB
/
Copy pathpythonpythonparser.py
File metadata and controls
executable file
·71 lines (57 loc) · 2.42 KB
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
#!/usr/bin/env python2.6
import ast
from xml.dom.minidom import Document
import types
import sys
class KDevelopNodeVisitor(ast.NodeVisitor):
xmlrepr = Document()
basenode = None
currentnode = None
nodecnt = 0
childNodeMap = {}
def __init__(self, *arg, **args):
super(KDevelopNodeVisitor, self).__init__(*arg, **args)
self.basenode = self.xmlrepr.createElement("pythonast")
self.xmlrepr.appendChild(self.basenode)
self.currentnode = self.basenode
def generic_visit(self, node):
self.nodecnt += 1
#self.childNodeMap[self.nodecnt] = node
self.childNodeMap[node] = self.nodecnt
node_xmlrepr = self.xmlrepr.createElement(node.__class__.__name__ + "Ast")
node_xmlrepr.setAttribute('nodecnt', str(self.nodecnt))
self.currentnode.appendChild(node_xmlrepr)
save_currentnode = self.currentnode
self.currentnode = node_xmlrepr
fields = list(node._attributes)
fields.extend(list(node._fields))
searching_locally = []
for field in fields:
value = getattr(node, field)
if type(value) not in [types.IntType, types.StringType, types.FloatType, types.BooleanType]:
continue
node_xmlrepr.setAttribute(field.lower(), str(value))
super(KDevelopNodeVisitor, self).generic_visit(node)
key = 'None'
for field in fields:
multiple_keys = []
value = getattr(node, field)
if type(value) not in [types.IntType, types.StringType, types.FloatType, types.BooleanType]:
if type(value) == types.ListType:
for currentValue in value:
try:
multiple_keys.append(str(self.childNodeMap[currentValue]))
except KeyError:
multiple_keys.append('None')
key = ','.join(multiple_keys)
else:
try:
key = self.childNodeMap[value]
except KeyError:
key = 'None'
node_xmlrepr.setAttribute(field.lower(), str(key))
self.currentnode = save_currentnode
f = open(sys.argv[1]).read()
v = KDevelopNodeVisitor()
v.visit(ast.parse(f))
print v.xmlrepr.toprettyxml(indent = " ")