sample_python_dbus_next.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import asyncio
  2. from dbus_next.aio import MessageBus
  3. from dbus_next.service import ServiceInterface, method
  4. import dbus_next.introspection as intr
  5. from dbus_next import BusType
  6. class SampleInterface(ServiceInterface):
  7. def __init__(self):
  8. super().__init__('test.interface')
  9. @method()
  10. def Ping(self):
  11. pass
  12. @method()
  13. def ConcatStrings(self, what1: 's', what2: 's') -> 's': # noqa: F821
  14. return what1 + what2
  15. async def main():
  16. bus_name = 'dbus.next.sample'
  17. bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
  18. bus2 = await MessageBus(bus_type=BusType.SYSTEM).connect()
  19. await bus.request_name(bus_name)
  20. service_interface = SampleInterface()
  21. bus.export('/test/path', service_interface)
  22. introspection = await bus2.introspect(bus_name, '/test/path')
  23. assert type(introspection) is intr.Node
  24. obj = bus2.get_proxy_object(bus_name, '/test/path', introspection)
  25. interface = obj.get_interface(service_interface.name)
  26. result = await interface.call_ping()
  27. assert result is None
  28. result = await interface.call_concat_strings('hello ', 'world')
  29. assert result == 'hello world'
  30. asyncio.run(main())