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