summaryrefslogtreecommitdiff
path: root/tools/glib-errors-check-gen.py
blob: 553fc9cafdf9c04fa8ec43d7b5100685707ac2aa (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
#!/usr/bin/python

import sys
import xml.dom.minidom

from libglibcodegen import NS_TP, 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 __call__(self):

        print '{'
        print '  GEnumClass *klass;'
        print '  GEnumValue *value_by_name;'
        print '  GEnumValue *value_by_nick;'
        print ''
        print '  g_type_init ();'
        print '  klass = g_type_class_ref (TP_TYPE_ERROR);'

        for error in self.errors.getElementsByTagNameNS(NS_TP, 'error'):
            ns = error.parentNode.getAttribute('namespace')
            nick = error.getAttribute('name').replace(' ', '')
            enum = ('TP_ERROR_' +
                    error.getAttribute('name').replace(' ', '_').replace('.', '_').upper())
            s = ('TP_ERROR_STR_' +
                 error.getAttribute('name').replace(' ', '_').replace('.', '_').upper())

            print ''
            print '  /* %s.%s */' % (ns, nick)
            print ('  value_by_name = g_enum_get_value_by_name (klass, "%s");'
                    % enum)
            print ('  value_by_nick = g_enum_get_value_by_nick (klass, "%s");'
                    % nick)
            print ('  g_assert (value_by_name != NULL);')
            print ('  g_assert (value_by_nick != NULL);')
            print ('  g_assert_cmpint (value_by_name->value, ==, %s);'
                    % enum)
            print ('  g_assert_cmpint (value_by_nick->value, ==, %s);'
                    % enum)
            print ('  g_assert_cmpstr (value_by_name->value_name, ==, "%s");'
                    % enum)
            print ('  g_assert_cmpstr (value_by_nick->value_name, ==, "%s");'
                    % enum)
            print ('  g_assert_cmpstr (value_by_name->value_nick, ==, "%s");'
                    % nick)
            print ('  g_assert_cmpstr (value_by_nick->value_nick, ==, "%s");'
                    % nick)
            print ('  g_assert_cmpstr (%s, ==, TP_ERROR_PREFIX ".%s");'
                    % (s, nick))

        print '}'

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