GNOME Bugzilla – Bug 554857
Add a better XPath engine on top ET
Last modified: 2015-02-07 16:48:33 UTC
Our current XPath engine (minixpath) does not support attributes. ElementTree 1.3 supports attributes, unfortunately it's only available in Python 2.6 or newer. Until we can depend on Python 2.6 we should just include its xpath implementation in our tree. The implementation can be found here, it's one file and self contained: http://svn.effbot.org/public/elementtree-1.3/elementtree/ElementPath.py
Created attachment 120138 [details] [review] use elementpath
Comment on attachment 120138 [details] [review] use elementpath >diff --git a/tests/Makefile.am b/tests/Makefile.am >+ @find $(top_srcdir)/giscanner -name \*.py | grep -v ElementPath | sort | uniq | xargs $(PYTHON) $(top_srcdir)/misc >+ @find $(top_srcdir)/giscanner -name \*.py | grep -v ElementPath | sort | uniq | xargs $(PYTHON) $(top_srcdir)/misc I'd prefer to just make ElementPath.py pep8/pyflakes error free instead. >+import giscanner.ElementPath as ElementPath This is the same as, which is easier to read: from giscanner import ElementPath A comment would probably make sense here too: # Import a local copy of ElementPath.py from ElementTree 1.3 which # supports xpath attribute matching. Remove this when we can # add a hard dependency on Python 2.6. >+def xpath_assert(node, path, attribs=[]): attribs=[] is always a bug. Since it'll only be created once. Never initialize default arguments to mutable types, instead just set it to None and conditionally create a list. Also, judging by the code below it should probably be an empty dictionary and not a list. >+ elt = ElementPath.find(node, path) >+ if elt is None: >+ raise AssertionError("Failed to find %r" % (path, )) >+ for (name, expvalue) in attribs: >+ value = elt.attrib.get(name) >+ if not value: >+ raise AssertionError("Failed to find attibute %r" + >+ "in node %r" % (name, elt, )) >+ if value != expvalue: >+ raise AssertionError("Attibute %r in node %r has " + >+ "unexpected value %r" % (name, elt, expvalue)) s/attrbute/attribute/ Don't you need to update any callsites for xpath_assert?
This one turns out to be harder than it looked, or at least more annoying because we'll have to prefix every element in the parsed xpath expression with {http://www.gtk.org/introspection/core/1.0} which is a lot less convenient. Let's punt on this for now - I think ElementTree needs work to be convenient to use with XML namespaces.
Maybe we could add a layer on top of ElementTree's xpath engine to make the syntax for specifying xml namespaces a bit nicer. For instance, replacing {C} with {http://www.gtk.org/introspection/core/1.0}.
We're not using xpath any more, so I'm closing this.
[Mass-moving gobject-introspection tickets to its own Bugzilla product - see bug 708029. Mass-filter your bugmail for this message: introspection20150207 ]