change default value of 'theme' key to 'Classic'
[empathy-mirror.git] / tools / glib-interfaces-gen.py
blob69c721be36d303b064fe83454b613f262178bcf6
1 #!/usr/bin/python
3 from sys import argv, stdout, stderr
4 import xml.dom.minidom
6 from libglibcodegen import NS_TP, 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 + '_'
13 assert declfile.endswith('.h')
14 docfile = declfile[:-2] + '-gtk-doc.h'
16 self.impls = open(implfile, 'w')
17 self.decls = open(declfile, 'w')
18 self.docs = open(docfile, 'w')
19 self.spec = get_by_path(dom, "spec")[0]
21 def h(self, code):
22 self.decls.write(code.encode('utf-8'))
24 def c(self, code):
25 self.impls.write(code.encode('utf-8'))
27 def d(self, code):
28 self.docs.write(code.encode('utf-8'))
30 def __call__(self):
31 for f in self.h, self.c:
32 self.do_header(f)
33 self.do_body()
35 # Header
36 def do_header(self, f):
37 f('/* Generated from: ')
38 f(get_descendant_text(get_by_path(self.spec, 'title')))
39 version = get_by_path(self.spec, "version")
40 if version:
41 f(' version ' + get_descendant_text(version))
42 f('\n\n')
43 for copyright in get_by_path(self.spec, 'copyright'):
44 f(get_descendant_text(copyright))
45 f('\n')
46 f('\n')
47 f(get_descendant_text(get_by_path(self.spec, 'license')))
48 f(get_descendant_text(get_by_path(self.spec, 'docstring')))
49 f("""
52 """)
54 # Body
55 def do_body(self):
56 for iface in self.spec.getElementsByTagName('interface'):
57 self.do_iface(iface)
59 def do_iface(self, iface):
60 parent_name = get_by_path(iface, '../@name')
61 self.d("""\
62 /**
63 * %(IFACE_DEFINE)s:
65 * The interface name "%(name)s"
67 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
68 parent_name).upper().replace('/', ''),
69 'name' : iface.getAttribute('name')})
71 self.h("""
72 #define %(IFACE_DEFINE)s \\
73 "%(name)s"
74 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
75 parent_name).upper().replace('/', ''),
76 'name' : iface.getAttribute('name')})
78 self.d("""
79 /**
80 * %(IFACE_QUARK_DEFINE)s:
82 * Expands to a call to a function that returns a quark for the interface \
83 name "%(name)s"
85 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
86 parent_name).upper().replace('/', ''),
87 'iface_quark_func' : (self.prefix + 'iface_quark_' + \
88 parent_name).lower().replace('/', ''),
89 'name' : iface.getAttribute('name')})
91 self.h("""
92 #define %(IFACE_QUARK_DEFINE)s \\
93 (%(iface_quark_func)s ())
95 GQuark %(iface_quark_func)s (void);
97 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
98 parent_name).upper().replace('/', ''),
99 'iface_quark_func' : (self.prefix + 'iface_quark_' + \
100 parent_name).lower().replace('/', ''),
101 'name' : iface.getAttribute('name')})
103 self.c("""\
104 GQuark
105 %(iface_quark_func)s (void)
107 static GQuark quark = 0;
109 if (G_UNLIKELY (quark == 0))
111 quark = g_quark_from_static_string ("%(name)s");
114 return quark;
117 """ % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
118 parent_name).lower().replace('/', ''),
119 'name' : iface.getAttribute('name')})
121 for prop in iface.getElementsByTagNameNS(None, 'property'):
122 self.d("""
124 * %(IFACE_PREFIX)s_%(PROP_UC)s:
126 * The fully-qualified property name "%(name)s.%(prop)s"
128 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
129 parent_name).upper().replace('/', ''),
130 'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
131 'name' : iface.getAttribute('name'),
132 'prop' : prop.getAttribute('name'),
135 self.h("""
136 #define %(IFACE_PREFIX)s_%(PROP_UC)s \\
137 "%(name)s.%(prop)s"
138 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
139 parent_name).upper().replace('/', ''),
140 'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
141 'name' : iface.getAttribute('name'),
142 'prop' : prop.getAttribute('name'),
146 for prop in iface.getElementsByTagNameNS(NS_TP, 'contact-attribute'):
147 self.d("""
149 * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
151 * The fully-qualified contact attribute token name "%(name)s/%(prop)s"
153 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
154 parent_name).upper().replace('/', ''),
155 'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
156 'name' : iface.getAttribute('name'),
157 'prop' : prop.getAttribute('name'),
160 self.h("""
161 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
162 "%(name)s/%(prop)s"
163 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
164 parent_name).upper().replace('/', ''),
165 'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
166 'name' : iface.getAttribute('name'),
167 'prop' : prop.getAttribute('name'),
170 for prop in iface.getElementsByTagNameNS(NS_TP, 'hct'):
171 if (prop.getAttribute('is-family') != "yes"):
172 self.d("""
174 * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
176 * The fully-qualified capability token name "%(name)s/%(prop)s"
178 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
179 parent_name).upper().replace('/', ''),
180 'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
181 'name' : iface.getAttribute('name'),
182 'prop' : prop.getAttribute('name'),
185 self.h("""
186 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
187 "%(name)s/%(prop)s"
188 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
189 parent_name).upper().replace('/', ''),
190 'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
191 'name' : iface.getAttribute('name'),
192 'prop' : prop.getAttribute('name'),
195 if __name__ == '__main__':
196 argv = argv[1:]
197 Generator(argv[0], argv[1], argv[2], xml.dom.minidom.parse(argv[3]))()