-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
197 lines (161 loc) · 6.34 KB
/
parser.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import xml.parsers.expat
import StringIO
import collections
import path2tree
class UnknownElementError(Exception):
def __init__(self, name):
self.name = name
def __repr__(self):
return "UnknownElementError: element '%s'" % self.name
def __str__(self):
return "UnknownXMLStructureError: path '%s'" % self.name
class UnknownXMLStructureError(Exception):
def __init__(self, path):
self.path = path
def __repr__(self):
return "UnknownXMLStructureError: path '%s'" % self.path
def __str__(self):
return "UnknownXMLStructureError: path '%s'" % self.path
class XMLProperty(object):
__slots__ = ("source", "offset", "_len", "_hash")
def __init__(self, source, offset, _len):
self.source = source
self.offset = offset
self._len = _len
self._hash = None
def load(self):
oldpos = self.source.tell()
self.source.seek(self.offset)
ret = self.source.read(self._len).strip()
self.source.seek(oldpos)
ret = ret.decode("utf-8")
return ret
def store(self, source, offset, _len):
self.source = source
self.offset = offset
self._len = _len
def __eq__(self, other):
return hash(self) == hash(other)
def __ne__(self, other):
return hash(self) != hash(other)
def __len__(self):
return self._len
def __repr__(self):
self.source.seek(self.offset)
ret = self.source.read(self._len).strip()
return ret
def __getitem__(self, _len):
start = _len.start if _len.start else 0
stop = _len.stop if _len.stop else 0
self.source.seek(self.offset + start)
ret = self.source.read(stop).strip()
return ret
def __hash__(self):
if not self._hash:
self.source.seek(self.offset)
ret = self.source.read(self._len).strip()
self._hash = hash(ret)
return self._hash
class Parser(object):
def __init__(self, nodes_limit=16000, depth_limit=3):
self.parser = xml.parsers.expat.ParserCreate()
self.parser.StartElementHandler = self.start_el_handler
self.parser.EndElementHandler = self.end_el_handler
self.parser.CharacterDataHandler = self.char_data_handler
self.el_stack = collections.deque()
self.path = ""
self.cdata = False
self.tree = path2tree.Node("root")
self.light_ended = False
self.nodes_limit = nodes_limit
self.depth_limit = depth_limit
self.nodes_counter = 0
def start_el_handler(self, name, attrs):
if self.light_ended:
self.start_el_light_ended(name, attrs)
if len(self.el_stack) > self.depth_limit or\
self.nodes_counter > self.nodes_limit:
self.start_el_light(name, attrs)
else:
self.start_el_normal(name, attrs)
self.nodes_counter += 1
def start_el_normal(self, name, attrs):
self.path = ".".join([el["name"] for el in self.el_stack] + [name])
added = self.tree.fill(self.path, value=None, _type="content")
self.el_stack.append({"name": name, "attrs": attrs, "node": added})
for key, val in attrs.iteritems():
self.tree.fill("%s.%s" % (self.path, key),
value=val, _type="attr")
self.cdata = None
def start_el_light_ended(self, name, attrs):
node = self.el_stack[-1]["node"]
if self.light_ended and node:
stack_item = self.el_stack.pop()
node = stack_item["node"]
node._len = self.parser.CurrentByteIndex - node.offset
self.path = ".".join([el["name"] for el in self.el_stack])
self.light_ended = False
def start_el_light(self, name, attrs):
self.path = ".".join([el["name"] for el in self.el_stack] + [name])
added = self.tree.fill_light(self.path, "content", self.source,
self.parser.CurrentByteIndex, 0)
self.el_stack.append({"name": name, "attrs": attrs, "node": added})
self.cdata = None
def end_el_handler(self, name):
self.start_el_light_ended(name, None)
node = self.el_stack[-1]["node"]
if not node or isinstance(node, path2tree.LightNode):
self.end_el_light(name)
else:
self.end_el_normal(name)
self.path = ".".join([el["name"] for el in self.el_stack])
def end_el_light(self, name):
node = self.el_stack[-1]["node"]
if node:
self.light_ended = True
else:
self.el_stack.pop()
def end_el_normal(self, name):
stack_item = self.el_stack.pop()
node = stack_item["node"]
self.path = ".".join([el["name"] for el in self.el_stack])
if self.cdata:
self.cdata_xml._len = self.parser.CurrentByteIndex - self.cdata_xml.offset
node.value = self.cdata_xml
node._type = "content"
self.cdata = None
def char_data_handler(self, text):
node = self.el_stack[-1]["node"]
if not node or isinstance(node, path2tree.LightNode):
self.char_data_light(text)
else:
self.char_data_normal(text)
def char_data_light(self, text):
pass
def char_data_normal(self, text):
if self.cdata is None:
self.cdata = False
self.cdata_xml = XMLProperty(self.source,
self.parser.CurrentByteIndex, 0)
if not self.cdata and text.strip():
self.cdata = True
def parse_file(self, fp):
self.source = fp
self.parser.ParseFile(fp)
if self.light_ended:
stack_item = self.el_stack.pop()
node = stack_item["node"]
node._len = self.parser.CurrentByteIndex - node.offset
self.path = ".".join([el["name"] for el in self.el_stack])
self.light_ended = False
return self.tree
def parse_str(self, _str):
self.source = StringIO.StringIO(_str)
self.parser.Parse(_str, True)
if self.light_ended:
stack_item = self.el_stack.pop()
node = stack_item["node"]
node._len = self.parser.CurrentByteIndex - node.offset
self.path = ".".join([el["name"] for el in self.el_stack])
self.light_ended = False
return self.tree