forked from scummos/kdevelop-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonpythonparser.py
More file actions
executable file
·77 lines (63 loc) · 2.71 KB
/
Copy pathpythonpythonparser.py
File metadata and controls
executable file
·77 lines (63 loc) · 2.71 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
72
73
74
75
76
77
#!/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 = ''
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:
sys.stderr.write("Warning: missing key on node " + str(node) + "\n")
multiple_keys.append('')
key = ','.join(multiple_keys)
node_xmlrepr.setAttribute("NRLST_" + field.lower(), str(key))
else:
try:
key = self.childNodeMap[value]
except KeyError:
key = ''
node_xmlrepr.setAttribute("NR_" + field.lower(), str(key))
self.currentnode = save_currentnode
f = open(sys.argv[1]).read()
v = KDevelopNodeVisitor()
try:
v.visit(ast.parse(f))
except Exception as e:
sys.stderr.write(str(e.lineno) + ':' + str(e.offset))
else:
sys.stdout.write(v.xmlrepr.toprettyxml(indent = " "))