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
, dom
, output_base
):
12 self
.prefix
= prefix
+ '_'
13 self
.spec
= get_by_path(dom
, "spec")[0]
15 self
.output_base
= output_base
24 file_set_contents(self
.output_base
+ '.h', u('').join(self
.__header
).encode('utf-8'))
25 file_set_contents(self
.output_base
+ '-gtk-doc.h', u('').join(self
.__docs
).encode('utf-8'))
27 def write(self
, code
):
28 self
.__header
.append(code
)
31 self
.__docs
.append(code
)
35 self
.write('/* Generated from ')
36 self
.write(get_descendant_text(get_by_path(self
.spec
, 'title')))
37 version
= get_by_path(self
.spec
, "version")
39 self
.write(', version ' + get_descendant_text(version
))
41 for copyright
in get_by_path(self
.spec
, 'copyright'):
42 self
.write(get_descendant_text(copyright
))
44 self
.write(get_descendant_text(get_by_path(self
.spec
, 'license')))
46 self
.write(get_descendant_text(get_by_path(self
.spec
, 'docstring')))
57 for elem
in self
.spec
.getElementsByTagNameNS(NS_TP
, '*'):
58 if elem
.localName
== 'flags':
60 elif elem
.localName
== 'enum':
63 def do_flags(self
, flags
):
64 name
= flags
.getAttribute('plural') or flags
.getAttribute('name')
65 value_prefix
= flags
.getAttribute('singular') or \
66 flags
.getAttribute('value-prefix') or \
67 flags
.getAttribute('name')
71 """ % (self
.prefix
+ name
).replace('_', ''))
72 for flag
in get_by_path(flags
, 'flag'):
73 self
.do_gtkdoc(flag
, value_prefix
)
75 docstrings
= get_by_path(flags
, 'docstring')
80 """ % get_descendant_text(docstrings
).replace('\n', ' '))
82 * Bitfield/set of flags generated from the Telepathy specification.
86 self
.write("typedef enum /*< flags >*/ {\n")
88 for flag
in get_by_path(flags
, 'flag'):
89 self
.do_val(flag
, value_prefix
)
93 """ % (self
.prefix
+ name
).replace('_', ''))
95 def do_enum(self
, enum
):
96 name
= enum
.getAttribute('singular') or enum
.getAttribute('name')
97 value_prefix
= enum
.getAttribute('singular') or \
98 enum
.getAttribute('value-prefix') or \
99 enum
.getAttribute('name')
100 name_plural
= enum
.getAttribute('plural') or \
101 enum
.getAttribute('name') + 's'
105 """ % (self
.prefix
+ name
).replace('_', ''))
106 vals
= get_by_path(enum
, 'enumvalue')
108 self
.do_gtkdoc(val
, value_prefix
)
110 docstrings
= get_by_path(enum
, 'docstring')
115 """ % get_descendant_text(docstrings
).replace('\n', ' '))
117 * Bitfield/set of flags generated from the Telepathy specification.
121 self
.write("typedef enum {\n")
124 self
.do_val(val
, value_prefix
)
125 self
.write("} %s;\n" % (self
.prefix
+ name
).replace('_', ''))
129 * %(upper-prefix)sNUM_%(upper-plural)s:
131 * 1 higher than the highest valid value of #%(mixed-name)s.
135 * NUM_%(upper-prefix)s%(upper-plural)s: (skip)
137 * 1 higher than the highest valid value of #%(mixed-name)s.
138 * In new code, use %(upper-prefix)sNUM_%(upper-plural)s instead.
140 """ % {'mixed-name' : (self
.prefix
+ name
).replace('_', ''),
141 'upper-prefix' : self
.prefix
.upper(),
142 'upper-plural' : name_plural
.upper(),
143 'last-val' : vals
[-1].getAttribute('value')})
146 #define %(upper-prefix)sNUM_%(upper-plural)s (%(last-val)s+1)
147 #define NUM_%(upper-prefix)s%(upper-plural)s %(upper-prefix)sNUM_%(upper-plural)s
149 """ % {'mixed-name' : (self
.prefix
+ name
).replace('_', ''),
150 'upper-prefix' : self
.prefix
.upper(),
151 'upper-plural' : name_plural
.upper(),
152 'last-val' : vals
[-1].getAttribute('value')})
154 def do_val(self
, val
, value_prefix
):
155 name
= val
.getAttribute('name')
156 suffix
= val
.getAttribute('suffix')
157 use_name
= (self
.prefix
+ value_prefix
+ '_' + \
158 (suffix
or name
)).upper()
159 assert not (name
and suffix
) or name
== suffix
, \
160 'Flag/enumvalue name %s != suffix %s' % (name
, suffix
)
161 self
.write(' %s = %s,\n' % (use_name
, val
.getAttribute('value')))
163 def do_gtkdoc(self
, node
, value_prefix
):
165 self
.d((self
.prefix
+ value_prefix
+ '_' +
166 node
.getAttribute('suffix')).upper())
167 self
.d(': <![CDATA[')
168 docstring
= get_by_path(node
, 'docstring')
169 self
.d(get_descendant_text(docstring
).replace('\n', ' '))
180 if __name__
== '__main__':
182 Generator(argv
[0], xml
.dom
.minidom
.parse(argv
[1]), argv
[2])()