11import os
2- import pytest
32import xml .etree .ElementTree as ET
43
54from dbus_fast import (
1211from dbus_fast .constants import ErrorType
1312from dbus_fast .errors import DBusError
1413from dbus_fast .message_bus import BaseMessageBus
15- from dbus_fast .proxy_object import BaseProxyObject , BaseProxyInterface
14+ from dbus_fast .proxy_object import BaseProxyInterface , BaseProxyObject
1615
1716with open (f"{ os .path .dirname (__file__ )} /data/strict-introspection.xml" ) as f :
1817 strict_data = f .read ()
@@ -154,30 +153,27 @@ def test_default_interfaces():
154153
155154
156155class MockMessageBus (BaseMessageBus ):
157-
158- def __init__ (self ,
159- nodes : dict [str , dict [str , intr .Node ]]
160- ) -> None :
156+ def __init__ (self , nodes : dict [str , dict [str , intr .Node ]]) -> None :
161157 super ().__init__ (ProxyObject = MockProxyObject )
162158 self .nodes = nodes
163159 self .introspect_count = 0
160+
164161 # disable bus name tracking for testing purposes
165162 class MockNameOwners :
166163 @staticmethod
167164 def get (key , default ):
168- return ':'
165+ return ":"
166+
169167 self ._name_owners = MockNameOwners
170168
171169 def introspect_sync (self , bus_name : str , path : str ) -> intr .Node :
172170 self .introspect_count = self .introspect_count + 1
173171 service = self .nodes .get (bus_name )
174172 if service is None :
175- raise DBusError (ErrorType .NAME_HAS_NO_OWNER ,
176- f"unknown service: { bus_name } " )
173+ raise DBusError (ErrorType .NAME_HAS_NO_OWNER , f"unknown service: { bus_name } " )
177174 node = service .get (path )
178175 if node is None :
179- raise DBusError (ErrorType .UNKNOWN_OBJECT ,
180- f"unknown object: { path } " )
176+ raise DBusError (ErrorType .UNKNOWN_OBJECT , f"unknown object: { path } " )
181177 return node
182178
183179 def _setup_socket (self ) -> None :
@@ -188,7 +184,6 @@ def _init_high_level_client(self) -> None:
188184
189185
190186class MockProxyInterface (BaseProxyInterface ):
191-
192187 def _add_method (self , intr_method : intr .Method ) -> None :
193188 pass
194189
@@ -197,21 +192,22 @@ def _add_property(self, intr_property: intr.Property) -> None:
197192
198193
199194class MockProxyObject (BaseProxyObject ):
200-
201- def __init__ ( self ,
195+ def __init__ (
196+ self ,
202197 bus_name : str ,
203198 path : str ,
204199 introspection : intr .Node | str | ET .Element | None ,
205- bus : BaseMessageBus
200+ bus : BaseMessageBus ,
206201 ) -> None :
207202 super ().__init__ (bus_name , path , introspection , bus , MockProxyInterface )
208203
209204
210205def test_inline_child ():
211- bus = MockMessageBus ({
212- 'com.example' : {
213- '/com/example/parent_object' : intr .Node .parse (
214- """<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
206+ bus = MockMessageBus (
207+ {
208+ "com.example" : {
209+ "/com/example/parent_object" : intr .Node .parse (
210+ """<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
215211 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
216212<node name="/com/example/parent_object">
217213 <interface name="com.example.ParentInterface">
@@ -222,78 +218,84 @@ def test_inline_child():
222218 <property name="ChildProperty" type="i"/>
223219 </interface>
224220 </node>
225- </node>""" )
221+ </node>"""
222+ )
226223 }
227- })
228- introspection = bus . introspect_sync ( 'com.example' ,
229- ' /com/example/parent_object' )
224+ }
225+ )
226+ introspection = bus . introspect_sync ( "com.example" , " /com/example/parent_object" )
230227 assert bus .introspect_count == 1
231228
232- parent = bus .get_proxy_object ('com.example' , '/com/example/parent_object' ,
233- introspection )
234- assert parent .bus_name == 'com.example'
235- assert parent .path == '/com/example/parent_object'
236- assert parent .child_paths == [
237- '/com/example/parent_object/child_object'
238- ]
239- interface = parent .get_interface ('com.example.ParentInterface' )
240- assert interface .bus_name == 'com.example'
241- assert interface .path == '/com/example/parent_object'
229+ parent = bus .get_proxy_object (
230+ "com.example" , "/com/example/parent_object" , introspection
231+ )
232+ assert parent .bus_name == "com.example"
233+ assert parent .path == "/com/example/parent_object"
234+ assert parent .child_paths == ["/com/example/parent_object/child_object" ]
235+ interface = parent .get_interface ("com.example.ParentInterface" )
236+ assert interface .bus_name == "com.example"
237+ assert interface .path == "/com/example/parent_object"
242238 assert [method .name for method in interface .introspection .methods ] == [
243- ' ParentMethod'
244- ]
239+ " ParentMethod"
240+ ]
245241
246242 child = next (iter (parent .get_children ()))
247- assert child .bus_name == ' com.example'
248- assert child .path == ' /com/example/parent_object/child_object'
249- interface = child .get_interface (' com.example.ChildInterface' )
243+ assert child .bus_name == " com.example"
244+ assert child .path == " /com/example/parent_object/child_object"
245+ interface = child .get_interface (" com.example.ChildInterface" )
250246 assert [prop .name for prop in interface .introspection .properties ] == [
251- ' ChildProperty'
252- ]
247+ " ChildProperty"
248+ ]
253249
254250 # getting the inline child should not have required another introspection
255251 assert bus .introspect_count == 1
256252
257253
258254def test_noninline_child ():
259255 obj0_node = intr .Node .parse (strict_data )
260- bus = MockMessageBus ({
261- 'com.example' : {
256+ bus = MockMessageBus (
257+ {
258+ "com.example" : {
262259 obj0_node .name : obj0_node ,
263- f' { obj0_node .name } /child_of_sample_object' : intr .Node .parse (
264- f"""<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
260+ f" { obj0_node .name } /child_of_sample_object" : intr .Node .parse (
261+ f"""<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
265262 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
266263<node name="{ obj0_node .name } /child_of_sample_object">
267264 <interface name="com.example.ChildInterface">
268265 <property name="ChildProperty" type="i"/>
269266 </interface>
270- </node>""" ),
267+ </node>"""
268+ ),
271269 }
272- })
273- introspection = bus . introspect_sync ( 'com.example' ,
274- ' /com/example/sample_object0' )
270+ }
271+ )
272+ introspection = bus . introspect_sync ( "com.example" , " /com/example/sample_object0" )
275273 assert bus .introspect_count == 1
276- parent = bus .get_proxy_object ('com.example' , '/com/example/sample_object0' ,
277- introspection )
274+ parent = bus .get_proxy_object (
275+ "com.example" , "/com/example/sample_object0" , introspection
276+ )
278277
279- assert parent .path == ' /com/example/sample_object0'
278+ assert parent .path == " /com/example/sample_object0"
280279 assert parent .child_paths == [
281- ' /com/example/sample_object0/child_of_sample_object' ,
282- ' /com/example/sample_object0/another_child_of_sample_object' ,
283- ]
280+ " /com/example/sample_object0/child_of_sample_object" ,
281+ " /com/example/sample_object0/another_child_of_sample_object" ,
282+ ]
284283 children = parent .get_children ()
285284 assert [child .path for child in children ] == parent .child_paths
286285
287286 # merely listing the children should not have required another introspection
288287 assert bus .introspect_count == 1
289288
290- child = next (child for child in children if child .path == \
291- '/com/example/sample_object0/child_of_sample_object' )
292- interface = child .get_interface ('com.example.ChildInterface' )
289+ child = next (
290+ child
291+ for child in children
292+ if child .path == "/com/example/sample_object0/child_of_sample_object"
293+ )
294+ interface = child .get_interface ("com.example.ChildInterface" )
293295
294296 # obtaining the child interface should have required a second introspection
295297 assert bus .introspect_count == 2
296298
297299 assert [prop .name for prop in interface .introspection .properties ] == [
298- ' ChildProperty'
299- ]
300+ " ChildProperty"
301+ ]
0 commit comments