summaryrefslogtreecommitdiff
path: root/giscanner/minixpath.py
blob: 5313a86ad0c58d1671cda42a07dec6ae353fe823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2008 Colin Walters
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

from .girparser import C_NS, GLIB_NS
from .girparser import _corens

_nsmap = {'c': C_NS,
          'glib': GLIB_NS}


def myxpath(node, expr):
    """I Can't Believe It's Not XPath!"""
    elts = expr.split('/')
    curnode = node
    for elt in elts:
        if elt == '':
            continue
        try:
            (elt, qual) = elt.split('[', 1)
            qual = qual[1:-1]
            pairs = [x.split('=', 1) for x in qual.split(',')]
            exp_attrs = [(x[0], x[1][1:-1]) for x in pairs]
        except ValueError, e:
            (elt, exp_attrs) = (elt, [])
        try:
            (ns, elt) = elt.split(':', 1)
            ns = _nsmap[ns]
            elt = '{%s}%s' % (ns, elt)
        except ValueError, e:
            elt = _corens(elt)
        curnodes = curnode.findall(elt)
        if not curnodes:
            return None
        found = True
        #print "LOOKING %d nodes" % (len(curnodes), )
        for node in curnodes:
            #print "LOOKING: %r expecting: %r attrib: %r" \
            #    % (node, exp_attrs, node.attrib)
            passes = True
            for name, val in exp_attrs:
                a = node.attrib.get(name)
                if not a or a != val:
                    #print "ATTR FAIL: %r=%r" % (val, a)
                    passes = False
                    break
            if passes:
                found = True
                #print 'ATTR PASS: %r' % (node, )
                curnode = node
                break
            found = False
        if not found:
            return None
    return curnode


def xpath_assert(node, path, attribs=[]):
    elt = myxpath(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))