summaryrefslogtreecommitdiff
path: root/giscanner/girparser.py
blob: 27258080d5ba1b362f83325cc2e9abbdfbdbb2a4 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- 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 .ast import (Alias, Array, Callback, Enum, Function, Field, Namespace,
                  Parameter, Property, Return, Union, Struct, Type, Varargs)
from .glibast import (GLibEnum, GLibEnumMember, GLibFlags,
                      GLibInterface, GLibObject, GLibBoxedStruct,
                      GLibBoxedUnion, GLibBoxedOther)

CORE_NS = "http://www.gtk.org/introspection/core/1.0"
C_NS = "http://www.gtk.org/introspection/c/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)


def _cns(tag):
    return '{%s}%s' % (C_NS, tag)


class GIRParser(object):

    def __init__(self, filename, initial_parse=True):
        self._includes = set()
        self._namespace = None
        self._shared_libraries = []
        self._tree = parse(filename)

        if (initial_parse):
            self.parse()

    # Public API

    def parse(self):
        self._includes.clear()
        del self._namespace
        del self._shared_libraries[:]

        self._parse_api(self._tree.getroot())

    def get_namespace(self):
        return self._namespace

    def get_shared_libraries(self):
        return self._shared_libraries

    def get_includes(self):
        return self._includes

    def get_doc(self):
        return self._tree

    # Private

    def _add_node(self, node):
        self._namespace.nodes.append(node)

    def _parse_api(self, root):
        assert root.tag == _corens('repository')
        for node in root.getchildren():
            if node.tag == _corens('include'):
                self._includes.add(node.attrib['name'])
        ns = root.find(_corens('namespace'))
        assert ns is not None
        self._namespace = Namespace(ns.attrib['name'])
        self._shared_libraries.extend(ns.attrib['shared-library'].split(','))
        for child in ns.getchildren():
            self._parse_node(child)

    def _parse_node(self, node):
        if node.tag == _corens('alias'):
            self._add_node(self._parse_alias(node))
        elif node.tag in [_corens('callback')]:
            self._add_node(self._parse_function(node, Callback))
        elif node.tag in [_corens('function')]:
            self._add_node(self._parse_function(node, Function))
        elif node.tag in [_corens('class'),
                          _corens('interface')]:
            self._parse_object_interface(node)
        elif node.tag == _corens('record'):
            self._parse_struct(node)
        elif node.tag == _corens('union'):
            self._parse_union(node)
        elif node.tag == _glibns('boxed'):
            self._parse_boxed(node)
        elif node.tag in [_corens('enumeration'),
                          _corens('bitfield')]:
            self._parse_enumeration_bitfield(node)

    def _parse_alias(self, node):
        return Alias(node.attrib['name'],
                     node.attrib['target'],
                     node.attrib.get(_cns('type')))

    def _parse_object_interface(self, node):
        if node.tag == _corens('interface'):
            klass = GLibInterface
        elif node.tag == _corens('class'):
            klass = GLibObject
        else:
            raise AssertionError(node)

        obj = klass(node.attrib['name'],
                    node.attrib.get('parent'),
                    node.attrib[_glibns('type-name')],
                    node.attrib[_glibns('get-type')],
                    node.attrib.get(_cns('type')))
        for iface in node.findall(_corens('implements')):
            obj.interfaces.append(iface.attrib['name'])
        for method in node.findall(_corens('method')):
            obj.methods.append(self._parse_function(method, Function))
        for ctor in node.findall(_corens('constructor')):
            obj.constructors.append(self._parse_function(ctor, Function))
        for callback in node.findall(_corens('callback')):
            obj.fields.append(self._parse_function(callback, Callback))
        for field in node.findall(_corens('field')):
            obj.fields.append(self._parse_field(field))
        for property in node.findall(_corens('property')):
            obj.properties.append(self._parse_property(property))
        for signal in node.findall(_glibns('signal')):
            obj.signals.append(self._parse_function(signal, Function))
        self._add_node(obj)

    def _parse_function(self, node, klass):
        name = node.attrib['name']
        returnnode = node.find(_corens('return-value'))
        if not returnnode:
            raise ValueError('node %r has no return-value' % (name, ))
        transfer = False
        transfer_param = returnnode.attrib.get('transfer-ownership')
        if transfer_param is not None:
            transfer = (transfer_param == '1')
        retval = Return(self._parse_type(returnnode), transfer)
        parameters_node = node.find(_corens('parameters'))
        parameters = []
        if (parameters_node is not None):
            for paramnode in parameters_node.findall(_corens('parameter')):
                parameters.append(Parameter(paramnode.attrib.get('name'),
                                  self._parse_type(paramnode)))
        if klass is Callback:
            return klass(name, retval, parameters,
                         node.attrib.get(_cns('type')))
        else:
            identifier = node.attrib.get(_cns('identifier'))
            return klass(name, retval, parameters, identifier)

    def _parse_struct(self, node):
        if _glibns('type-name') in node.attrib:
            struct = GLibBoxedStruct(node.attrib['name'],
                                     node.attrib[_glibns('type-name')],
                                     node.attrib[_glibns('get-type')],
                                     node.attrib.get(_cns('type')))
        else:
            struct = Struct(node.attrib['name'],
                            node.attrib.get(_cns('type')))
        for field in node.findall(_corens('field')):
            struct.fields.append(self._parse_field(field))
        for callback in node.findall(_corens('callback')):
            struct.fields.append(self._parse_function(callback, Callback))
        for method in node.findall(_corens('method')):
            struct.fields.append(self._parse_function(method, Function))
        for ctor in node.findall(_corens('constructor')):
            struct.constructors.append(self._parse_function(ctor, Function))
        self._add_node(struct)

    def _parse_union(self, node):
        if _glibns('type-name') in node.attrib:
            struct = GLibBoxedUnion(node.attrib['name'],
                                    node.attrib[_glibns('type-name')],
                                    node.attrib[_glibns('get-type')],
                                    node.attrib.get(_cns('type')))
        else:
            struct = Union(node.attrib['name'],
                           node.attrib.get(_cns('type')))
        for callback in node.findall(_corens('callback')):
            struct.fields.append(self._parse_function(callback, Callback))
        for field in node.findall(_corens('field')):
            struct.fields.append(self._parse_field(field))
        for method in node.findall(_corens('method')):
            struct.fields.append(self._parse_function(method, Function))
        for ctor in node.findall(_corens('constructor')):
            struct.constructors.append(self._parse_function(ctor, Function))
        self._add_node(struct)

    def _parse_type(self, node):
        typenode = node.find(_corens('type'))
        if typenode is not None:
            return Type(typenode.attrib['name'],
                        typenode.attrib.get(_cns('type')))
        typenode = node.find(_corens('array'))
        if typenode is not None:
            return Array(typenode.attrib.get(_cns('type')),
                         self._parse_type(typenode))
        typenode = node.find(_corens('varargs'))
        if typenode is not None:
            return Varargs()
        raise ValueError("Couldn't parse type of node %r; children=%r",
                         node, list(node))

    def _parse_boxed(self, node):
        obj = GLibBoxedOther(node.attrib[_glibns('name')],
                             node.attrib[_glibns('type-name')],
                             node.attrib[_glibns('get-type')])
        for method in node.findall(_corens('method')):
            obj.methods.append(self._parse_function(method, Function))
        for ctor in node.findall(_corens('constructor')):
            obj.constructors.append(self._parse_function(ctor, Function))
        for callback in node.findall(_corens('callback')):
            obj.fields.append(self._parse_function(callback, Callback))
        self._add_node(obj)

    def _parse_field(self, node):
        type_node = self._parse_type(node)
        return Field(node.attrib['name'],
                     type_node,
                     type_node.ctype)

    def _parse_property(self, node):
        type_node = self._parse_type(node)
        return Property(node.attrib['name'],
                        type_node.name,
                        node.attrib.get('readable') != '0',
                        node.attrib.get('writable') == '1',
                        node.attrib.get('construct') == '1',
                        node.attrib.get('construct-only') == '1',
                        type_node.ctype)

    def _parse_member(self, node):
        return GLibEnumMember(node.attrib['name'],
                              node.attrib['value'],
                              node.attrib.get(_cns('identifier')),
                              node.attrib.get(_glibns('nick')))

    def _parse_enumeration_bitfield(self, node):
        name = node.attrib.get('name')
        ctype = node.attrib.get(_cns('type'))
        get_type = node.attrib.get(_glibns('get-type'))
        type_name = node.attrib.get(_glibns('type-name'))
        if get_type:
            if node.tag == _corens('bitfield'):
                klass = GLibFlags
            else:
                klass = GLibEnum
        else:
            klass = Enum
            type_name = ctype
        members = []
        for member in node.findall(_corens('member')):
            members.append(self._parse_member(member))

        if klass is Enum:
            obj = klass(name, type_name, members)
        else:
            obj = klass(name, type_name, members, get_type)
            obj.ctype = ctype
        self._add_node(obj)