3 from sys
import argv
, stdout
, stderr
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]
22 self
.decls
.write(code
.encode('utf-8'))
25 self
.impls
.write(code
.encode('utf-8'))
28 self
.docs
.write(code
.encode('utf-8'))
31 for f
in self
.h
, self
.c
:
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")
41 f(' version ' + get_descendant_text(version
))
43 for copyright
in get_by_path(self
.spec
, 'copyright'):
44 f(get_descendant_text(copyright
))
47 f(get_descendant_text(get_by_path(self
.spec
, 'license')))
48 f(get_descendant_text(get_by_path(self
.spec
, 'docstring')))
56 for iface
in self
.spec
.getElementsByTagName('interface'):
59 def do_iface(self
, iface
):
60 parent_name
= get_by_path(iface
, '../@name')
65 * The interface name "%(name)s"
67 """ % {'IFACE_DEFINE' : (self
.prefix
+ 'IFACE_' + \
68 parent_name
).upper().replace('/', ''),
69 'name' : iface
.getAttribute('name')})
72 #define %(IFACE_DEFINE)s \\
74 """ % {'IFACE_DEFINE' : (self
.prefix
+ 'IFACE_' + \
75 parent_name
).upper().replace('/', ''),
76 'name' : iface
.getAttribute('name')})
80 * %(IFACE_QUARK_DEFINE)s:
82 * Expands to a call to a function that returns a quark for the interface \
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')})
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')})
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");
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'):
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'),
136 #define %(IFACE_PREFIX)s_%(PROP_UC)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'):
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'),
161 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)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"):
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'),
186 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)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__':
197 Generator(argv
[0], argv
[1], argv
[2], xml
.dom
.minidom
.parse(argv
[3]))()