summaryrefslogtreecommitdiff
path: root/tools/glib-errors-enum-header-gen.py
blob: 64939b4142b88051b223c3f8cd5897d7207587a0 (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
#!/usr/bin/python

import sys
import xml.dom.minidom

from libglibcodegen import NS_TP, camelcase_to_upper, get_docstring, \
        get_descendant_text

class Generator(object):
    def __init__(self, dom):
        self.dom = dom
        self.errors = self.dom.getElementsByTagNameNS(NS_TP, 'errors')[0]

    def do_header(self):
        print '/* Generated from the Telepathy spec\n'
        copyrights = self.errors.getElementsByTagNameNS(NS_TP, 'copyright')
        for copyright in copyrights:
            print get_descendant_text(copyright)
        license = self.errors.getElementsByTagNameNS(NS_TP, 'license')[0]
        print '\n' + get_descendant_text(license) + '\n*/'

    def do_gtkdoc(self):
        for error in self.errors.getElementsByTagNameNS(NS_TP, 'error'):
            ns = error.parentNode.getAttribute('namespace')
            nick = error.getAttribute('name').replace(' ', '')
            enum = 'TP_ERROR_' + camelcase_to_upper(nick.replace('.', ''))
            print ' * @' + enum + ': ' + ns + '.' + nick + ':'
            print ' *     ' + get_docstring(error) + '    '

    def do_enumnames(self):
        for error in self.errors.getElementsByTagNameNS(NS_TP, 'error'):
            nick = error.getAttribute('name').replace(' ', '')
            enum = 'TP_ERROR_' + camelcase_to_upper(nick.replace('.', ''))
            print '    ' + enum + ','

    def do_get_type(self):
        print """
#include <glib-object.h>

G_BEGIN_DECLS

GType tp_error_get_type (void);

/**
 * TP_TYPE_ERROR:
 *
 * The GType of the Telepathy error enumeration.
 */
#define TP_TYPE_ERROR (tp_error_get_type())
"""

    def do_enum(self):
        print """\
/**
 * TpError:"""
        self.do_gtkdoc()
        print """\
 *
 * Enumerated type representing the Telepathy D-Bus errors.
 */
typedef enum {"""
        self.do_enumnames()
        print """\
} TpError;

G_END_DECLS"""

    def __call__(self):
        self.do_header()
        self.do_get_type()
        self.do_enum()

if __name__ == '__main__':
    argv = sys.argv[1:]
    Generator(xml.dom.minidom.parse(argv[0]))()