diff --git a/dbus_next/message_bus.py b/dbus_next/message_bus.py index aa890bb..3b51608 100644 --- a/dbus_next/message_bus.py +++ b/dbus_next/message_bus.py @@ -399,25 +399,27 @@ def _interface_signal_notify(self, interface, interface_name, member, signature, def _introspect_export_path(self, path): assert_object_path_valid(path) - node = None if path in self._path_exports: node = intr.Node.default(path) - [ + for interface in self._path_exports[path]: node.interfaces.append(interface.introspect()) - for interface in self._path_exports[path] - ] else: node = intr.Node(path) - path_split = [path for path in path.split('/') if path] + children = set() - for export_path in self._path_exports.keys(): - export_path_split = [path for path in export_path.split('/') if path] - if len(export_path_split) <= len(path_split): + for export_path in self._path_exports: + try: + child_path = export_path.split(path, maxsplit=1)[1] + except IndexError: continue - if all(path == export_path_split[i] for i, path in enumerate(path_split)): - child = intr.Node(export_path_split[len(path_split)]) - node.nodes.append(child) + + child_path = child_path.lstrip('/') + child_name = child_path.split('/', maxsplit=1)[0] + + children.add(child_name) + + node.nodes = [intr.Node(name) for name in children if name] return node diff --git a/test/service/test_export.py b/test/service/test_export.py index c4a4a02..b7b37d5 100644 --- a/test/service/test_export.py +++ b/test/service/test_export.py @@ -94,3 +94,20 @@ async def test_export_alias(): member='some_method')) assert result.message_type is MessageType.METHOD_RETURN, result.body[0] assert interface._method_called + + +@pytest.mark.asyncio +async def test_export_introspection(): + interface = ExampleInterface('test.interface') + interface2 = ExampleInterface('test.interface2') + + export_path = '/test/path' + export_path2 = '/test/path/child' + + bus = await MessageBus().connect() + bus.export(export_path, interface) + bus.export(export_path2, interface2) + + root = bus._introspect_export_path('/') + assert len(root.nodes) == 1 +