@@ -82,31 +82,31 @@ import asyncio
8282
8383
8484async def main ():
85- bus = await MessageBus().connect()
86- # the introspection xml would normally be included in your project, but
87- # this is convenient for development
88- introspection = await bus.introspect(' org.mpris.MediaPlayer2.vlc' , ' /org/mpris/MediaPlayer2' )
85+ async with MessageBus() as bus:
86+ # the introspection xml would normally be included in your project, but
87+ # this is convenient for development
88+ introspection = await bus.introspect(' org.mpris.MediaPlayer2.vlc' , ' /org/mpris/MediaPlayer2' )
8989
90- obj = bus.get_proxy_object(' org.mpris.MediaPlayer2.vlc' , ' /org/mpris/MediaPlayer2' , introspection)
91- player = obj.get_interface(' org.mpris.MediaPlayer2.Player' )
92- properties = obj.get_interface(' org.freedesktop.DBus.Properties' )
90+ obj = bus.get_proxy_object(' org.mpris.MediaPlayer2.vlc' , ' /org/mpris/MediaPlayer2' , introspection)
91+ player = obj.get_interface(' org.mpris.MediaPlayer2.Player' )
92+ properties = obj.get_interface(' org.freedesktop.DBus.Properties' )
9393
94- # call methods on the interface (this causes the media player to play)
95- await player.call_play()
94+ # call methods on the interface (this causes the media player to play)
95+ await player.call_play()
9696
97- volume = await player.get_volume()
98- print (f ' current volume: { volume} , setting to 0.5 ' )
97+ volume = await player.get_volume()
98+ print (f ' current volume: { volume} , setting to 0.5 ' )
9999
100- await player.set_volume(0.5 )
100+ await player.set_volume(0.5 )
101101
102- # listen to signals
103- def on_properties_changed (interface_name , changed_properties , invalidated_properties ):
104- for changed, variant in changed_properties.items():
105- print (f ' property changed: { changed} - { variant.value} ' )
102+ # listen to signals
103+ def on_properties_changed (interface_name , changed_properties , invalidated_properties ):
104+ for changed, variant in changed_properties.items():
105+ print (f ' property changed: { changed} - { variant.value} ' )
106106
107- properties.on_properties_changed(on_properties_changed)
107+ properties.on_properties_changed(on_properties_changed)
108108
109- await asyncio.Event().wait()
109+ await asyncio.Event().wait()
110110
111111asyncio.run(main())
112112```
@@ -155,13 +155,13 @@ class ExampleInterface(ServiceInterface):
155155 return ' hello'
156156
157157async def main ():
158- bus = await MessageBus().connect()
159- interface = ExampleInterface(' test.interface' )
160- bus.export(' /test/path' , interface)
161- # now that we are ready to handle requests, we can request name from D-Bus
162- await bus.request_name(' test.name' )
163- # wait indefinitely
164- await asyncio.Event().wait()
158+ async with MessageBus() as bus:
159+ interface = ExampleInterface(' test.interface' )
160+ bus.export(' /test/path' , interface)
161+ # now that we are ready to handle requests, we can request name from D-Bus
162+ await bus.request_name(' test.name' )
163+ # wait indefinitely
164+ await asyncio.Event().wait()
165165
166166asyncio.run(main())
167167```
@@ -181,13 +181,12 @@ import json
181181
182182
183183async def main ():
184- bus = await MessageBus().connect()
185-
186- reply = await bus.call(
187- Message(destination = ' org.freedesktop.DBus' ,
188- path = ' /org/freedesktop/DBus' ,
189- interface = ' org.freedesktop.DBus' ,
190- member = ' ListNames' ))
184+ async with MessageBus() as bus:
185+ reply = await bus.call(
186+ Message(destination = ' org.freedesktop.DBus' ,
187+ path = ' /org/freedesktop/DBus' ,
188+ interface = ' org.freedesktop.DBus' ,
189+ member = ' ListNames' ))
191190
192191 if reply.message_type == MessageType.ERROR :
193192 raise Exception (reply.body[0 ])
0 commit comments