summaryrefslogtreecommitdiff
path: root/giscanner/girparser.py
blob: 00739ab6b556f0aac0419d1f9871a1d2ea8332e0 (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
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2008  Johan Dahlin
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

from xml.etree.cElementTree import parse

from .glibast import GLibObject

CORE_NS = "http://www.gtk.org/introspection/core/1.0"
GLIB_NS = "http://www.gtk.org/introspection/glib/1.0"

def _corens(tag):
    return '{%s}%s' % (CORE_NS, tag)

def _glibns(tag):
    return '{%s}%s' % (GLIB_NS, tag)


class GIRParser(object):
    def __init__(self, filename):
        self._nodes = []
        self._namespace_name = None

        tree = parse(filename)
        self._parse_api(tree.getroot())

    def _parse_api(self, root):
        assert root.tag == _corens('repository')
        ns = root.find(_corens('namespace'))
        assert ns is not None
        self._namespace_name = ns.attrib['name']
        for child in ns.getchildren():
            if child.tag == _corens('class'):
                self._parse_object(child)
            elif child.tag in [_corens('callback'),
                               _corens('function'),
                               _corens('record'),
                               _corens('enumeration'),
                               _corens('bitfield'),
                               _corens('interface'),
                               _glibns('boxed'),
                               ]:
                continue
            else:
                print 'PARSER: Unhandled %s' % (child.tag,)

    def _parse_object(self, node):
        gobj = GLibObject(node.attrib['name'],
                          node.attrib.get('parent'),
                          node.attrib[_glibns('type-name')],
                          node.attrib[_glibns('get-type')])
        self._nodes.append(gobj)

    def get_namespace_name(self):
        return self._namespace_name

    def get_nodes(self):
        return self._nodes