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, dom, output_base):
11 self.prefix = prefix + '_'
12 self.spec = get_by_path(dom, "spec")[0]
14 self.__header = open(output_base + '.h', 'w')
15 self.__docs = open(output_base + '-gtk-doc.h', 'w')
22 def write(self, code):
23 self.__header.write(code.encode('utf-8'))
26 self.__docs.write(code.encode('utf-8'))
30 self.write('/* Generated from ')
31 self.write(get_descendant_text(get_by_path(self.spec, 'title')))
32 version = get_by_path(self.spec, "version")
34 self.write(', version ' + get_descendant_text(version))
36 for copyright in get_by_path(self.spec, 'copyright'):
37 self.write(get_descendant_text(copyright))
39 self.write(get_descendant_text(get_by_path(self.spec, 'license')))
41 self.write(get_descendant_text(get_by_path(self.spec, 'docstring')))
52 for elem in self.spec.getElementsByTagNameNS(NS_TP, '*'):
53 if elem.localName == 'flags':
55 elif elem.localName == 'enum':
58 def do_flags(self, flags):
59 name = flags.getAttribute('plural') or flags.getAttribute('name')
60 value_prefix = flags.getAttribute('singular') or \
61 flags.getAttribute('value-prefix') or \
62 flags.getAttribute('name')
67 """ % (self.prefix + name).replace('_', ''))
68 for flag in get_by_path(flags, 'flag'):
69 self.do_gtkdoc(flag, value_prefix)
71 docstrings = get_by_path(flags, 'docstring')
76 """ % get_descendant_text(docstrings).replace('\n', ' '))
78 * Bitfield/set of flags generated from the Telepathy specification.
82 self.write("typedef enum /*< flags >*/ {\n")
84 for flag in get_by_path(flags, 'flag'):
85 self.do_val(flag, value_prefix)
89 """ % (self.prefix + name).replace('_', ''))
91 def do_enum(self, enum):
92 name = enum.getAttribute('singular') or enum.getAttribute('name')
93 value_prefix = enum.getAttribute('singular') or \
94 enum.getAttribute('value-prefix') or \
95 enum.getAttribute('name')
96 name_plural = enum.getAttribute('plural') or \
97 enum.getAttribute('name') + 's'
102 """ % (self.prefix + name).replace('_', ''))
103 vals = get_by_path(enum, 'enumvalue')
105 self.do_gtkdoc(val, value_prefix)
107 docstrings = get_by_path(enum, 'docstring')
112 """ % get_descendant_text(docstrings).replace('\n', ' '))
114 * Bitfield/set of flags generated from the Telepathy specification.
118 self.write("typedef enum {\n")
121 self.do_val(val, value_prefix)
122 self.write("} %s;\n" % (self.prefix + name).replace('_', ''))
126 * NUM_%(upper-plural)s:
128 * 1 higher than the highest valid value of #%(mixed-name)s.
130 """ % {'mixed-name' : (self.prefix + name).replace('_', ''),
131 'upper-plural' : (self.prefix + name_plural).upper(),
132 'last-val' : vals[-1].getAttribute('value')})
135 #define NUM_%(upper-plural)s (%(last-val)s+1)
137 """ % {'mixed-name' : (self.prefix + name).replace('_', ''),
138 'upper-plural' : (self.prefix + name_plural).upper(),
139 'last-val' : vals[-1].getAttribute('value')})
141 def do_val(self, val, value_prefix):
142 name = val.getAttribute('name')
143 suffix = val.getAttribute('suffix')
144 use_name = (self.prefix + value_prefix + '_' + \
145 (suffix or name)).upper()
146 assert not (name and suffix) or name == suffix, \
147 'Flag/enumvalue name %s != suffix %s' % (name, suffix)
148 self.write(' %s = %s,\n' % (use_name, val.getAttribute('value')))
150 def do_gtkdoc(self, node, value_prefix):
152 self.d((self.prefix + value_prefix + '_' +
153 node.getAttribute('suffix')).upper())
154 self.d(': <![CDATA[')
155 docstring = get_by_path(node, 'docstring')
156 self.d(get_descendant_text(docstring).replace('\n', ' '))
167 if __name__ == '__main__':
169 Generator(argv[0], xml.dom.minidom.parse(argv[1]), argv[2])()