From b212b6c3a90b53a96af5bd0b57efb7b609d0409c Mon Sep 17 00:00:00 2001 From: Yuta Okamoto Date: Thu, 24 Aug 2023 22:32:23 +0900 Subject: [PATCH] Add browse_nodes to SyncClient (#1412) * add browse_nodes to SyncClient * fix ruff E721 warning --- asyncua/client/client.py | 4 ++-- asyncua/sync.py | 11 ++++++----- tests/test_sync.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/asyncua/client/client.py b/asyncua/client/client.py index 14a28e98f..5b57fa285 100644 --- a/asyncua/client/client.py +++ b/asyncua/client/client.py @@ -1,7 +1,7 @@ import asyncio import logging import socket -from typing import List, Union, Coroutine, Optional, Type +from typing import List, Tuple, Union, Coroutine, Optional, Type from urllib.parse import urlparse, unquote from pathlib import Path @@ -894,7 +894,7 @@ async def write_values(self, nodes, values, raise_on_partial_error=True): get_values = read_values # legacy compatibility set_values = write_values # legacy compatibility - async def browse_nodes(self, nodes): + async def browse_nodes(self, nodes: List[Node]) -> List[Tuple[Node, ua.BrowseResult]]: """ Browses multiple nodes in one ua call returns a List of Tuples(Node, BrowseResult) diff --git a/asyncua/sync.py b/asyncua/sync.py index 80c184811..e38eee136 100644 --- a/asyncua/sync.py +++ b/asyncua/sync.py @@ -77,11 +77,8 @@ def _to_async(args, kwargs): def _to_sync(tloop, result): if isinstance(result, node.Node): return SyncNode(tloop, result) - if isinstance(result, (list, tuple)) and len(result) > 0: - if isinstance(result[0], node.Node): - return [SyncNode(tloop, i) for i in result] - elif isinstance(result[0], (list, tuple)): - return [_to_sync(tloop, item) for item in result] + if isinstance(result, (list, tuple)): + return [_to_sync(tloop, item) for item in result] if isinstance(result, server.event_generator.EventGenerator): return EventGenerator(tloop, result) if isinstance(result, subscription.Subscription): @@ -315,6 +312,10 @@ def read_values(self, nodes): def write_values(self, nodes, values, raise_on_partial_error=True): pass + @syncmethod + def browse_nodes(self, nodes: List["SyncNode"]) -> List[Tuple["SyncNode", ua.BrowseResult]]: # type: ignore[empty-body] + pass + @syncmethod def translate_browsepaths( # type: ignore[empty-body] self, diff --git a/tests/test_sync.py b/tests/test_sync.py index 61f51fead..b09b7ca72 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -108,6 +108,22 @@ def test_sync_client_get_node(client, idx): assert vars[0].read_value() == 6.7 +def test_sync_client_browse_nodes(client: Client, idx): + nodes = [ + client.get_node("ns=0;i=2267"), + client.get_node("ns=0;i=2259"), + ] + results = client.browse_nodes(nodes) + assert len(results) == 2 + assert isinstance(results, list) + assert results[0][0] == nodes[0] + assert results[1][0] == nodes[1] + assert isinstance(results[0][0], SyncNode) + assert isinstance(results[1][0], SyncNode) + assert isinstance(results[0][1], ua.BrowseResult) + assert isinstance(results[1][1], ua.BrowseResult) + + def test_sync_server_get_node(server, idx): node = server.get_node(85) assert node == server.nodes.objects