bab341
Add compatibility with Python 3 (keeping compatibility with Python 2.7)
bab341
bab341
--- gtk+-2.24.32/gtk/gtk-builder-convert       (original)
bab341
+++ gtk+-2.24.32/gtk/gtk-builder-convert       (refactored)
bab341
@@ -36,10 +36,11 @@
bab341
 
bab341
 Report bugs to http://bugzilla.gnome.org/."""
bab341
 
bab341
+from __future__ import print_function
bab341
 import getopt
bab341
 import os
bab341
 import sys
bab341
-
bab341
+import subprocess
bab341
 from xml.dom import minidom, Node
bab341
 
bab341
 DIALOGS = ['GtkDialog',
bab341
@@ -47,13 +48,6 @@
bab341
            'GtkMessageDialog']
bab341
 WINDOWS = ['GtkWindow'] + DIALOGS
bab341
 
bab341
-# The subprocess is only available in Python 2.4+
bab341
-try:
bab341
-    import subprocess
bab341
-    subprocess # pyflakes
bab341
-except ImportError:
bab341
-    subprocess = None
bab341
-
bab341
 def get_child_nodes(node):
bab341
     assert node.tagName == 'object'
bab341
     nodes = []
bab341
@@ -167,7 +161,11 @@
bab341
 
bab341
     def to_xml(self):
bab341
         xml = self._dom.toprettyxml("", "")
bab341
-        return xml.encode('utf-8')
bab341
+        if sys.version_info < (3, 0):
bab341
+            # Python 2: convert with explicit encoding
bab341
+            # (For Python 3, the result is left as text)
bab341
+            xml = xml.encode('utf-8')
bab341
+        return xml
bab341
 
bab341
     #
bab341
     # Private
bab341
@@ -259,7 +257,7 @@
bab341
         for node in objects:
bab341
             self._convert(node.getAttribute("class"), node)
bab341
             if self._get_object(node.getAttribute('id')) is not None:
bab341
-		print "WARNING: duplicate id \"" + node.getAttribute('id') + "\""
bab341
+                print("WARNING: duplicate id \"" + node.getAttribute('id') + "\"")
bab341
             self.objects[node.getAttribute('id')] = node
bab341
 
bab341
         # Convert Gazpachos UI tag
bab341
@@ -272,13 +270,9 @@
bab341
 
bab341
         # Output the newly created root objects and sort them
bab341
         # by attribute id
bab341
-        # FIXME: Use sorted(self.root_objects,
bab341
-        #                   key=lambda n: n.getAttribute('id'),
bab341
-        #                   reverse=True):
bab341
-        # when we can depend on python 2.4 or higher
bab341
-        root_objects = self.root_objects[:]
bab341
-        root_objects.sort(lambda a, b: cmp(b.getAttribute('id'),
bab341
-                                           a.getAttribute('id')))
bab341
+        root_objects = sorted(self.root_objects,
bab341
+                              key=lambda n: n.getAttribute('id'),
bab341
+                              reverse=True)
bab341
         for obj in root_objects:
bab341
             self._interface.childNodes.insert(0, obj)
bab341
 
bab341
@@ -461,8 +455,8 @@
bab341
             if signal_name in ['activate', 'toggled']:
bab341
                 action.appendChild(signal)
bab341
             else:
bab341
-                print 'Unhandled signal %s::%s' % (node.getAttribute('class'),
bab341
-                                                   signal_name)
bab341
+                print('Unhandled signal %s::%s' % (node.getAttribute('class'),
bab341
+                                                   signal_name))
bab341
 
bab341
         if not uimgr.childNodes:
bab341
             child = self._dom.createElement('child')
bab341
@@ -481,8 +475,8 @@
bab341
         for accelerator in get_accelerator_nodes(node):
bab341
             signal_name = accelerator.getAttribute('signal')
bab341
             if signal_name != 'activate':
bab341
-                print 'Unhandled accelerator signal for %s::%s' % (
bab341
-                    node.getAttribute('class'), signal_name)
bab341
+                print('Unhandled accelerator signal for %s::%s' % (
bab341
+                    node.getAttribute('class'), signal_name))
bab341
                 continue
bab341
             accelerator.removeAttribute('signal')
bab341
             child.appendChild(accelerator)
bab341
@@ -741,13 +735,14 @@
bab341
 
bab341
     s = subprocess.Popen([filename, '--format', '-'],
bab341
                          stdin=subprocess.PIPE,
bab341
-                         stdout=subprocess.PIPE)
bab341
+                         stdout=subprocess.PIPE,
bab341
+                         universal_newlines=True)
bab341
     s.stdin.write(output)
bab341
     s.stdin.close()
bab341
     return s.stdout.read()
bab341
 
bab341
 def usage():
bab341
-    print __doc__
bab341
+    print(__doc__)
bab341
 
bab341
 def main(args):
bab341
     try:
bab341
@@ -788,10 +783,10 @@
bab341
 
bab341
     xml = _indent(conv.to_xml())
bab341
     if output_filename == "-":
bab341
-        print xml
bab341
+        print(xml)
bab341
     else:
bab341
         open(output_filename, 'w').write(xml)
bab341
-        print "Wrote", output_filename
bab341
+        print("Wrote", output_filename)
bab341
 
bab341
     return 0
bab341