Tagged for release 2.26.0.
[empathy-mirror.git] / tools / glib-interfaces-gen.py
blob741626ceb0970f4b68efc6f9ea0d99bbc8f3934b
1 #!/usr/bin/python
3 from sys import argv, stdout, stderr
4 import xml.dom.minidom
6 from libglibcodegen import NS_TP, camelcase_to_upper, get_docstring, \
7 get_descendant_text, get_by_path
9 class Generator(object):
10 def __init__(self, prefix, implfile, declfile, dom):
11 self.prefix = prefix + '_'
12 self.impls = open(implfile, 'w')
13 self.decls = open(declfile, 'w')
14 self.spec = get_by_path(dom, "spec")[0]
16 def __call__(self):
17 for file in self.decls, self.impls:
18 self.do_header(file)
19 self.do_body()
21 # Header
22 def do_header(self, file):
23 file.write('/* Generated from: ')
24 file.write(get_descendant_text(get_by_path(self.spec, 'title')))
25 version = get_by_path(self.spec, "version")
26 if version:
27 file.write(' version ' + get_descendant_text(version))
28 file.write('\n\n')
29 for copyright in get_by_path(self.spec, 'copyright'):
30 stdout.write(get_descendant_text(copyright))
31 stdout.write('\n')
32 file.write('\n')
33 file.write(get_descendant_text(get_by_path(self.spec, 'license')))
34 file.write(get_descendant_text(get_by_path(self.spec, 'docstring')))
35 file.write("""
38 """)
40 # Body
41 def do_body(self):
42 for iface in self.spec.getElementsByTagName('interface'):
43 self.do_iface(iface)
45 def do_iface(self, iface):
46 parent_name = get_by_path(iface, '../@name')
47 self.decls.write("""\
48 /**
49 * %(IFACE_DEFINE)s:
51 * The interface name "%(name)s"
53 #define %(IFACE_DEFINE)s \\
54 "%(name)s"
55 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
56 parent_name).upper().replace('/', ''),
57 'name' : iface.getAttribute('name')})
59 self.decls.write("""
60 /**
61 * %(IFACE_QUARK_DEFINE)s:
63 * Expands to a call to a function that returns a quark for the interface \
64 name "%(name)s"
66 #define %(IFACE_QUARK_DEFINE)s \\
67 (%(iface_quark_func)s ())
69 GQuark %(iface_quark_func)s (void);
71 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
72 parent_name).upper().replace('/', ''),
73 'iface_quark_func' : (self.prefix + 'iface_quark_' + \
74 parent_name).lower().replace('/', ''),
75 'name' : iface.getAttribute('name')})
77 self.impls.write("""\
78 GQuark
79 %(iface_quark_func)s (void)
81 static GQuark quark = 0;
83 if (G_UNLIKELY (quark == 0))
85 quark = g_quark_from_static_string ("%(name)s");
88 return quark;
91 """ % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
92 parent_name).lower().replace('/', ''),
93 'name' : iface.getAttribute('name')})
95 if __name__ == '__main__':
96 argv = argv[1:]
97 Generator(argv[0], argv[1], argv[2], xml.dom.minidom.parse(argv[3]))()