File: glib-interfaces-gen.py

package info (click to toggle)
telepathy-logger 0.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 4,352 kB
  • sloc: ansic: 16,018; sh: 11,552; python: 2,960; makefile: 635; xml: 185
file content (207 lines) | stat: -rw-r--r-- 6,519 bytes parent folder | download | duplicates (15)
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
#!/usr/bin/python

from sys import argv, stdout, stderr
import xml.dom.minidom

from libtpcodegen import file_set_contents, u
from libglibcodegen import NS_TP, get_docstring, \
        get_descendant_text, get_by_path

class Generator(object):
    def __init__(self, prefix, implfile, declfile, dom):
        self.prefix = prefix + '_'

        assert declfile.endswith('.h')
        docfile = declfile[:-2] + '-gtk-doc.h'

        self.implfile = implfile
        self.declfile = declfile
        self.docfile = docfile

        self.impls = []
        self.decls = []
        self.docs = []
        self.spec = get_by_path(dom, "spec")[0]

    def h(self, code):
        self.decls.append(code)

    def c(self, code):
        self.impls.append(code)

    def d(self, code):
        self.docs.append(code)

    def __call__(self):
        for f in self.h, self.c:
            self.do_header(f)
        self.do_body()

        file_set_contents(self.implfile, u('').join(self.impls).encode('utf-8'))
        file_set_contents(self.declfile, u('').join(self.decls).encode('utf-8'))
        file_set_contents(self.docfile, u('').join(self.docs).encode('utf-8'))

    # Header
    def do_header(self, f):
        f('/* Generated from: ')
        f(get_descendant_text(get_by_path(self.spec, 'title')))
        version = get_by_path(self.spec, "version")
        if version:
            f(' version ' + get_descendant_text(version))
        f('\n\n')
        for copyright in get_by_path(self.spec, 'copyright'):
            f(get_descendant_text(copyright))
            f('\n')
        f('\n')
        f(get_descendant_text(get_by_path(self.spec, 'license')))
        f(get_descendant_text(get_by_path(self.spec, 'docstring')))
        f("""
 */

#include <glib.h>
""")

    # Body
    def do_body(self):
        for iface in self.spec.getElementsByTagName('interface'):
            self.do_iface(iface)

    def do_iface(self, iface):
        parent_name = get_by_path(iface, '../@name')
        self.d("""\
/**
 * %(IFACE_DEFINE)s:
 *
 * The interface name "%(name)s"
 */
""" % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
            parent_name).upper().replace('/', ''),
       'name' : iface.getAttribute('name')})

        self.h("""
#define %(IFACE_DEFINE)s \\
"%(name)s"
""" % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
            parent_name).upper().replace('/', ''),
       'name' : iface.getAttribute('name')})

        self.d("""
/**
 * %(IFACE_QUARK_DEFINE)s:
 *
 * Expands to a call to a function that returns a quark for the interface \
name "%(name)s"
 */
""" % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
            parent_name).upper().replace('/', ''),
       'iface_quark_func' : (self.prefix + 'iface_quark_' + \
            parent_name).lower().replace('/', ''),
       'name' : iface.getAttribute('name')})

        self.h("""
#define %(IFACE_QUARK_DEFINE)s \\
  (%(iface_quark_func)s ())

GQuark %(iface_quark_func)s (void);

""" % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
            parent_name).upper().replace('/', ''),
       'iface_quark_func' : (self.prefix + 'iface_quark_' + \
            parent_name).lower().replace('/', ''),
       'name' : iface.getAttribute('name')})

        self.c("""\
GQuark
%(iface_quark_func)s (void)
{
  static GQuark quark = 0;

  if (G_UNLIKELY (quark == 0))
    {
      quark = g_quark_from_static_string ("%(name)s");
    }

  return quark;
}

""" % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
            parent_name).lower().replace('/', ''),
       'name' : iface.getAttribute('name')})

        for prop in iface.getElementsByTagNameNS(None, 'property'):
            self.d("""
/**
 * %(IFACE_PREFIX)s_%(PROP_UC)s:
 *
 * The fully-qualified property name "%(name)s.%(prop)s"
 */
""" % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
                parent_name).upper().replace('/', ''),
           'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
           'name' : iface.getAttribute('name'),
           'prop' : prop.getAttribute('name'),
           })

            self.h("""
#define %(IFACE_PREFIX)s_%(PROP_UC)s \\
"%(name)s.%(prop)s"
""" % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
                parent_name).upper().replace('/', ''),
           'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
           'name' : iface.getAttribute('name'),
           'prop' : prop.getAttribute('name'),
           })


        for prop in iface.getElementsByTagNameNS(NS_TP, 'contact-attribute'):
            self.d("""
/**
 * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
 *
 * The fully-qualified contact attribute token name "%(name)s/%(prop)s"
 */
""" % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
                parent_name).upper().replace('/', ''),
           'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
           'name' : iface.getAttribute('name'),
           'prop' : prop.getAttribute('name'),
           })

            self.h("""
#define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
"%(name)s/%(prop)s"
""" % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
                parent_name).upper().replace('/', ''),
           'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
           'name' : iface.getAttribute('name'),
           'prop' : prop.getAttribute('name'),
           })

        for prop in iface.getElementsByTagNameNS(NS_TP, 'hct'):
            if (prop.getAttribute('is-family') != "yes"):
                self.d("""
/**
 * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
 *
 * The fully-qualified capability token name "%(name)s/%(prop)s"
 */
""" % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
                parent_name).upper().replace('/', ''),
           'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
           'name' : iface.getAttribute('name'),
           'prop' : prop.getAttribute('name'),
           })

                self.h("""
#define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
"%(name)s/%(prop)s"
""" % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
                parent_name).upper().replace('/', ''),
           'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
           'name' : iface.getAttribute('name'),
           'prop' : prop.getAttribute('name'),
           })

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