annotate Lib/xml/etree/ElementPath.py @ 80661:94de77bd0b4b 2.7

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