Codebase list telepathy-glib / f4634b2
Make the code generation tools work under either Python 2 or 3 They have been verified to produce identical output in _gen. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=56758 Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk> Reviewed-by: Xavier Claessens <xavier.claessens@collabora.co.uk> Simon McVittie 11 years ago
13 changed file(s) with 129 addition(s) and 120 deletion(s). Raw diff Collapse all Expand all
22 from sys import argv, stdout, stderr
33 import xml.dom.minidom
44
5 from libtpcodegen import file_set_contents
5 from libtpcodegen import file_set_contents, u
66 from libglibcodegen import NS_TP, get_docstring, \
77 get_descendant_text, get_by_path
88
1111 self.prefix = prefix + '_'
1212 self.spec = get_by_path(dom, "spec")[0]
1313
14 self.output_base = output_base
14 self.output_base = output_base
1515 self.__header = []
1616 self.__docs = []
1717
2020 self.do_body()
2121 self.do_footer()
2222
23 file_set_contents(self.output_base + '.h', ''.join(self.__header))
24 file_set_contents(self.output_base + '-gtk-doc.h', ''.join(self.__docs))
23 file_set_contents(self.output_base + '.h', u('').join(self.__header).encode('utf-8'))
24 file_set_contents(self.output_base + '-gtk-doc.h', u('').join(self.__docs).encode('utf-8'))
2525
2626 def write(self, code):
27 self.__header.append(code.encode('utf-8'))
27 self.__header.append(code)
2828
2929 def d(self, code):
30 self.__docs.append(code.encode('utf-8'))
30 self.__docs.append(code)
3131
3232 # Header
3333 def do_header(self):
2626 import xml.dom.minidom
2727 from getopt import gnu_getopt
2828
29 from libtpcodegen import file_set_contents
30 from libglibcodegen import Signature, type_to_gtype, cmp_by_name, \
29 from libtpcodegen import file_set_contents, key_by_name, u
30 from libglibcodegen import Signature, type_to_gtype, \
3131 get_docstring, xml_escape, get_deprecated
3232
3333
7373 self.guard = opts.get('--guard', None)
7474
7575 def h(self, s):
76 if isinstance(s, unicode):
77 s = s.encode('utf-8')
7876 self.__header.append(s)
7977
8078 def b(self, s):
81 if isinstance(s, unicode):
82 s = s.encode('utf-8')
8379 self.__body.append(s)
8480
8581 def d(self, s):
86 if isinstance(s, unicode):
87 s = s.encode('utf-8')
8882 self.__docs.append(s)
8983
9084 def get_iface_quark(self):
11901184 self.b('')
11911185
11921186 nodes = self.dom.getElementsByTagName('node')
1193 nodes.sort(cmp_by_name)
1187 nodes.sort(key=key_by_name)
11941188
11951189 for node in nodes:
11961190 self.do_interface(node)
12431237 self.h('#endif /* defined (%s) */' % self.guard)
12441238 self.h('')
12451239
1246 file_set_contents(self.basename + '.h', '\n'.join(self.__header))
1247 file_set_contents(self.basename + '-body.h', '\n'.join(self.__body))
1248 file_set_contents(self.basename + '-gtk-doc.h', '\n'.join(self.__docs))
1240 file_set_contents(self.basename + '.h', u('\n').join(self.__header).encode('utf-8'))
1241 file_set_contents(self.basename + '-body.h', u('\n').join(self.__body).encode('utf-8'))
1242 file_set_contents(self.basename + '-gtk-doc.h', u('\n').join(self.__docs).encode('utf-8'))
12491243
12501244 def types_to_gtypes(types):
12511245 return [type_to_gtype(t)[1] for t in types]
3030 for signal in signals:
3131 self.do_signal(signal)
3232
33 print 'void'
34 print '%s_register_dbus_glib_marshallers (void)' % self.prefix
35 print '{'
33 print('void')
34 print('%s_register_dbus_glib_marshallers (void)' % self.prefix)
35 print('{')
3636
37 all = self.marshallers.keys()
37 all = list(self.marshallers.keys())
3838 all.sort()
3939 for marshaller in all:
4040 rhs = self.marshallers[marshaller]
4141
42 print ' dbus_g_object_register_marshaller ('
43 print ' g_cclosure_marshal_generic,'
44 print ' G_TYPE_NONE, /* return */'
42 print(' dbus_g_object_register_marshaller (')
43 print(' g_cclosure_marshal_generic,')
44 print(' G_TYPE_NONE, /* return */')
4545 for type in rhs:
46 print ' G_TYPE_%s,' % type.replace('VOID', 'NONE')
47 print ' G_TYPE_INVALID);'
46 print(' G_TYPE_%s,' % type.replace('VOID', 'NONE'))
47 print(' G_TYPE_INVALID);')
4848
49 print '}'
49 print('}')
5050
5151
5252 def types_to_gtypes(types):
1111
1212 def __call__(self):
1313
14 print '{'
15 print ' GEnumClass *klass;'
16 print ' GEnumValue *value_by_name;'
17 print ' GEnumValue *value_by_nick;'
18 print ''
19 print ' g_type_init ();'
20 print ' klass = g_type_class_ref (TP_TYPE_ERROR);'
14 print('{')
15 print(' GEnumClass *klass;')
16 print(' GEnumValue *value_by_name;')
17 print(' GEnumValue *value_by_nick;')
18 print('')
19 print(' g_type_init ();')
20 print(' klass = g_type_class_ref (TP_TYPE_ERROR);')
2121
2222 for error in self.errors.getElementsByTagNameNS(NS_TP, 'error'):
2323 ns = error.parentNode.getAttribute('namespace')
2727 s = ('TP_ERROR_STR_' +
2828 error.getAttribute('name').replace(' ', '_').replace('.', '_').upper())
2929
30 print ''
31 print ' /* %s.%s */' % (ns, nick)
32 print (' value_by_name = g_enum_get_value_by_name (klass, "%s");'
33 % enum)
34 print (' value_by_nick = g_enum_get_value_by_nick (klass, "%s");'
35 % nick)
36 print (' g_assert (value_by_name != NULL);')
37 print (' g_assert (value_by_nick != NULL);')
38 print (' g_assert_cmpint (value_by_name->value, ==, %s);'
39 % enum)
40 print (' g_assert_cmpint (value_by_nick->value, ==, %s);'
41 % enum)
42 print (' g_assert_cmpstr (value_by_name->value_name, ==, "%s");'
43 % enum)
44 print (' g_assert_cmpstr (value_by_nick->value_name, ==, "%s");'
45 % enum)
46 print (' g_assert_cmpstr (value_by_name->value_nick, ==, "%s");'
47 % nick)
48 print (' g_assert_cmpstr (value_by_nick->value_nick, ==, "%s");'
49 % nick)
50 print (' g_assert_cmpstr (%s, ==, TP_ERROR_PREFIX ".%s");'
51 % (s, nick))
30 print('')
31 print(' /* %s.%s */' % (ns, nick))
32 print(' value_by_name = g_enum_get_value_by_name (klass, "%s");'
33 % enum)
34 print(' value_by_nick = g_enum_get_value_by_nick (klass, "%s");'
35 % nick)
36 print(' g_assert (value_by_name != NULL);')
37 print(' g_assert (value_by_nick != NULL);')
38 print(' g_assert_cmpint (value_by_name->value, ==, %s);'
39 % enum)
40 print(' g_assert_cmpint (value_by_nick->value, ==, %s);'
41 % enum)
42 print(' g_assert_cmpstr (value_by_name->value_name, ==, "%s");'
43 % enum)
44 print(' g_assert_cmpstr (value_by_nick->value_name, ==, "%s");'
45 % enum)
46 print(' g_assert_cmpstr (value_by_name->value_nick, ==, "%s");'
47 % nick)
48 print(' g_assert_cmpstr (value_by_nick->value_nick, ==, "%s");'
49 % nick)
50 print(' g_assert_cmpstr (%s, ==, TP_ERROR_PREFIX ".%s");'
51 % (s, nick))
5252
53 print '}'
53 print('}')
5454
5555 if __name__ == '__main__':
5656 argv = sys.argv[1:]
22 import sys
33 import xml.dom.minidom
44
5 from libtpcodegen import file_set_contents
5 from libtpcodegen import file_set_contents, u
66 from libglibcodegen import NS_TP, get_docstring, xml_escape
77
88 class Generator(object):
1616 self.__docs = []
1717
1818 def h(self, s):
19 if isinstance(s, unicode):
20 s = s.encode('utf-8')
2119 self.__header.append(s)
2220
2321 def b(self, s):
24 if isinstance(s, unicode):
25 s = s.encode('utf-8')
2622 self.__body.append(s)
2723
2824 def d(self, s):
29 if isinstance(s, unicode):
30 s = s.encode('utf-8')
3125 self.__docs.append(s)
3226
3327 def __call__(self):
7165 self.h('')
7266 self.b('')
7367
74 file_set_contents(self.basename + '.h', '\n'.join(self.__header))
75 file_set_contents(self.basename + '.c', '\n'.join(self.__body))
76 file_set_contents(self.basename + '-gtk-doc.h', '\n'.join(self.__docs))
68 file_set_contents(self.basename + '.h', u('\n').join(self.__header).encode('utf-8'))
69 file_set_contents(self.basename + '.c', u('\n').join(self.__body).encode('utf-8'))
70 file_set_contents(self.basename + '-gtk-doc.h', u('\n').join(self.__docs).encode('utf-8'))
7771
7872 if __name__ == '__main__':
7973 argv = sys.argv[1:]
2525 import os.path
2626 import xml.dom.minidom
2727
28 from libtpcodegen import file_set_contents
29 from libglibcodegen import Signature, type_to_gtype, cmp_by_name, \
28 from libtpcodegen import file_set_contents, key_by_name, u
29 from libglibcodegen import Signature, type_to_gtype, \
3030 NS_TP, dbus_gutils_wincaps_to_uscore
3131
3232
8484 self.allow_havoc = allow_havoc
8585
8686 def h(self, s):
87 if isinstance(s, unicode):
88 s = s.encode('utf-8')
8987 self.__header.append(s)
9088
9189 def b(self, s):
92 if isinstance(s, unicode):
93 s = s.encode('utf-8')
9490 self.__body.append(s)
9591
9692 def d(self, s):
97 if isinstance(s, unicode):
98 s = s.encode('utf-8')
9993 self.__docs.append(s)
10094
10195 def do_node(self, node):
732726
733727 def __call__(self):
734728 nodes = self.dom.getElementsByTagName('node')
735 nodes.sort(cmp_by_name)
729 nodes.sort(key=key_by_name)
736730
737731 self.h('#include <glib-object.h>')
738732 self.h('#include <dbus/dbus-glib.h>')
760754
761755 self.h('')
762756 self.b('')
763 file_set_contents(self.basename + '.h', '\n'.join(self.__header))
764 file_set_contents(self.basename + '.c', '\n'.join(self.__body))
765 file_set_contents(self.basename + '-gtk-doc.h', '\n'.join(self.__docs))
757 file_set_contents(self.basename + '.h', u('\n').join(self.__header).encode('utf-8'))
758 file_set_contents(self.basename + '.c', u('\n').join(self.__body).encode('utf-8'))
759 file_set_contents(self.basename + '-gtk-doc.h', u('\n').join(self.__docs).encode('utf-8'))
766760
767761 def cmdline_error():
768 print """\
762 print("""\
769763 usage:
770764 gen-ginterface [OPTIONS] xmlfile Prefix_
771765 options:
785779 void symbol (DBusGMethodInvocation *context)
786780 and return some sort of "not implemented" error via
787781 dbus_g_method_return_error (context, ...)
788 """
782 """)
789783 sys.exit(1)
790784
791785
2222 import sys
2323 import xml.dom.minidom
2424
25 from libtpcodegen import file_set_contents
25 from libtpcodegen import file_set_contents, u
2626 from libglibcodegen import escape_as_identifier, \
2727 get_docstring, \
2828 NS_TP, \
6767 self.need_other_arrays = {}
6868
6969 def h(self, code):
70 self.header.append(code.encode("utf-8"))
70 self.header.append(code)
7171
7272 def c(self, code):
73 self.body.append(code.encode("utf-8"))
73 self.body.append(code)
7474
7575 def d(self, code):
76 self.docs.append(code.encode('utf-8'))
76 self.docs.append(code)
7777
7878 def do_mapping_header(self, mapping):
7979 members = mapping.getElementsByTagNameNS(NS_TP, 'member')
291291 self.c(' return t;\n')
292292 self.c('}\n\n')
293293
294 file_set_contents(self.output + '.h', ''.join(self.header))
295 file_set_contents(self.output + '-body.h', ''.join(self.body))
296 file_set_contents(self.output + '-gtk-doc.h', ''.join(self.docs))
294 file_set_contents(self.output + '.h', u('').join(self.header).encode('utf-8'))
295 file_set_contents(self.output + '-body.h', u('').join(self.body).encode('utf-8'))
296 file_set_contents(self.output + '-gtk-doc.h', u('').join(self.docs).encode('utf-8'))
297297
298298 if __name__ == '__main__':
299299 argv = sys.argv[1:]
22 from sys import argv, stdout, stderr
33 import xml.dom.minidom
44
5 from libtpcodegen import file_set_contents
5 from libtpcodegen import file_set_contents, u
66 from libglibcodegen import NS_TP, get_docstring, \
77 get_descendant_text, get_by_path
88
2323 self.spec = get_by_path(dom, "spec")[0]
2424
2525 def h(self, code):
26 self.decls.append(code.encode('utf-8'))
26 self.decls.append(code)
2727
2828 def c(self, code):
29 self.impls.append(code.encode('utf-8'))
29 self.impls.append(code)
3030
3131 def d(self, code):
32 self.docs.append(code.encode('utf-8'))
32 self.docs.append(code)
3333
3434 def __call__(self):
3535 for f in self.h, self.c:
3636 self.do_header(f)
3737 self.do_body()
3838
39 file_set_contents(self.implfile, ''.join(self.impls))
40 file_set_contents(self.declfile, ''.join(self.decls))
41 file_set_contents(self.docfile, ''.join(self.docs))
39 file_set_contents(self.implfile, u('').join(self.impls).encode('utf-8'))
40 file_set_contents(self.declfile, u('').join(self.decls).encode('utf-8'))
41 file_set_contents(self.docfile, u('').join(self.docs).encode('utf-8'))
4242
4343 # Header
4444 def do_header(self, f):
8686
8787 head, tail = argv
8888
89 print '\n'.join(gobject_header(head, tail, as_interface=as_interface))
89 print('\n'.join(gobject_header(head, tail, as_interface=as_interface)))
153153 return ("GHashTable *", "DBUS_TYPE_G_STRING_STRING_HASHTABLE", "BOXED", False)
154154 elif s[:2] == 'a{': #some arbitrary hash tables
155155 if s[2] not in ('y', 'b', 'n', 'q', 'i', 'u', 's', 'o', 'g'):
156 raise Exception, "can't index a hashtable off non-basic type " + s
156 raise Exception("can't index a hashtable off non-basic type " + s)
157157 first = type_to_gtype(s[2])
158158 second = type_to_gtype(s[3:-1])
159159 return ("GHashTable *", "(dbus_g_type_get_map (\"GHashTable\", " + first[1] + ", " + second[1] + "))", "BOXED", False)
168168 return ("GValueArray *", gtype, "BOXED", True)
169169
170170 # we just don't know ..
171 raise Exception, "don't know the GType for " + s
171 raise Exception("don't know the GType for " + s)
2020 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2121
2222 import os
23 import sys
2324 from string import ascii_letters, digits
2425
2526
2627 NS_TP = "http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
2728
2829 _ASCII_ALNUM = ascii_letters + digits
30
31 if sys.version_info[0] >= 3:
32 def u(s):
33 """Return s, which must be a str literal with no non-ASCII characters.
34 This is like a more restricted form of the Python 2 u'' syntax.
35 """
36 return s.encode('ascii').decode('ascii')
37 else:
38 def u(s):
39 """Return a Unicode version of s, which must be a str literal
40 (a bytestring) in which each byte is an ASCII character.
41 This is like a more restricted form of the u'' syntax.
42 """
43 return s.decode('ascii')
2944
3045 def file_set_contents(filename, contents):
3146 try:
3752 except OSError:
3853 pass
3954
40 open(filename + '.tmp', 'w').write(contents)
55 open(filename + '.tmp', 'wb').write(contents)
4156 os.rename(filename + '.tmp', filename)
4257
4358 def cmp_by_name(node1, node2):
4459 return cmp(node1.getAttributeNode("name").nodeValue,
4560 node2.getAttributeNode("name").nodeValue)
4661
62 def key_by_name(node):
63 return node.getAttributeNode("name").nodeValue
4764
4865 def escape_as_identifier(identifier):
4966 """Escape the given string to be a valid D-Bus object path or service
167184 self.remaining = string
168185
169186 def next(self):
187 return self.__next__()
188
189 def __next__(self):
170190 if self.remaining == '':
171191 raise StopIteration
172192
6262
6363 if dpkg:
6464 assert dpkg_first_line is not None
65 print dpkg_first_line
65 print(dpkg_first_line)
6666 if dpkg_build_depends_package is not None:
67 print "* Build-Depends-Package: %s" % dpkg_build_depends_package
67 print("* Build-Depends-Package: %s" % dpkg_build_depends_package)
6868
6969 for filename in abifiles:
7070 lines = open(filename, 'r').readlines()
119119 lines = lines[cut:]
120120
121121 if gnuld:
122 print "%s {" % version
123 print " global:"
122 print("%s {" % version)
123 print(" global:")
124124
125125 for symbol in lines:
126126 symbol = symbol.strip()
129129 continue
130130
131131 if gnuld:
132 print " %s;" % symbol
132 print(" %s;" % symbol)
133133 elif dpkg:
134134 dpkg_symbols.append('%s@%s %s' % (symbol, version, release))
135135
141141
142142 if gnuld:
143143 if extends == '-':
144 print " local:"
145 print " *;"
146 print "};"
144 print(" local:")
145 print(" *;")
146 print("};")
147147 else:
148 print "} %s;" % extends
149 print
148 print("} %s;" % extends)
149 print("")
150150
151151 if dpkg:
152152 dpkg_symbols.sort()
153153 dpkg_versions.sort()
154154
155155 for x in dpkg_versions:
156 print " %s" % x
156 print(" %s" % x)
157157
158158 for x in dpkg_symbols:
159 print " %s" % x
159 print(" %s" % x)
160160
161161 if symbol_set is not None:
162162 missing = versioned_symbols - symbol_set
181181 raise SystemExit(1)
182182
183183 if gnuld:
184 print "%s {" % unreleased_version
185 print " global:"
184 print("%s {" % unreleased_version)
185 print(" global:")
186186
187187 for symbol in unreleased:
188 print " %s;" % symbol
189
190 print "} %s;" % version
188 print(" %s;" % symbol)
189
190 print("} %s;" % version)
191191
192192
193193 if __name__ == '__main__':
00 #!/usr/bin/python
11
2 import sys
23 from sys import argv, stdout, stderr
34 import codecs, locale
45 import os
56 import xml.dom.minidom
67
7 stdout = codecs.getwriter('utf-8')(stdout)
8 if sys.version_info[0] < 3:
9 stdout = codecs.getwriter('utf-8')(stdout)
810
911 NS_XI = 'http://www.w3.org/2001/XInclude'
1012
1113 def xincludate(dom, base, dropns = []):
1214 remove_attrs = []
13 for i in xrange(dom.documentElement.attributes.length):
15 for i in range(dom.documentElement.attributes.length):
1416 attr = dom.documentElement.attributes.item(i)
1517 if attr.prefix == 'xmlns':
1618 if attr.localName in dropns:
3335 argv = argv[1:]
3436 dom = xml.dom.minidom.parse(argv[0])
3537 xincludate(dom, argv[0])
36 xml = dom.toxml()
38
39 if sys.version_info[0] >= 3:
40 xml = dom.toxml(encoding=None)
41 else:
42 xml = dom.toxml()
43
3744 stdout.write(xml)
3845 stdout.write('\n')