-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus.py
32 lines (24 loc) · 972 Bytes
/
bus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import dbus_next
class Bus:
def __init__(self):
self._bus = dbus_next.aio.MessageBus(bus_type=dbus_next.BusType.SYSTEM)
self._handlers = set()
async def connect(self):
await self._bus.connect()
self._bus.add_message_handler(self._handle_message)
def disconnect(self):
self._bus.remove_message_handler(self._handle_message)
self._bus.disconnect()
def add_message_handler(self, handler):
self._handlers.add(handler)
def remove_message_handler(self, handler):
self._handlers.remove(handler)
async def call(self, **kwargs):
msg = dbus_next.Message(**kwargs)
reply = await self._bus.call(msg)
if reply.error_name:
raise RuntimeError(f'{reply.error_name}: {reply.body[0]}')
return reply.body
def _handle_message(self, msg):
for handler in self._handlers:
handler(msg.path, msg.interface, msg.member, msg.body)