Examples

Calling a method

import dcar

with dcar.Bus() as bus:
    reply = bus.method_call('/org/freedesktop/DBus',
                            'org.freedesktop.DBus',
                            'GetId',
                            'org.freedesktop.DBus')
    print(reply[0])

Subscribing to a signal

import dcar

bus = dcar.Bus()
bus.register_signal(dcar.MatchRule(interface='org.freedesktop.DBus',
                                   signal_name='NameAcquired'),
                    lambda x: print('NameAcquired:', x.args[0]),
                    unicast=True)
with bus:
    bus.register_signal(dcar.MatchRule(interface='org.freedesktop.DBus',
                                       signal_name='NameOwnerChanged'),
                        lambda x: print('NameOwnerChanged:', x.args))
    reply = bus.method_call('/org/freedesktop/DBus',
                            'org.freedesktop.DBus',
                            'RequestName',
                            'org.freedesktop.DBus',
                            signature='su',
                            args=('test.Dummy', 0))
    print(reply[0])
    bus.block()

Export a method

import dcar

def echo(bus, msginfo):
    bus.method_return(msginfo.serial, msginfo.sender,
                      signature='s', args=msginfo.args)

bus = dcar.Bus()
bus.register_method('/test/Dummy', 'test.Dummy', 'Echo', echo, 's')
with bus:
    reply = bus.method_call('/org/freedesktop/DBus',
                            'org.freedesktop.DBus',
                            'RequestName',
                            'org.freedesktop.DBus',
                            signature='su',
                            args=('test.Dummy', 0))
    print(reply[0])  # should be 1
    bus.block()

This method can be called like this:

import dcar

with dcar.Bus() as bus:
    reply = bus.method_call('/test/Dummy', 'test.Dummy',
                            'Echo', 'test.Dummy',
                            signature='s', args=('Hei',))
    print(reply[0])