From 621bbbb8631f4b149d0478a3423687e4e973b04f Mon Sep 17 00:00:00 2001 From: Emilio Pozuelo Monfort Date: Tue, 1 Feb 2011 13:47:58 +0000 Subject: Update tools from telepathy-glib --- tools/glib-gtypes-generator.py | 214 +++++++++++++++++++++++++++-------------- 1 file changed, 141 insertions(+), 73 deletions(-) (limited to 'tools/glib-gtypes-generator.py') diff --git a/tools/glib-gtypes-generator.py b/tools/glib-gtypes-generator.py index fcb46e841..a49c36e7f 100644 --- a/tools/glib-gtypes-generator.py +++ b/tools/glib-gtypes-generator.py @@ -44,16 +44,35 @@ class GTypesGenerator(object): self.header = open(output + '.h', 'w') self.body = open(output + '-body.h', 'w') + self.docs = open(output + '-gtk-doc.h', 'w') - for f in (self.header, self.body): + for f in (self.header, self.body, self.docs): f.write('/* Auto-generated, do not edit.\n *\n' ' * This file may be distributed under the same terms\n' ' * as the specification from which it was generated.\n' ' */\n\n') + # keys are e.g. 'sv', values are the key escaped self.need_mappings = {} + # keys are the contents of the struct (e.g. 'sssu'), values are the + # key escaped self.need_structs = {} - self.need_arrays = {} + # keys are the contents of the struct (e.g. 'sssu'), values are the + # key escaped + self.need_struct_arrays = {} + + # keys are the contents of the array (unlike need_struct_arrays!), + # values are the key escaped + self.need_other_arrays = {} + + def h(self, code): + self.header.write(code.encode("utf-8")) + + def c(self, code): + self.body.write(code.encode("utf-8")) + + def d(self, code): + self.docs.write(code.encode('utf-8')) def do_mapping_header(self, mapping): members = mapping.getElementsByTagNameNS(NS_TP, 'member') @@ -70,45 +89,60 @@ class GTypesGenerator(object): docstring = get_docstring(mapping) or '(Undocumented)' - self.header.write('/**\n * %s:\n *\n' % name) - self.header.write(' * %s\n' % xml_escape(docstring)) - self.header.write(' *\n') - self.header.write(' * This macro expands to a call to a function\n') - self.header.write(' * that returns the #GType of a #GHashTable\n') - self.header.write(' * appropriate for representing a D-Bus\n') - self.header.write(' * dictionary of signature\n') - self.header.write(' * a{%s}.\n' % impl_sig) - self.header.write(' *\n') + self.d('/**\n * %s:\n *\n' % name) + self.d(' * %s\n' % xml_escape(docstring)) + self.d(' *\n') + self.d(' * This macro expands to a call to a function\n') + self.d(' * that returns the #GType of a #GHashTable\n') + self.d(' * appropriate for representing a D-Bus\n') + self.d(' * dictionary of signature\n') + self.d(' * a{%s}.\n' % impl_sig) + self.d(' *\n') key, value = members - self.header.write(' * Keys (D-Bus type %s,\n' + self.d(' * Keys (D-Bus type %s,\n' % key.getAttribute('type')) tp_type = key.getAttributeNS(NS_TP, 'type') if tp_type: - self.header.write(' * type %s,\n' % tp_type) - self.header.write(' * named %s):\n' + self.d(' * type %s,\n' % tp_type) + self.d(' * named %s):\n' % key.getAttribute('name')) docstring = get_docstring(key) or '(Undocumented)' - self.header.write(' * %s\n' % xml_escape(docstring)) - self.header.write(' *\n') + self.d(' * %s\n' % xml_escape(docstring)) + self.d(' *\n') - self.header.write(' * Values (D-Bus type %s,\n' + self.d(' * Values (D-Bus type %s,\n' % value.getAttribute('type')) tp_type = value.getAttributeNS(NS_TP, 'type') if tp_type: - self.header.write(' * type %s,\n' % tp_type) - self.header.write(' * named %s):\n' + self.d(' * type %s,\n' % tp_type) + self.d(' * named %s):\n' % value.getAttribute('name')) docstring = get_docstring(value) or '(Undocumented)' - self.header.write(' * %s\n' % xml_escape(docstring)) - self.header.write(' *\n') + self.d(' * %s\n' % xml_escape(docstring)) + self.d(' *\n') - self.header.write(' */\n') + self.d(' */\n') - self.header.write('#define %s (%s ())\n\n' % (name, impl)) + self.h('#define %s (%s ())\n\n' % (name, impl)) self.need_mappings[impl_sig] = esc_impl_sig + array_name = mapping.getAttribute('array-name') + if array_name: + gtype_name = self.PREFIX_ + 'ARRAY_TYPE_' + array_name.upper() + contents_sig = 'a{' + impl_sig + '}' + esc_contents_sig = escape_as_identifier(contents_sig) + impl = self.prefix_ + 'type_dbus_array_of_' + esc_contents_sig + self.d('/**\n * %s:\n\n' % gtype_name) + self.d(' * Expands to a call to a function\n') + self.d(' * that returns the #GType of a #GPtrArray\n') + self.d(' * of #%s.\n' % name) + self.d(' */\n\n') + + self.h('#define %s (%s ())\n\n' % (gtype_name, impl)) + self.need_other_arrays[contents_sig] = esc_contents_sig + def do_struct_header(self, struct): members = struct.getElementsByTagNameNS(NS_TP, 'member') impl_sig = ''.join([elt.getAttribute('type') for elt in members]) @@ -128,43 +162,45 @@ class GTypesGenerator(object): docstring = '(Undocumented)' else: docstring = '(Undocumented)' - self.header.write('/**\n * %s:\n\n' % name) - self.header.write(' * %s\n' % xml_escape(docstring)) - self.header.write(' *\n') - self.header.write(' * This macro expands to a call to a function\n') - self.header.write(' * that returns the #GType of a #GValueArray\n') - self.header.write(' * appropriate for representing a D-Bus struct\n') - self.header.write(' * with signature (%s).\n' + self.d('/**\n * %s:\n\n' % name) + self.d(' * %s\n' % xml_escape(docstring)) + self.d(' *\n') + self.d(' * This macro expands to a call to a function\n') + self.d(' * that returns the #GType of a #GValueArray\n') + self.d(' * appropriate for representing a D-Bus struct\n') + self.d(' * with signature (%s).\n' % impl_sig) - self.header.write(' *\n') + self.d(' *\n') for i, member in enumerate(members): - self.header.write(' * Member %d (D-Bus type ' + self.d(' * Member %d (D-Bus type ' '%s,\n' % (i, member.getAttribute('type'))) tp_type = member.getAttributeNS(NS_TP, 'type') if tp_type: - self.header.write(' * type %s,\n' % tp_type) - self.header.write(' * named %s):\n' + self.d(' * type %s,\n' % tp_type) + self.d(' * named %s):\n' % member.getAttribute('name')) docstring = get_docstring(member) or '(Undocumented)' - self.header.write(' * %s\n' % xml_escape(docstring)) - self.header.write(' *\n') + self.d(' * %s\n' % xml_escape(docstring)) + self.d(' *\n') - self.header.write(' */\n') - self.header.write('#define %s (%s ())\n\n' % (name, impl)) + self.d(' */\n\n') + + self.h('#define %s (%s ())\n\n' % (name, impl)) array_name = struct.getAttribute('array-name') if array_name != '': array_name = (self.PREFIX_ + 'ARRAY_TYPE_' + array_name.upper()) impl = self.prefix_ + 'type_dbus_array_' + esc_impl_sig - self.header.write('/**\n * %s:\n\n' % array_name) - self.header.write(' * Expands to a call to a function\n') - self.header.write(' * that returns the #GType of a #GPtrArray\n') - self.header.write(' * of #%s.\n' % name) - self.header.write(' */\n') - self.header.write('#define %s (%s ())\n\n' % (array_name, impl)) - self.need_arrays[impl_sig] = esc_impl_sig + self.d('/**\n * %s:\n\n' % array_name) + self.d(' * Expands to a call to a function\n') + self.d(' * that returns the #GType of a #GPtrArray\n') + self.d(' * of #%s.\n' % name) + self.d(' */\n\n') + + self.h('#define %s (%s ())\n\n' % (array_name, impl)) + self.need_struct_arrays[impl_sig] = esc_impl_sig self.need_structs[impl_sig] = esc_impl_sig @@ -176,51 +212,83 @@ class GTypesGenerator(object): self.do_mapping_header(mapping) for sig in self.need_mappings: - self.header.write('GType %stype_dbus_hash_%s (void);\n\n' % + self.h('GType %stype_dbus_hash_%s (void);\n\n' % (self.prefix_, self.need_mappings[sig])) - self.body.write('GType\n%stype_dbus_hash_%s (void)\n{\n' % + self.c('GType\n%stype_dbus_hash_%s (void)\n{\n' % (self.prefix_, self.need_mappings[sig])) - self.body.write(' static GType t = 0;\n\n') - self.body.write(' if (G_UNLIKELY (t == 0))\n') + self.c(' static GType t = 0;\n\n') + self.c(' if (G_UNLIKELY (t == 0))\n') # FIXME: translate sig into two GTypes items = tuple(Signature(sig)) gtypes = types_to_gtypes(items) - self.body.write(' t = dbus_g_type_get_map ("GHashTable", ' + self.c(' t = dbus_g_type_get_map ("GHashTable", ' '%s, %s);\n' % (gtypes[0], gtypes[1])) - self.body.write(' return t;\n') - self.body.write('}\n\n') + self.c(' return t;\n') + self.c('}\n\n') for struct in structs: self.do_struct_header(struct) for sig in self.need_structs: - self.header.write('GType %stype_dbus_struct_%s (void);\n\n' % + self.h('GType %stype_dbus_struct_%s (void);\n\n' % (self.prefix_, self.need_structs[sig])) - self.body.write('GType\n%stype_dbus_struct_%s (void)\n{\n' % + self.c('GType\n%stype_dbus_struct_%s (void)\n{\n' % (self.prefix_, self.need_structs[sig])) - self.body.write(' static GType t = 0;\n\n') - self.body.write(' if (G_UNLIKELY (t == 0))\n') - self.body.write(' t = dbus_g_type_get_struct ("GValueArray",\n') + self.c(' static GType t = 0;\n\n') + self.c(' if (G_UNLIKELY (t == 0))\n') + self.c(' t = dbus_g_type_get_struct ("GValueArray",\n') items = tuple(Signature(sig)) gtypes = types_to_gtypes(items) for gtype in gtypes: - self.body.write(' %s,\n' % gtype) - self.body.write(' G_TYPE_INVALID);\n') - self.body.write(' return t;\n') - self.body.write('}\n\n') - - for sig in self.need_arrays: - self.header.write('GType %stype_dbus_array_%s (void);\n\n' % - (self.prefix_, self.need_structs[sig])) - self.body.write('GType\n%stype_dbus_array_%s (void)\n{\n' % - (self.prefix_, self.need_structs[sig])) - self.body.write(' static GType t = 0;\n\n') - self.body.write(' if (G_UNLIKELY (t == 0))\n') - self.body.write(' t = dbus_g_type_get_collection ("GPtrArray", ' + self.c(' %s,\n' % gtype) + self.c(' G_TYPE_INVALID);\n') + self.c(' return t;\n') + self.c('}\n\n') + + for sig in self.need_struct_arrays: + self.h('GType %stype_dbus_array_%s (void);\n\n' % + (self.prefix_, self.need_struct_arrays[sig])) + self.c('GType\n%stype_dbus_array_%s (void)\n{\n' % + (self.prefix_, self.need_struct_arrays[sig])) + self.c(' static GType t = 0;\n\n') + self.c(' if (G_UNLIKELY (t == 0))\n') + self.c(' t = dbus_g_type_get_collection ("GPtrArray", ' '%stype_dbus_struct_%s ());\n' % - (self.prefix_, self.need_structs[sig])) - self.body.write(' return t;\n') - self.body.write('}\n\n') + (self.prefix_, self.need_struct_arrays[sig])) + self.c(' return t;\n') + self.c('}\n\n') + + for sig in self.need_other_arrays: + self.h('GType %stype_dbus_array_of_%s (void);\n\n' % + (self.prefix_, self.need_other_arrays[sig])) + self.c('GType\n%stype_dbus_array_of_%s (void)\n{\n' % + (self.prefix_, self.need_other_arrays[sig])) + self.c(' static GType t = 0;\n\n') + self.c(' if (G_UNLIKELY (t == 0))\n') + + if sig[:2] == 'a{' and sig[-1:] == '}': + # array of mappings + self.c(' t = dbus_g_type_get_collection (' + '"GPtrArray", ' + '%stype_dbus_hash_%s ());\n' % + (self.prefix_, escape_as_identifier(sig[2:-1]))) + elif sig[:2] == 'a(' and sig[-1:] == ')': + # array of arrays of struct + self.c(' t = dbus_g_type_get_collection (' + '"GPtrArray", ' + '%stype_dbus_array_%s ());\n' % + (self.prefix_, escape_as_identifier(sig[2:-1]))) + elif sig[:1] == 'a': + # array of arrays of non-struct + self.c(' t = dbus_g_type_get_collection (' + '"GPtrArray", ' + '%stype_dbus_array_of_%s ());\n' % + (self.prefix_, escape_as_identifier(sig[1:]))) + else: + raise AssertionError("array of '%s' not supported" % sig) + + self.c(' return t;\n') + self.c('}\n\n') if __name__ == '__main__': argv = sys.argv[1:] -- cgit v1.2.3