[TYPEDEF] revert change that broke build
[gnome.gobject-introspection] / giscanner / minixpath.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Colin Walters
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 #
20
21 from .girparser import C_NS, GLIB_NS
22 from .girparser import _corens
23
24 _nsmap = {'c': C_NS,
25           'glib': GLIB_NS}
26
27
28 def myxpath(node, expr):
29     """I Can't Believe It's Not XPath!"""
30     elts = expr.split('/')
31     curnode = node
32     for elt in elts:
33         if elt == '':
34             continue
35         try:
36             (elt, qual) = elt.split('[', 1)
37             qual = qual[1:-1]
38             pairs = [x.split('=', 1) for x in qual.split(',')]
39             exp_attrs = [(x[0], x[1][1:-1]) for x in pairs]
40         except ValueError, e:
41             (elt, exp_attrs) = (elt, [])
42         try:
43             (ns, elt) = elt.split(':', 1)
44             ns = _nsmap[ns]
45             elt = '{%s}%s' % (ns, elt)
46         except ValueError, e:
47             elt = _corens(elt)
48         curnodes = curnode.findall(elt)
49         if not curnodes:
50             return None
51         found = True
52         #print "LOOKING %d nodes" % (len(curnodes), )
53         for node in curnodes:
54             #print "LOOKING: %r expecting: %r attrib: %r" \
55             #    % (node, exp_attrs, node.attrib)
56             passes = True
57             for name, val in exp_attrs:
58                 a = node.attrib.get(name)
59                 if not a or a != val:
60                     #print "ATTR FAIL: %r=%r" % (val, a)
61                     passes = False
62                     break
63             if passes:
64                 found = True
65                 #print 'ATTR PASS: %r' % (node, )
66                 curnode = node
67                 break
68             found = False
69         if not found:
70             return None
71     return curnode
72
73
74 def xpath_assert(node, path, attribs=[]):
75     elt = myxpath(node, path)
76     if elt is None:
77         raise AssertionError("Failed to find %r" % (path, ))
78     for (name, expvalue) in attribs:
79         value = elt.attrib.get(name)
80         if not value:
81             raise AssertionError("Failed to find attibute %r" +
82                                  "in node %r" % (name, elt, ))
83         if value != expvalue:
84             raise AssertionError("Attibute %r in node %r has " +
85                                  "unexpected value %r" % (name, elt, expvalue))