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