jpayne@68
|
1 #
|
jpayne@68
|
2 # ElementTree
|
jpayne@68
|
3 # $Id: ElementPath.py 3375 2008-02-13 08:05:08Z fredrik $
|
jpayne@68
|
4 #
|
jpayne@68
|
5 # limited xpath support for element trees
|
jpayne@68
|
6 #
|
jpayne@68
|
7 # history:
|
jpayne@68
|
8 # 2003-05-23 fl created
|
jpayne@68
|
9 # 2003-05-28 fl added support for // etc
|
jpayne@68
|
10 # 2003-08-27 fl fixed parsing of periods in element names
|
jpayne@68
|
11 # 2007-09-10 fl new selection engine
|
jpayne@68
|
12 # 2007-09-12 fl fixed parent selector
|
jpayne@68
|
13 # 2007-09-13 fl added iterfind; changed findall to return a list
|
jpayne@68
|
14 # 2007-11-30 fl added namespaces support
|
jpayne@68
|
15 # 2009-10-30 fl added child element value filter
|
jpayne@68
|
16 #
|
jpayne@68
|
17 # Copyright (c) 2003-2009 by Fredrik Lundh. All rights reserved.
|
jpayne@68
|
18 #
|
jpayne@68
|
19 # fredrik@pythonware.com
|
jpayne@68
|
20 # http://www.pythonware.com
|
jpayne@68
|
21 #
|
jpayne@68
|
22 # --------------------------------------------------------------------
|
jpayne@68
|
23 # The ElementTree toolkit is
|
jpayne@68
|
24 #
|
jpayne@68
|
25 # Copyright (c) 1999-2009 by Fredrik Lundh
|
jpayne@68
|
26 #
|
jpayne@68
|
27 # By obtaining, using, and/or copying this software and/or its
|
jpayne@68
|
28 # associated documentation, you agree that you have read, understood,
|
jpayne@68
|
29 # and will comply with the following terms and conditions:
|
jpayne@68
|
30 #
|
jpayne@68
|
31 # Permission to use, copy, modify, and distribute this software and
|
jpayne@68
|
32 # its associated documentation for any purpose and without fee is
|
jpayne@68
|
33 # hereby granted, provided that the above copyright notice appears in
|
jpayne@68
|
34 # all copies, and that both that copyright notice and this permission
|
jpayne@68
|
35 # notice appear in supporting documentation, and that the name of
|
jpayne@68
|
36 # Secret Labs AB or the author not be used in advertising or publicity
|
jpayne@68
|
37 # pertaining to distribution of the software without specific, written
|
jpayne@68
|
38 # prior permission.
|
jpayne@68
|
39 #
|
jpayne@68
|
40 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
|
jpayne@68
|
41 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
|
jpayne@68
|
42 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
|
jpayne@68
|
43 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
|
jpayne@68
|
44 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
jpayne@68
|
45 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
jpayne@68
|
46 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
jpayne@68
|
47 # OF THIS SOFTWARE.
|
jpayne@68
|
48 # --------------------------------------------------------------------
|
jpayne@68
|
49
|
jpayne@68
|
50 # Licensed to PSF under a Contributor Agreement.
|
jpayne@68
|
51 # See http://www.python.org/psf/license for licensing details.
|
jpayne@68
|
52
|
jpayne@68
|
53 ##
|
jpayne@68
|
54 # Implementation module for XPath support. There's usually no reason
|
jpayne@68
|
55 # to import this module directly; the <b>ElementTree</b> does this for
|
jpayne@68
|
56 # you, if needed.
|
jpayne@68
|
57 ##
|
jpayne@68
|
58
|
jpayne@68
|
59 import re
|
jpayne@68
|
60
|
jpayne@68
|
61 xpath_tokenizer_re = re.compile(
|
jpayne@68
|
62 r"("
|
jpayne@68
|
63 r"'[^']*'|\"[^\"]*\"|"
|
jpayne@68
|
64 r"::|"
|
jpayne@68
|
65 r"//?|"
|
jpayne@68
|
66 r"\.\.|"
|
jpayne@68
|
67 r"\(\)|"
|
jpayne@68
|
68 r"[/.*:\[\]\(\)@=])|"
|
jpayne@68
|
69 r"((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
|
jpayne@68
|
70 r"\s+"
|
jpayne@68
|
71 )
|
jpayne@68
|
72
|
jpayne@68
|
73 def xpath_tokenizer(pattern, namespaces=None):
|
jpayne@68
|
74 default_namespace = namespaces.get('') if namespaces else None
|
jpayne@68
|
75 parsing_attribute = False
|
jpayne@68
|
76 for token in xpath_tokenizer_re.findall(pattern):
|
jpayne@68
|
77 ttype, tag = token
|
jpayne@68
|
78 if tag and tag[0] != "{":
|
jpayne@68
|
79 if ":" in tag:
|
jpayne@68
|
80 prefix, uri = tag.split(":", 1)
|
jpayne@68
|
81 try:
|
jpayne@68
|
82 if not namespaces:
|
jpayne@68
|
83 raise KeyError
|
jpayne@68
|
84 yield ttype, "{%s}%s" % (namespaces[prefix], uri)
|
jpayne@68
|
85 except KeyError:
|
jpayne@68
|
86 raise SyntaxError("prefix %r not found in prefix map" % prefix) from None
|
jpayne@68
|
87 elif default_namespace and not parsing_attribute:
|
jpayne@68
|
88 yield ttype, "{%s}%s" % (default_namespace, tag)
|
jpayne@68
|
89 else:
|
jpayne@68
|
90 yield token
|
jpayne@68
|
91 parsing_attribute = False
|
jpayne@68
|
92 else:
|
jpayne@68
|
93 yield token
|
jpayne@68
|
94 parsing_attribute = ttype == '@'
|
jpayne@68
|
95
|
jpayne@68
|
96
|
jpayne@68
|
97 def get_parent_map(context):
|
jpayne@68
|
98 parent_map = context.parent_map
|
jpayne@68
|
99 if parent_map is None:
|
jpayne@68
|
100 context.parent_map = parent_map = {}
|
jpayne@68
|
101 for p in context.root.iter():
|
jpayne@68
|
102 for e in p:
|
jpayne@68
|
103 parent_map[e] = p
|
jpayne@68
|
104 return parent_map
|
jpayne@68
|
105
|
jpayne@68
|
106
|
jpayne@68
|
107 def _is_wildcard_tag(tag):
|
jpayne@68
|
108 return tag[:3] == '{*}' or tag[-2:] == '}*'
|
jpayne@68
|
109
|
jpayne@68
|
110
|
jpayne@68
|
111 def _prepare_tag(tag):
|
jpayne@68
|
112 _isinstance, _str = isinstance, str
|
jpayne@68
|
113 if tag == '{*}*':
|
jpayne@68
|
114 # Same as '*', but no comments or processing instructions.
|
jpayne@68
|
115 # It can be a surprise that '*' includes those, but there is no
|
jpayne@68
|
116 # justification for '{*}*' doing the same.
|
jpayne@68
|
117 def select(context, result):
|
jpayne@68
|
118 for elem in result:
|
jpayne@68
|
119 if _isinstance(elem.tag, _str):
|
jpayne@68
|
120 yield elem
|
jpayne@68
|
121 elif tag == '{}*':
|
jpayne@68
|
122 # Any tag that is not in a namespace.
|
jpayne@68
|
123 def select(context, result):
|
jpayne@68
|
124 for elem in result:
|
jpayne@68
|
125 el_tag = elem.tag
|
jpayne@68
|
126 if _isinstance(el_tag, _str) and el_tag[0] != '{':
|
jpayne@68
|
127 yield elem
|
jpayne@68
|
128 elif tag[:3] == '{*}':
|
jpayne@68
|
129 # The tag in any (or no) namespace.
|
jpayne@68
|
130 suffix = tag[2:] # '}name'
|
jpayne@68
|
131 no_ns = slice(-len(suffix), None)
|
jpayne@68
|
132 tag = tag[3:]
|
jpayne@68
|
133 def select(context, result):
|
jpayne@68
|
134 for elem in result:
|
jpayne@68
|
135 el_tag = elem.tag
|
jpayne@68
|
136 if el_tag == tag or _isinstance(el_tag, _str) and el_tag[no_ns] == suffix:
|
jpayne@68
|
137 yield elem
|
jpayne@68
|
138 elif tag[-2:] == '}*':
|
jpayne@68
|
139 # Any tag in the given namespace.
|
jpayne@68
|
140 ns = tag[:-1]
|
jpayne@68
|
141 ns_only = slice(None, len(ns))
|
jpayne@68
|
142 def select(context, result):
|
jpayne@68
|
143 for elem in result:
|
jpayne@68
|
144 el_tag = elem.tag
|
jpayne@68
|
145 if _isinstance(el_tag, _str) and el_tag[ns_only] == ns:
|
jpayne@68
|
146 yield elem
|
jpayne@68
|
147 else:
|
jpayne@68
|
148 raise RuntimeError(f"internal parser error, got {tag}")
|
jpayne@68
|
149 return select
|
jpayne@68
|
150
|
jpayne@68
|
151
|
jpayne@68
|
152 def prepare_child(next, token):
|
jpayne@68
|
153 tag = token[1]
|
jpayne@68
|
154 if _is_wildcard_tag(tag):
|
jpayne@68
|
155 select_tag = _prepare_tag(tag)
|
jpayne@68
|
156 def select(context, result):
|
jpayne@68
|
157 def select_child(result):
|
jpayne@68
|
158 for elem in result:
|
jpayne@68
|
159 yield from elem
|
jpayne@68
|
160 return select_tag(context, select_child(result))
|
jpayne@68
|
161 else:
|
jpayne@68
|
162 if tag[:2] == '{}':
|
jpayne@68
|
163 tag = tag[2:] # '{}tag' == 'tag'
|
jpayne@68
|
164 def select(context, result):
|
jpayne@68
|
165 for elem in result:
|
jpayne@68
|
166 for e in elem:
|
jpayne@68
|
167 if e.tag == tag:
|
jpayne@68
|
168 yield e
|
jpayne@68
|
169 return select
|
jpayne@68
|
170
|
jpayne@68
|
171 def prepare_star(next, token):
|
jpayne@68
|
172 def select(context, result):
|
jpayne@68
|
173 for elem in result:
|
jpayne@68
|
174 yield from elem
|
jpayne@68
|
175 return select
|
jpayne@68
|
176
|
jpayne@68
|
177 def prepare_self(next, token):
|
jpayne@68
|
178 def select(context, result):
|
jpayne@68
|
179 yield from result
|
jpayne@68
|
180 return select
|
jpayne@68
|
181
|
jpayne@68
|
182 def prepare_descendant(next, token):
|
jpayne@68
|
183 try:
|
jpayne@68
|
184 token = next()
|
jpayne@68
|
185 except StopIteration:
|
jpayne@68
|
186 return
|
jpayne@68
|
187 if token[0] == "*":
|
jpayne@68
|
188 tag = "*"
|
jpayne@68
|
189 elif not token[0]:
|
jpayne@68
|
190 tag = token[1]
|
jpayne@68
|
191 else:
|
jpayne@68
|
192 raise SyntaxError("invalid descendant")
|
jpayne@68
|
193
|
jpayne@68
|
194 if _is_wildcard_tag(tag):
|
jpayne@68
|
195 select_tag = _prepare_tag(tag)
|
jpayne@68
|
196 def select(context, result):
|
jpayne@68
|
197 def select_child(result):
|
jpayne@68
|
198 for elem in result:
|
jpayne@68
|
199 for e in elem.iter():
|
jpayne@68
|
200 if e is not elem:
|
jpayne@68
|
201 yield e
|
jpayne@68
|
202 return select_tag(context, select_child(result))
|
jpayne@68
|
203 else:
|
jpayne@68
|
204 if tag[:2] == '{}':
|
jpayne@68
|
205 tag = tag[2:] # '{}tag' == 'tag'
|
jpayne@68
|
206 def select(context, result):
|
jpayne@68
|
207 for elem in result:
|
jpayne@68
|
208 for e in elem.iter(tag):
|
jpayne@68
|
209 if e is not elem:
|
jpayne@68
|
210 yield e
|
jpayne@68
|
211 return select
|
jpayne@68
|
212
|
jpayne@68
|
213 def prepare_parent(next, token):
|
jpayne@68
|
214 def select(context, result):
|
jpayne@68
|
215 # FIXME: raise error if .. is applied at toplevel?
|
jpayne@68
|
216 parent_map = get_parent_map(context)
|
jpayne@68
|
217 result_map = {}
|
jpayne@68
|
218 for elem in result:
|
jpayne@68
|
219 if elem in parent_map:
|
jpayne@68
|
220 parent = parent_map[elem]
|
jpayne@68
|
221 if parent not in result_map:
|
jpayne@68
|
222 result_map[parent] = None
|
jpayne@68
|
223 yield parent
|
jpayne@68
|
224 return select
|
jpayne@68
|
225
|
jpayne@68
|
226 def prepare_predicate(next, token):
|
jpayne@68
|
227 # FIXME: replace with real parser!!! refs:
|
jpayne@68
|
228 # http://effbot.org/zone/simple-iterator-parser.htm
|
jpayne@68
|
229 # http://javascript.crockford.com/tdop/tdop.html
|
jpayne@68
|
230 signature = []
|
jpayne@68
|
231 predicate = []
|
jpayne@68
|
232 while 1:
|
jpayne@68
|
233 try:
|
jpayne@68
|
234 token = next()
|
jpayne@68
|
235 except StopIteration:
|
jpayne@68
|
236 return
|
jpayne@68
|
237 if token[0] == "]":
|
jpayne@68
|
238 break
|
jpayne@68
|
239 if token == ('', ''):
|
jpayne@68
|
240 # ignore whitespace
|
jpayne@68
|
241 continue
|
jpayne@68
|
242 if token[0] and token[0][:1] in "'\"":
|
jpayne@68
|
243 token = "'", token[0][1:-1]
|
jpayne@68
|
244 signature.append(token[0] or "-")
|
jpayne@68
|
245 predicate.append(token[1])
|
jpayne@68
|
246 signature = "".join(signature)
|
jpayne@68
|
247 # use signature to determine predicate type
|
jpayne@68
|
248 if signature == "@-":
|
jpayne@68
|
249 # [@attribute] predicate
|
jpayne@68
|
250 key = predicate[1]
|
jpayne@68
|
251 def select(context, result):
|
jpayne@68
|
252 for elem in result:
|
jpayne@68
|
253 if elem.get(key) is not None:
|
jpayne@68
|
254 yield elem
|
jpayne@68
|
255 return select
|
jpayne@68
|
256 if signature == "@-='":
|
jpayne@68
|
257 # [@attribute='value']
|
jpayne@68
|
258 key = predicate[1]
|
jpayne@68
|
259 value = predicate[-1]
|
jpayne@68
|
260 def select(context, result):
|
jpayne@68
|
261 for elem in result:
|
jpayne@68
|
262 if elem.get(key) == value:
|
jpayne@68
|
263 yield elem
|
jpayne@68
|
264 return select
|
jpayne@68
|
265 if signature == "-" and not re.match(r"\-?\d+$", predicate[0]):
|
jpayne@68
|
266 # [tag]
|
jpayne@68
|
267 tag = predicate[0]
|
jpayne@68
|
268 def select(context, result):
|
jpayne@68
|
269 for elem in result:
|
jpayne@68
|
270 if elem.find(tag) is not None:
|
jpayne@68
|
271 yield elem
|
jpayne@68
|
272 return select
|
jpayne@68
|
273 if signature == ".='" or (signature == "-='" and not re.match(r"\-?\d+$", predicate[0])):
|
jpayne@68
|
274 # [.='value'] or [tag='value']
|
jpayne@68
|
275 tag = predicate[0]
|
jpayne@68
|
276 value = predicate[-1]
|
jpayne@68
|
277 if tag:
|
jpayne@68
|
278 def select(context, result):
|
jpayne@68
|
279 for elem in result:
|
jpayne@68
|
280 for e in elem.findall(tag):
|
jpayne@68
|
281 if "".join(e.itertext()) == value:
|
jpayne@68
|
282 yield elem
|
jpayne@68
|
283 break
|
jpayne@68
|
284 else:
|
jpayne@68
|
285 def select(context, result):
|
jpayne@68
|
286 for elem in result:
|
jpayne@68
|
287 if "".join(elem.itertext()) == value:
|
jpayne@68
|
288 yield elem
|
jpayne@68
|
289 return select
|
jpayne@68
|
290 if signature == "-" or signature == "-()" or signature == "-()-":
|
jpayne@68
|
291 # [index] or [last()] or [last()-index]
|
jpayne@68
|
292 if signature == "-":
|
jpayne@68
|
293 # [index]
|
jpayne@68
|
294 index = int(predicate[0]) - 1
|
jpayne@68
|
295 if index < 0:
|
jpayne@68
|
296 raise SyntaxError("XPath position >= 1 expected")
|
jpayne@68
|
297 else:
|
jpayne@68
|
298 if predicate[0] != "last":
|
jpayne@68
|
299 raise SyntaxError("unsupported function")
|
jpayne@68
|
300 if signature == "-()-":
|
jpayne@68
|
301 try:
|
jpayne@68
|
302 index = int(predicate[2]) - 1
|
jpayne@68
|
303 except ValueError:
|
jpayne@68
|
304 raise SyntaxError("unsupported expression")
|
jpayne@68
|
305 if index > -2:
|
jpayne@68
|
306 raise SyntaxError("XPath offset from last() must be negative")
|
jpayne@68
|
307 else:
|
jpayne@68
|
308 index = -1
|
jpayne@68
|
309 def select(context, result):
|
jpayne@68
|
310 parent_map = get_parent_map(context)
|
jpayne@68
|
311 for elem in result:
|
jpayne@68
|
312 try:
|
jpayne@68
|
313 parent = parent_map[elem]
|
jpayne@68
|
314 # FIXME: what if the selector is "*" ?
|
jpayne@68
|
315 elems = list(parent.findall(elem.tag))
|
jpayne@68
|
316 if elems[index] is elem:
|
jpayne@68
|
317 yield elem
|
jpayne@68
|
318 except (IndexError, KeyError):
|
jpayne@68
|
319 pass
|
jpayne@68
|
320 return select
|
jpayne@68
|
321 raise SyntaxError("invalid predicate")
|
jpayne@68
|
322
|
jpayne@68
|
323 ops = {
|
jpayne@68
|
324 "": prepare_child,
|
jpayne@68
|
325 "*": prepare_star,
|
jpayne@68
|
326 ".": prepare_self,
|
jpayne@68
|
327 "..": prepare_parent,
|
jpayne@68
|
328 "//": prepare_descendant,
|
jpayne@68
|
329 "[": prepare_predicate,
|
jpayne@68
|
330 }
|
jpayne@68
|
331
|
jpayne@68
|
332 _cache = {}
|
jpayne@68
|
333
|
jpayne@68
|
334 class _SelectorContext:
|
jpayne@68
|
335 parent_map = None
|
jpayne@68
|
336 def __init__(self, root):
|
jpayne@68
|
337 self.root = root
|
jpayne@68
|
338
|
jpayne@68
|
339 # --------------------------------------------------------------------
|
jpayne@68
|
340
|
jpayne@68
|
341 ##
|
jpayne@68
|
342 # Generate all matching objects.
|
jpayne@68
|
343
|
jpayne@68
|
344 def iterfind(elem, path, namespaces=None):
|
jpayne@68
|
345 # compile selector pattern
|
jpayne@68
|
346 if path[-1:] == "/":
|
jpayne@68
|
347 path = path + "*" # implicit all (FIXME: keep this?)
|
jpayne@68
|
348
|
jpayne@68
|
349 cache_key = (path,)
|
jpayne@68
|
350 if namespaces:
|
jpayne@68
|
351 cache_key += tuple(sorted(namespaces.items()))
|
jpayne@68
|
352
|
jpayne@68
|
353 try:
|
jpayne@68
|
354 selector = _cache[cache_key]
|
jpayne@68
|
355 except KeyError:
|
jpayne@68
|
356 if len(_cache) > 100:
|
jpayne@68
|
357 _cache.clear()
|
jpayne@68
|
358 if path[:1] == "/":
|
jpayne@68
|
359 raise SyntaxError("cannot use absolute path on element")
|
jpayne@68
|
360 next = iter(xpath_tokenizer(path, namespaces)).__next__
|
jpayne@68
|
361 try:
|
jpayne@68
|
362 token = next()
|
jpayne@68
|
363 except StopIteration:
|
jpayne@68
|
364 return
|
jpayne@68
|
365 selector = []
|
jpayne@68
|
366 while 1:
|
jpayne@68
|
367 try:
|
jpayne@68
|
368 selector.append(ops[token[0]](next, token))
|
jpayne@68
|
369 except StopIteration:
|
jpayne@68
|
370 raise SyntaxError("invalid path") from None
|
jpayne@68
|
371 try:
|
jpayne@68
|
372 token = next()
|
jpayne@68
|
373 if token[0] == "/":
|
jpayne@68
|
374 token = next()
|
jpayne@68
|
375 except StopIteration:
|
jpayne@68
|
376 break
|
jpayne@68
|
377 _cache[cache_key] = selector
|
jpayne@68
|
378 # execute selector pattern
|
jpayne@68
|
379 result = [elem]
|
jpayne@68
|
380 context = _SelectorContext(elem)
|
jpayne@68
|
381 for select in selector:
|
jpayne@68
|
382 result = select(context, result)
|
jpayne@68
|
383 return result
|
jpayne@68
|
384
|
jpayne@68
|
385 ##
|
jpayne@68
|
386 # Find first matching object.
|
jpayne@68
|
387
|
jpayne@68
|
388 def find(elem, path, namespaces=None):
|
jpayne@68
|
389 return next(iterfind(elem, path, namespaces), None)
|
jpayne@68
|
390
|
jpayne@68
|
391 ##
|
jpayne@68
|
392 # Find all matching objects.
|
jpayne@68
|
393
|
jpayne@68
|
394 def findall(elem, path, namespaces=None):
|
jpayne@68
|
395 return list(iterfind(elem, path, namespaces))
|
jpayne@68
|
396
|
jpayne@68
|
397 ##
|
jpayne@68
|
398 # Find text for first matching object.
|
jpayne@68
|
399
|
jpayne@68
|
400 def findtext(elem, path, default=None, namespaces=None):
|
jpayne@68
|
401 try:
|
jpayne@68
|
402 elem = next(iterfind(elem, path, namespaces))
|
jpayne@68
|
403 return elem.text or ""
|
jpayne@68
|
404 except StopIteration:
|
jpayne@68
|
405 return default
|