summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2006-12-13 14:40:32 +0000
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2006-12-13 14:40:32 +0000
commitb70364f2a37e64585c53a72e36481407e375808c (patch)
tree41e1004014cc54995bebb7c647232c5319aec60d /examples
parentc874bb37093c39e3dcc974c7bff39a36e4f7202a (diff)
dbus/examples: Move to examples/ (it's not part of the library and isn't installed)
Diffstat (limited to 'examples')
-rw-r--r--examples/.cvsignore2
-rw-r--r--examples/example-client.py51
-rw-r--r--examples/example-service.py50
-rw-r--r--examples/example-signal-emitter.py46
-rw-r--r--examples/example-signal-recipient.py77
-rw-r--r--examples/gconf-proxy-client.py15
-rw-r--r--examples/gconf-proxy-service2.py40
-rw-r--r--examples/list-system-services.py41
8 files changed, 322 insertions, 0 deletions
diff --git a/examples/.cvsignore b/examples/.cvsignore
new file mode 100644
index 0000000..282522d
--- /dev/null
+++ b/examples/.cvsignore
@@ -0,0 +1,2 @@
+Makefile
+Makefile.in
diff --git a/examples/example-client.py b/examples/example-client.py
new file mode 100644
index 0000000..aae4c3f
--- /dev/null
+++ b/examples/example-client.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+usage = """Usage:
+python example-service.py &
+python example-client.py
+python example-client.py --exit-service
+"""
+
+import sys
+from traceback import print_exc
+
+import dbus
+import dbus.mainloop.glib
+
+def main():
+ bus = dbus.SessionBus()
+
+ try:
+ remote_object = bus.get_object("com.example.SampleService",
+ "/SomeObject")
+
+ # you can either specify the dbus_interface in each call...
+ hello_reply_list = remote_object.HelloWorld("Hello from example-client.py!",
+ dbus_interface = "com.example.SampleInterface")
+ except dbus.DBusException:
+ print_exc()
+ print usage
+ sys.exit(1)
+
+ print (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
+
+ hello_reply_dict = iface.GetDict()
+
+ print hello_reply_dict
+
+ # introspection is automatically supported
+ print remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
+
+ if sys.argv[1:] == ['--exit-service']:
+ iface.Exit()
+
+if __name__ == '__main__':
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+ main()
diff --git a/examples/example-service.py b/examples/example-service.py
new file mode 100644
index 0000000..fc3ded5
--- /dev/null
+++ b/examples/example-service.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+usage = """Usage:
+python example-service.py &
+python example-client.py
+python example-client.py --exit-service
+"""
+
+import gobject
+
+import dbus
+import dbus.service
+import dbus.mainloop.glib
+
+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))
+ return ["Hello", " from example-service.py", "with unique name",
+ session_bus.get_unique_name()]
+
+ @dbus.service.method("com.example.SampleInterface",
+ in_signature='', out_signature='(ss)')
+ def GetTuple(self):
+ return ("Hello Tuple", " from example-service.py")
+
+ @dbus.service.method("com.example.SampleInterface",
+ in_signature='', out_signature='a{ss}')
+ def GetDict(self):
+ return {"first": "Hello Dict", "second": " from example-service.py"}
+
+ @dbus.service.method("com.example.SampleInterface",
+ in_signature='', out_signature='')
+ def Exit(self):
+ mainloop.quit()
+
+
+if __name__ == '__main__':
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ session_bus = dbus.SessionBus()
+ name = dbus.service.BusName("com.example.SampleService", session_bus)
+ object = SomeObject(session_bus, '/SomeObject')
+
+ mainloop = gobject.MainLoop()
+ print "Running example service."
+ print usage
+ mainloop.run()
diff --git a/examples/example-signal-emitter.py b/examples/example-signal-emitter.py
new file mode 100644
index 0000000..24384c1
--- /dev/null
+++ b/examples/example-signal-emitter.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+usage = """Usage:
+python example-signal-emitter.py &
+python example-signal-recipient.py
+python example-signal-recipient.py --exit-service
+"""
+
+import gobject
+
+import dbus
+import dbus.service
+import dbus.mainloop.glib
+
+class TestObject(dbus.service.Object):
+ def __init__(self, conn, object_path='/com/example/TestService/object'):
+ dbus.service.Object.__init__(self, conn, object_path)
+
+ @dbus.service.signal('com.example.TestService')
+ def HelloSignal(self, message):
+ # The signal is emitted when this method exits
+ # You can have code here if you wish
+ pass
+
+ @dbus.service.method('com.example.TestService')
+ def emitHelloSignal(self):
+ #you emit signals by calling the signal's skeleton method
+ self.HelloSignal('Hello')
+ return 'Signal emitted'
+
+ @dbus.service.method("com.example.TestService",
+ in_signature='', out_signature='')
+ def Exit(self):
+ loop.quit()
+
+if __name__ == '__main__':
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ session_bus = dbus.SessionBus()
+ name = dbus.service.BusName('com.example.TestService', session_bus)
+ object = TestObject(session_bus)
+
+ loop = gobject.MainLoop()
+ print "Running example signal emitter service."
+ print usage
+ loop.run()
diff --git a/examples/example-signal-recipient.py b/examples/example-signal-recipient.py
new file mode 100644
index 0000000..3d8a190
--- /dev/null
+++ b/examples/example-signal-recipient.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+
+usage = """Usage:
+python example-signal-emitter.py &
+python example-signal-recipient.py
+python example-signal-recipient.py --exit-service
+"""
+
+import sys
+import traceback
+
+import gobject
+
+import dbus
+import dbus.decorators
+import dbus.mainloop.glib
+
+def handle_reply(msg):
+ print msg
+
+def handle_error(e):
+ print str(e)
+
+def emit_signal():
+ #call the emitHelloSignal method
+ object.emitHelloSignal(dbus_interface="com.example.TestService")
+ #reply_handler = handle_reply, error_handler = handle_error)
+ # exit after waiting a short time for the signal
+ gobject.timeout_add(2000, loop.quit)
+
+ if sys.argv[1:] == ['--exit-service']:
+ object.Exit(dbus_interface='com.example.TestService')
+
+ return False
+
+def hello_signal_handler(hello_string):
+ 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) "
+ + kwargs['dbus_interface'] + "." + kwargs['member'])
+ for arg in args:
+ print " " + str(arg)
+
+def catchall_hello_signals_handler(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()
+
+
+if __name__ == '__main__':
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ bus = dbus.SessionBus()
+ try:
+ object = bus.get_object("com.example.TestService","/com/example/TestService/object")
+
+ object.connect_to_signal("HelloSignal", hello_signal_handler, dbus_interface="com.example.TestService", arg0="Hello")
+ except dbus.DBusException:
+ traceback.print_exc()
+ print usage
+ sys.exit(1)
+
+ #lets make a catchall
+ bus.add_signal_receiver(catchall_signal_handler, interface_keyword='dbus_interface', member_keyword='member')
+
+ bus.add_signal_receiver(catchall_hello_signals_handler, dbus_interface = "com.example.TestService", signal_name = "HelloSignal")
+
+ bus.add_signal_receiver(catchall_testservice_interface_handler, dbus_interface = "com.example.TestService", message_keyword='dbus_message')
+
+ # Tell the remote object to emit the signal after a short delay
+ gobject.timeout_add(2000, emit_signal)
+
+ loop = gobject.MainLoop()
+ loop.run()
diff --git a/examples/gconf-proxy-client.py b/examples/gconf-proxy-client.py
new file mode 100644
index 0000000..222c96e
--- /dev/null
+++ b/examples/gconf-proxy-client.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+
+print "WARNING: this hasn't been updated to current API yet, and might not work"
+
+import dbus
+
+gconf_key = "/desktop/gnome/file_views/icon_theme"
+
+bus = dbus.SessionBus()
+gconf_service = bus.get_service("org.gnome.GConf")
+gconf_key_object = gconf_service.get_object("/org/gnome/GConf" + gconf_key, "org.gnome.GConf")
+
+value = gconf_key_object.getString()
+
+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
new file mode 100644
index 0000000..36a323a
--- /dev/null
+++ b/examples/gconf-proxy-service2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+print "WARNING: this hasn't been updated to current API yet, and might not work"
+#FIXME: doesn't work with the new bindings
+import dbus
+
+import gobject
+import gconf
+
+class GConfService(dbus.Service):
+
+ def __init__(self):
+ dbus.Service.__init__(self, "org.gnome.GConf", dbus.SessionBus())
+
+ gconf_object_tree = self.GConfObjectTree(self)
+
+ class GConfObjectTree(dbus.ObjectTree):
+ def __init__(self, service):
+ dbus.ObjectTree.__init__(self, "/org/gnome/GConf", service)
+
+ self.client = gconf.client_get_default()
+
+ def object_method_called(self, message, object_path, method_name, argument_list):
+ print ("Method %s called on GConf key %s" % (method_name, object_path))
+
+ if "getString" == method_name:
+ return self.client.get_string(object_path)
+ elif "setString" == method_name:
+ self.client.set_int(object_path, argument_list[0])
+ elif "getInt" == method_name:
+ return self.client.get_int(object_path)
+ elif "setInt" == method_name:
+ self.client.set_int(object_path, argument_list[0])
+
+gconf_service = GConfService()
+
+print ("GConf Proxy service started.")
+print ("Run 'gconf-proxy-client.py' to fetch a GConf key through the proxy...")
+
+mainloop = gobject.MainLoop()
+mainloop.run()
diff --git a/examples/list-system-services.py b/examples/list-system-services.py
new file mode 100644
index 0000000..da4111b
--- /dev/null
+++ b/examples/list-system-services.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+
+"""Usage: python list-system-services.py [--session|--system]
+List services on the system bus (default) or the session bus."""
+
+import sys
+
+import dbus
+
+def main(argv):
+ factory = dbus.SystemBus
+
+ if len(argv) > 2:
+ sys.exit(__doc__)
+ elif len(argv) == 2:
+ if argv[1] == '--session':
+ factory = dbus.SessionBus
+ elif argv[1] != 'system':
+ sys.exit(__doc__)
+
+ # Get a connection to the system or session bus as appropriate
+ bus = factory()
+
+ # Get a reference to the desktop bus' standard object, denoted
+ # by the path /org/freedesktop/DBus.
+ dbus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
+
+ # The object /org/freedesktop/DBus
+ # implements the 'org.freedesktop.DBus' interface
+ dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
+
+ # One of the member functions in the org.freedesktop.DBus interface
+ # is ListServices(), which provides a list of all the other services
+ # registered on this bus. Call it, and print the list.
+ services = dbus_object.ListNames()
+ services.sort()
+ for service in services:
+ print service
+
+if __name__ == '__main__':
+ main(sys.argv)