3 from sys import argv, stdout, stderr
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]
17 for file in self.decls, self.impls:
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")
27 file.write(' version ' + get_descendant_text(version))
29 for copyright in get_by_path(self.spec, 'copyright'):
30 stdout.write(get_descendant_text(copyright))
33 file.write(get_descendant_text(get_by_path(self.spec, 'license')))
34 file.write(get_descendant_text(get_by_path(self.spec, 'docstring')))
42 for iface in self.spec.getElementsByTagName('interface'):
45 def do_iface(self, iface):
46 parent_name = get_by_path(iface, '../@name')
51 * The interface name "%(name)s"
53 #define %(IFACE_DEFINE)s \\
55 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
56 parent_name).upper().replace('/', ''),
57 'name' : iface.getAttribute('name')})
61 * %(IFACE_QUARK_DEFINE)s:
63 * Expands to a call to a function that returns a quark for the interface \
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')})
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");
91 """ % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
92 parent_name).lower().replace('/', ''),
93 'name' : iface.getAttribute('name')})
95 if __name__ == '__main__':
97 Generator(argv[0], argv[1], argv[2], xml.dom.minidom.parse(argv[3]))()