diff options
author | Simon McVittie <smcv@collabora.com> | 2021-07-20 10:13:24 +0100 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2021-07-20 10:34:20 +0100 |
commit | ce5fa75f01a7197908f462a422237af09cb73a73 (patch) | |
tree | 725f4360ca7c605119c87ce181743589736ca9a0 | |
parent | 2860c6873169ffc0f950b748f1865ad39402840c (diff) | |
download | dbus-python-ce5fa75f01a7197908f462a422237af09cb73a73.tar.gz |
examples: Convert to Python 3
Python 2 reached EOL more than 18 months ago.
For the gconf example, which is only still here because it provides an
example of implementing a FallbackObject, use a mock implementation if
the deprecated gconf module is no longer available.
Signed-off-by: Simon McVittie <smcv@collabora.com>
-rwxr-xr-x | examples/example-async-client.py | 24 | ||||
-rwxr-xr-x | examples/example-client.py | 22 | ||||
-rwxr-xr-x | examples/example-service.py | 18 | ||||
-rwxr-xr-x | examples/example-signal-emitter.py | 14 | ||||
-rwxr-xr-x | examples/example-signal-recipient.py | 28 | ||||
-rwxr-xr-x | examples/gconf-proxy-client.py | 6 | ||||
-rwxr-xr-x | examples/gconf-proxy-service2.py | 28 | ||||
-rwxr-xr-x | examples/list-system-services.py | 8 | ||||
-rwxr-xr-x | examples/unix-fd-client.py | 16 | ||||
-rwxr-xr-x | examples/unix-fd-service.py | 22 |
10 files changed, 113 insertions, 73 deletions
diff --git a/examples/example-async-client.py b/examples/example-async-client.py index 0d024c3..7b97943 100755 --- a/examples/example-async-client.py +++ b/examples/example-async-client.py @@ -1,9 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function usage = """Usage: -python example-service.py & -python example-async-client.py -python example-client.py --exit-service +python3 example-service.py & +python3 example-async-client.py +python3 example-client.py --exit-service """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -45,7 +47,7 @@ def handle_hello_reply(r): global hello_replied hello_replied = True - print str(r) + print("async client:", str(r)) if hello_replied and raise_replied: loop.quit() @@ -56,8 +58,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("async client: HelloWorld raised an exception! That's not meant to happen...") + print("\t", str(e)) if hello_replied and raise_replied: loop.quit() @@ -68,7 +70,7 @@ def handle_raise_reply(): raise_replied = True failed = True - print "RaiseException returned normally! That's not meant to happen..." + print("async client: RaiseException returned normally! That's not meant to happen...") if hello_replied and raise_replied: loop.quit() @@ -77,8 +79,8 @@ def handle_raise_error(e): global raise_replied raise_replied = True - print "RaiseException raised an exception as expected:" - print "\t", str(e) + print("async client: RaiseException raised an exception as expected:") + print("\t", str(e)) if hello_replied and raise_replied: loop.quit() @@ -106,7 +108,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 d9ff776..6f7ade2 100755 --- a/examples/example-client.py +++ b/examples/example-client.py @@ -1,9 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function usage = """Usage: -python example-service.py & -python example-client.py -python example-client.py --exit-service +python3 example-service.py & +python3 example-client.py +python3 example-client.py --exit-service """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -48,30 +50,30 @@ def main(): dbus_interface = "com.example.SampleInterface") except dbus.DBusException: print_exc() - print usage + print(usage) sys.exit(1) - print (hello_reply_list) + print("client:", hello_reply_list) # ... or create an Interface wrapper for the remote object iface = dbus.Interface(remote_object, "com.example.SampleInterface") hello_reply_tuple = iface.GetTuple() - print hello_reply_tuple + print("client:", hello_reply_tuple) hello_reply_dict = iface.GetDict() - print hello_reply_dict + print("client:", hello_reply_dict) # D-Bus exceptions are mapped to Python exceptions try: iface.RaiseException() except dbus.DBusException as e: - print str(e) + print("client:", str(e)) # introspection is automatically supported - print remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable") + print("client:", 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 12c81d9..e1ed325 100755 --- a/examples/example-service.py +++ b/examples/example-service.py @@ -1,10 +1,12 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function usage = """Usage: -python example-service.py & -python example-client.py -python example-async-client.py -python example-client.py --exit-service +python3 example-service.py & +python3 example-client.py +python3 example-async-client.py +python3 example-client.py --exit-service """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -46,7 +48,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("service:", str(hello_message)) return ["Hello", " from example-service.py", "with unique name", session_bus.get_unique_name()] @@ -80,6 +82,6 @@ if __name__ == '__main__': object = SomeObject(session_bus, '/SomeObject') mainloop = GLib.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 48349a8..fc3f0db 100755 --- a/examples/example-signal-emitter.py +++ b/examples/example-signal-emitter.py @@ -1,9 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function usage = """Usage: -python example-signal-emitter.py & -python example-signal-recipient.py -python example-signal-recipient.py --exit-service +python3 example-signal-emitter.py & +python3 example-signal-recipient.py +python3 example-signal-recipient.py --exit-service """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -66,6 +68,6 @@ if __name__ == '__main__': object = TestObject(session_bus) loop = GLib.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 63f53d6..6e89ace 100755 --- a/examples/example-signal-recipient.py +++ b/examples/example-signal-recipient.py @@ -1,9 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function usage = """Usage: -python example-signal-emitter.py & -python example-signal-recipient.py -python example-signal-recipient.py --exit-service +python3 example-signal-emitter.py & +python3 example-signal-recipient.py +python3 example-signal-recipient.py --exit-service """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -40,10 +42,10 @@ import dbus import dbus.mainloop.glib def handle_reply(msg): - print msg + print("recipient:", msg) def handle_error(e): - print str(e) + print("recipient:", str(e)) def emit_signal(): #call the emitHelloSignal method @@ -58,20 +60,20 @@ def emit_signal(): return False def hello_signal_handler(hello_string): - print ("Received signal (by connecting using remote object) and it says: " + print("recipient: 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("recipient: 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("recipient: 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("recipient: com.example.TestService interface says " + hello_string + " when it sent signal " + dbus_message.get_member()) if __name__ == '__main__': @@ -84,7 +86,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 8d81486..c03e947 100755 --- a/examples/gconf-proxy-client.py +++ b/examples/gconf-proxy-client.py @@ -1,4 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function # Client for gconf-proxy-service2.py. @@ -36,4 +38,4 @@ gconf_key_object = dbus.Interface(bus.get_object("com.example.GConfProxy", "/org value = gconf_key_object.getString() -print ("Value of GConf key %s is %s" % (gconf_key, value)) +print("client: 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 d31277f..74cc814 100755 --- a/examples/gconf-proxy-service2.py +++ b/examples/gconf-proxy-service2.py @@ -1,4 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function # Example of implementing an entire subtree of objects using # a FallbackObject. @@ -37,7 +39,12 @@ import dbus.mainloop.glib import dbus.service from gi.repository import GLib -import gconf + +try: + import gconf +except ImportError: + print('service: gconf not available, using mock implementation') + gconf = None dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) @@ -47,22 +54,37 @@ name = dbus.service.BusName("com.example.GConfProxy", 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() + if gconf is None: + self.client = None + else: + 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): + if self.client is None: + return '<gconf not available>' + return self.client.get_string(object_path) @dbus.service.method("org.gnome.GConf", in_signature='s', out_signature='', rel_path_keyword='object_path') def setString(self, value, object_path): + if self.client is None: + raise RuntimeError('gconf not available') + self.client.set_string(object_path, value) @dbus.service.method("org.gnome.GConf", in_signature='', out_signature='i', rel_path_keyword='object_path') def getInt(self, object_path): + if self.client is None: + return 42 + return self.client.get_int(object_path) @dbus.service.method("org.gnome.GConf", in_signature='i', out_signature='', rel_path_keyword='object_path') def setInt(self, value, object_path): + if self.client is None: + raise RuntimeError('gconf not available') + self.client.set_int(object_path, value) gconf_service = GConfObject() diff --git a/examples/list-system-services.py b/examples/list-system-services.py index 4734f5e..aad9e9a 100755 --- a/examples/list-system-services.py +++ b/examples/list-system-services.py @@ -1,6 +1,8 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -"""Usage: python list-system-services.py [--session|--system] +from __future__ import print_function + +"""Usage: python3 list-system-services.py [--session|--system] List services on the system bus (default) or the session bus.""" # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -65,7 +67,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 ad421b3..5725513 100755 --- a/examples/unix-fd-client.py +++ b/examples/unix-fd-client.py @@ -1,10 +1,12 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function import time usage = """Usage: -python unix-fd-service.py <file name> & -python unix-fd-client.py +python3 unix-fd-service.py <file name> & +python3 unix-fd-client.py """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -48,18 +50,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("client: fd_object = %s" % fd_object) # Once we take the fd number, we are in charge of closing it! fd = fd_object.take() - print fd + print("client: fd = %s" % fd) # We want to encapsulate the integer fd into a Python file or socket object f = os.fdopen(fd, "r") @@ -72,7 +74,7 @@ def main(): # otherwise it 'leaks' (stays open until program exits). f.seek(0) - print f.read() + print("client: read from fd = %r" % f.read()) if __name__ == '__main__': main() diff --git a/examples/unix-fd-service.py b/examples/unix-fd-service.py index 8b65bf2..e7a6b50 100755 --- a/examples/unix-fd-service.py +++ b/examples/unix-fd-service.py @@ -1,8 +1,10 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 + +from __future__ import print_function usage = """Usage: -python unix-fd-service.py <file name> & -python unix-fd-client.py +python3 unix-fd-service.py <file name> & +python3 unix-fd-client.py """ # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/> @@ -49,20 +51,20 @@ class SomeObject(dbus.service.Object): self.counter = (self.counter + 1) % 3 if self.counter == 0: - print "sending UnixFd(filelike)" + print("service: sending UnixFd(filelike)") return dbus.types.UnixFd(f) elif self.counter == 1: - print "sending int" + print("service: sending int") return f.fileno() else: - print "sending UnixFd(int)" + print("service: 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) @@ -72,6 +74,6 @@ if __name__ == '__main__': object = SomeObject(session_bus, '/SomeObject') mainloop = GLib.MainLoop() - print "Running fd service." - print usage + print("Running fd service.") + print(usage) mainloop.run() |