3 from sys
import argv
, stdout
, stderr
6 from libtpcodegen
import file_set_contents
, u
7 from libglibcodegen
import NS_TP
, get_docstring
, \
8 get_descendant_text
, get_by_path
10 class Generator(object):
11 def __init__(self
, prefix
, implfile
, declfile
, dom
):
12 self
.prefix
= prefix
+ '_'
14 assert declfile
.endswith('.h')
15 docfile
= declfile
[:-2] + '-gtk-doc.h'
17 self
.implfile
= implfile
18 self
.declfile
= declfile
19 self
.docfile
= docfile
24 self
.spec
= get_by_path(dom
, "spec")[0]
27 self
.decls
.append(code
)
30 self
.impls
.append(code
)
33 self
.docs
.append(code
)
36 for f
in self
.h
, self
.c
:
40 file_set_contents(self
.implfile
, u('').join(self
.impls
).encode('utf-8'))
41 file_set_contents(self
.declfile
, u('').join(self
.decls
).encode('utf-8'))
42 file_set_contents(self
.docfile
, u('').join(self
.docs
).encode('utf-8'))
45 def do_header(self
, f
):
46 f('/* Generated from: ')
47 f(get_descendant_text(get_by_path(self
.spec
, 'title')))
48 version
= get_by_path(self
.spec
, "version")
50 f(' version ' + get_descendant_text(version
))
52 for copyright
in get_by_path(self
.spec
, 'copyright'):
53 f(get_descendant_text(copyright
))
56 f(get_descendant_text(get_by_path(self
.spec
, 'license')))
57 f(get_descendant_text(get_by_path(self
.spec
, 'docstring')))
66 for iface
in self
.spec
.getElementsByTagName('interface'):
69 def do_iface(self
, iface
):
70 parent_name
= get_by_path(iface
, '../@name')
75 * The interface name "%(name)s"
77 """ % {'IFACE_DEFINE' : (self
.prefix
+ 'IFACE_' + \
78 parent_name
).upper().replace('/', ''),
79 'name' : iface
.getAttribute('name')})
82 #define %(IFACE_DEFINE)s \\
84 """ % {'IFACE_DEFINE' : (self
.prefix
+ 'IFACE_' + \
85 parent_name
).upper().replace('/', ''),
86 'name' : iface
.getAttribute('name')})
90 * %(IFACE_QUARK_DEFINE)s:
92 * Expands to a call to a function that returns a quark for the interface \
95 """ % {'IFACE_QUARK_DEFINE' : (self
.prefix
+ 'IFACE_QUARK_' + \
96 parent_name
).upper().replace('/', ''),
97 'iface_quark_func' : (self
.prefix
+ 'iface_quark_' + \
98 parent_name
).lower().replace('/', ''),
99 'name' : iface
.getAttribute('name')})
102 #define %(IFACE_QUARK_DEFINE)s \\
103 (%(iface_quark_func)s ())
105 GQuark %(iface_quark_func)s (void);
107 """ % {'IFACE_QUARK_DEFINE' : (self
.prefix
+ 'IFACE_QUARK_' + \
108 parent_name
).upper().replace('/', ''),
109 'iface_quark_func' : (self
.prefix
+ 'iface_quark_' + \
110 parent_name
).lower().replace('/', ''),
111 'name' : iface
.getAttribute('name')})
115 %(iface_quark_func)s (void)
117 static GQuark quark = 0;
119 if (G_UNLIKELY (quark == 0))
121 quark = g_quark_from_static_string ("%(name)s");
127 """ % {'iface_quark_func' : (self
.prefix
+ 'iface_quark_' + \
128 parent_name
).lower().replace('/', ''),
129 'name' : iface
.getAttribute('name')})
131 for prop
in iface
.getElementsByTagNameNS(None, 'property'):
134 * %(IFACE_PREFIX)s_%(PROP_UC)s:
136 * The fully-qualified property name "%(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 #define %(IFACE_PREFIX)s_%(PROP_UC)s \\
148 """ % {'IFACE_PREFIX' : (self
.prefix
+ 'PROP_' + \
149 parent_name
).upper().replace('/', ''),
150 'PROP_UC': prop
.getAttributeNS(NS_TP
, "name-for-bindings").upper(),
151 'name' : iface
.getAttribute('name'),
152 'prop' : prop
.getAttribute('name'),
156 for prop
in iface
.getElementsByTagNameNS(NS_TP
, 'contact-attribute'):
159 * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
161 * The fully-qualified contact attribute token name "%(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'),
171 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
173 """ % {'TOKEN_PREFIX' : (self
.prefix
+ 'TOKEN_' + \
174 parent_name
).upper().replace('/', ''),
175 'TOKEN_UC': prop
.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
176 'name' : iface
.getAttribute('name'),
177 'prop' : prop
.getAttribute('name'),
180 for prop
in iface
.getElementsByTagNameNS(NS_TP
, 'hct'):
181 if (prop
.getAttribute('is-family') != "yes"):
184 * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
186 * The fully-qualified capability token name "%(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'),
196 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
198 """ % {'TOKEN_PREFIX' : (self
.prefix
+ 'TOKEN_' + \
199 parent_name
).upper().replace('/', ''),
200 'TOKEN_UC': prop
.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
201 'name' : iface
.getAttribute('name'),
202 'prop' : prop
.getAttribute('name'),
205 if __name__
== '__main__':
207 Generator(argv
[0], argv
[1], argv
[2], xml
.dom
.minidom
.parse(argv
[3]))()