From f0fb65c97169aeb8cd9e86a9e6d59da9df48e3b2 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Fri, 31 Oct 2014 14:54:48 -0700 Subject: [PATCH 2/2] Make examples compatible with python3 as well --- examples/example-async-client.py | 18 ++++++++++-------- examples/example-client.py | 12 +++++++----- examples/example-service.py | 10 ++++++---- examples/example-signal-emitter.py | 8 +++++--- examples/example-signal-recipient.py | 20 +++++++++++--------- examples/gconf-proxy-client.py | 4 +++- examples/gconf-proxy-service2.py | 8 +++++--- examples/list-system-services.py | 4 +++- examples/unix-fd-client.py | 10 ++++++---- examples/unix-fd-service.py | 18 ++++++++++-------- 10 files changed, 66 insertions(+), 46 deletions(-) diff --git a/examples/example-async-client.py b/examples/example-async-client.py index c9e0a4f..4bb55b3 100755 --- a/examples/example-async-client.py +++ b/examples/example-async-client.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + usage = """Usage: python example-service.py & python example-async-client.py @@ -32,7 +34,7 @@ python example-client.py --exit-service import sys import traceback -import gobject +from gi.repository import GLib as gobject import dbus import dbus.mainloop.glib @@ -43,7 +45,7 @@ def handle_hello_reply(r): global hello_replied hello_replied = True - print str(r) + print(r) if hello_replied and raise_replied: loop.quit() @@ -54,8 +56,8 @@ def handle_hello_error(e): hello_replied = True failed = True - print "HelloWorld raised an exception! That's not meant to happen..." - print "\t", str(e) + print("HelloWorld raised an exception! That's not meant to happen...") + print("\t", e) if hello_replied and raise_replied: loop.quit() @@ -66,7 +68,7 @@ def handle_raise_reply(): raise_replied = True failed = True - print "RaiseException returned normally! That's not meant to happen..." + print("RaiseException returned normally! That's not meant to happen...") if hello_replied and raise_replied: loop.quit() @@ -75,8 +77,8 @@ def handle_raise_error(e): global raise_replied raise_replied = True - print "RaiseException raised an exception as expected:" - print "\t", str(e) + print("RaiseException raised an exception as expected:") + print("\t", e) if hello_replied and raise_replied: loop.quit() @@ -104,7 +106,7 @@ if __name__ == '__main__': remote_object = bus.get_object("com.example.SampleService","/SomeObject") except dbus.DBusException: traceback.print_exc() - print usage + print(usage) sys.exit(1) # Make the method call after a short delay diff --git a/examples/example-client.py b/examples/example-client.py index 262f892..9104b54 100755 --- a/examples/example-client.py +++ b/examples/example-client.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + usage = """Usage: python example-service.py & python example-client.py @@ -46,7 +48,7 @@ def main(): dbus_interface = "com.example.SampleInterface") except dbus.DBusException: print_exc() - print usage + print(usage) sys.exit(1) print (hello_reply_list) @@ -56,20 +58,20 @@ def main(): hello_reply_tuple = iface.GetTuple() - print hello_reply_tuple + print(hello_reply_tuple) hello_reply_dict = iface.GetDict() - print hello_reply_dict + print(hello_reply_dict) # D-Bus exceptions are mapped to Python exceptions try: iface.RaiseException() except dbus.DBusException as e: - print str(e) + print(e) # introspection is automatically supported - print remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable") + print(remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")) if sys.argv[1:] == ['--exit-service']: iface.Exit() diff --git a/examples/example-service.py b/examples/example-service.py index c42b526..7c86380 100755 --- a/examples/example-service.py +++ b/examples/example-service.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + usage = """Usage: python example-service.py & python example-client.py @@ -30,7 +32,7 @@ python example-client.py --exit-service # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import gobject +from gi.repository import GLib as gobject import dbus import dbus.service @@ -44,7 +46,7 @@ class SomeObject(dbus.service.Object): @dbus.service.method("com.example.SampleInterface", in_signature='s', out_signature='as') def HelloWorld(self, hello_message): - print (str(hello_message)) + print(hello_message) return ["Hello", " from example-service.py", "with unique name", session_bus.get_unique_name()] @@ -78,6 +80,6 @@ if __name__ == '__main__': object = SomeObject(session_bus, '/SomeObject') mainloop = gobject.MainLoop() - print "Running example service." - print usage + print("Running example service.") + print(usage) mainloop.run() diff --git a/examples/example-signal-emitter.py b/examples/example-signal-emitter.py index bb832aa..03fddaa 100755 --- a/examples/example-signal-emitter.py +++ b/examples/example-signal-emitter.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + usage = """Usage: python example-signal-emitter.py & python example-signal-recipient.py @@ -29,7 +31,7 @@ python example-signal-recipient.py --exit-service # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import gobject +from gi.repository import GLib as gobject import dbus import dbus.service @@ -64,6 +66,6 @@ if __name__ == '__main__': object = TestObject(session_bus) loop = gobject.MainLoop() - print "Running example signal emitter service." - print usage + print("Running example signal emitter service.") + print(usage) loop.run() diff --git a/examples/example-signal-recipient.py b/examples/example-signal-recipient.py index 4810f5e..8ddba41 100755 --- a/examples/example-signal-recipient.py +++ b/examples/example-signal-recipient.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + usage = """Usage: python example-signal-emitter.py & python example-signal-recipient.py @@ -32,16 +34,16 @@ python example-signal-recipient.py --exit-service import sys import traceback -import gobject +from gi.repository import GLib as gobject import dbus import dbus.mainloop.glib def handle_reply(msg): - print msg + print(msg) def handle_error(e): - print str(e) + print(e) def emit_signal(): #call the emitHelloSignal method @@ -56,20 +58,20 @@ def emit_signal(): return False def hello_signal_handler(hello_string): - print ("Received signal (by connecting using remote object) and it says: " + print("Received signal (by connecting using remote object) and it says: " + hello_string) def catchall_signal_handler(*args, **kwargs): - print ("Caught signal (in catchall handler) " + print("Caught signal (in catchall handler) " + kwargs['dbus_interface'] + "." + kwargs['member']) for arg in args: - print " " + str(arg) + print(" " + str(arg)) def catchall_hello_signals_handler(hello_string): - print "Received a hello signal and it says " + hello_string + print("Received a hello signal and it says " + hello_string) def catchall_testservice_interface_handler(hello_string, dbus_message): - print "com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member() + print("com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()) if __name__ == '__main__': @@ -82,7 +84,7 @@ if __name__ == '__main__': object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello") except dbus.DBusException: traceback.print_exc() - print usage + print(usage) sys.exit(1) #lets make a catchall diff --git a/examples/gconf-proxy-client.py b/examples/gconf-proxy-client.py index 2e440f4..e9bbf8b 100755 --- a/examples/gconf-proxy-client.py +++ b/examples/gconf-proxy-client.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + # Copyright (C) 2004-2006 Red Hat Inc. # Copyright (C) 2005-2007 Collabora Ltd. # @@ -32,4 +34,4 @@ gconf_key_object = dbus.Interface(bus.get_object("org.gnome.GConf.Example", "/or value = gconf_key_object.getString() -print ("Value of GConf key %s is %s" % (gconf_key, value)) +print("Value of GConf key %s is %s" % (gconf_key, value)) diff --git a/examples/gconf-proxy-service2.py b/examples/gconf-proxy-service2.py index 7ce132b..133c5c4 100755 --- a/examples/gconf-proxy-service2.py +++ b/examples/gconf-proxy-service2.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + # Copyright (C) 2004-2006 Red Hat Inc. # Copyright (C) 2005-2007 Collabora Ltd. # @@ -27,8 +29,8 @@ import dbus import dbus.mainloop.glib import dbus.service -import gobject -import gconf +from gi.repository import GLib as gobject +from gi.repository import GConf as gconf dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) @@ -38,7 +40,7 @@ name = dbus.service.BusName("org.gnome.GConf.Example", dbus.SessionBus()) class GConfObject(dbus.service.FallbackObject): def __init__(self): dbus.service.FallbackObject.__init__(self, dbus.SessionBus(), '/org/gnome/GConf') - self.client = gconf.client_get_default() + self.client = gconf.Client.get_default() @dbus.service.method("org.gnome.GConf", in_signature='', out_signature='s', rel_path_keyword='object_path') def getString(self, object_path): diff --git a/examples/list-system-services.py b/examples/list-system-services.py index a8a1829..21515ba 100755 --- a/examples/list-system-services.py +++ b/examples/list-system-services.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + """Usage: python list-system-services.py [--session|--system] List services on the system bus (default) or the session bus.""" @@ -63,7 +65,7 @@ def main(argv): services = dbus_iface.ListNames() services.sort() for service in services: - print service + print(service) if __name__ == '__main__': main(sys.argv) diff --git a/examples/unix-fd-client.py b/examples/unix-fd-client.py index ce1011d..9ef7279 100755 --- a/examples/unix-fd-client.py +++ b/examples/unix-fd-client.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + import time usage = """Usage: @@ -46,18 +48,18 @@ def main(): except dbus.DBusException: print_exc() - print usage + print(usage) sys.exit(1) iface = dbus.Interface(remote_object, "com.example.SampleInterface") # UnixFd is an opaque object that takes care of received fd fd_object = iface.GetFd() - print fd_object + print(fd_object) # Once we take the fd number, we are in charge of closing it! fd = fd_object.take() - print fd + print(fd) # We want to encapsulate the integer fd into a Python file or socket object f = os.fdopen(fd, "r") @@ -70,7 +72,7 @@ def main(): # otherwise it 'leaks' (stays open until program exits). f.seek(0) - print f.read() + print(f.read()) if __name__ == '__main__': main() diff --git a/examples/unix-fd-service.py b/examples/unix-fd-service.py index 7850049..df7ce0a 100755 --- a/examples/unix-fd-service.py +++ b/examples/unix-fd-service.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +from __future__ import print_function + usage = """Usage: python unix-fd-service.py & python unix-fd-client.py @@ -29,7 +31,7 @@ python unix-fd-client.py # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import gobject +from gi.repository import GLib as gobject import dbus import dbus.service @@ -47,20 +49,20 @@ class SomeObject(dbus.service.Object): self.counter = (self.counter + 1) % 3 if self.counter == 0: - print "sending UnixFd(filelike)" + print("sending UnixFd(filelike)") return dbus.types.UnixFd(f) elif self.counter == 1: - print "sending int" + print("sending int") return f.fileno() else: - print "sending UnixFd(int)" + print("sending UnixFd(int)") return dbus.types.UnixFd(f.fileno()) if len(sys.argv) < 2: - print usage + print(usage) sys.exit(1) -f = file(sys.argv[1], "r") +f = open(sys.argv[1], "r") if __name__ == '__main__': dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) @@ -70,6 +72,6 @@ if __name__ == '__main__': object = SomeObject(session_bus, '/SomeObject') mainloop = gobject.MainLoop() - print "Running fd service." - print usage + print("Running fd service.") + print(usage) mainloop.run() -- 2.1.1