Project

General

Profile

« Previous | Next » 

Revision cd10f9a6

Added by David Sorber almost 8 years ago

Adding simple Python-based dbus example.

View differences:

software/dbus_test/client.py
#!/usr/bin/env python3
import dbus
class Client():
def __init__(self):
bus = dbus.SystemBus()
service = bus.get_object('com.example.service', "/com/example/service")
self._message = service.get_dbus_method('get_message', 'com.example.service.Message')
self._quit = service.get_dbus_method('quit', 'com.example.service.Quit')
def run(self):
print("Mesage from service:", self._message())
self._quit()
if __name__ == "__main__":
Client().run()
software/dbus_test/com.example.service.conf
<?xml version="1.0"?> <!--*-nxml-*-->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<!--
This file goes here:
/etc/dbus-1/system.d/com.example.service.conf
-->
<busconfig>
<policy user="root">
<allow own="com.example.service"/>
<allow send_destination="com.example.service"/>
<allow receive_sender="com.example.service"/>
</policy>
<policy context="default">
<allow send_destination="com.example.service"/>
<allow receive_sender="com.example.service"/>
</policy>
</busconfig>
software/dbus_test/service.py
#!/usr/bin/env python3
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GObject as gobject
class Service(dbus.service.Object):
def __init__(self, message):
self._message = message
def run(self):
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus_name = dbus.service.BusName("com.example.service", dbus.SystemBus())
dbus.service.Object.__init__(self, bus_name, "/com/example/service")
self._loop = gobject.MainLoop()
print("Service running...")
self._loop.run()
print("Service stopped")
@dbus.service.method("com.example.service.Message", in_signature='', out_signature='s')
def get_message(self):
print(" sending message")
return self._message
@dbus.service.method("com.example.service.Quit", in_signature='', out_signature='')
def quit(self):
print(" shutting down")
self._loop.quit()
if __name__ == "__main__":
Service("This is the service").run()

Also available in: Unified diff