Commit 52074ecd authored by olivier R-D's avatar olivier R-D

make Node.get_path return nodes. get_path_as_string returns qname strings

parent 73a2fee8
......@@ -353,7 +353,7 @@ class Node(object):
return None
return references[0].NodeId
def get_path(self, max_length=20):
def get_path_as_string(self, max_length=20):
"""
Attempt to find path of node from root node and return it as a list of strings.
There might several possible paths to a node, this function will return one
......@@ -362,14 +362,42 @@ class Node(object):
Since address space may have circular references, a max length is specified
"""
path = [self.get_browse_name().to_string()]
path = self._get_path(max_length)
path = [ref.BrowseName.to_string() for ref in path]
path.append(self.get_browse_name().to_string())
return path
def get_path(self, max_length=20):
"""
Attempt to find path of node from root node and return it as a list of Nodes.
There might several possible paths to a node, this function will return one
Some nodes may be missing references, so this method may
return an empty list
Since address space may have circular references, a max length is specified
"""
path = self._get_path(max_length)
path = [Node(self.server, ref.NodeId) for ref in path]
path.append(self)
return path
def _get_path(self, max_length=20):
"""
Attempt to find path of node from root node and return it as a list of Nodes.
There might several possible paths to a node, this function will return one
Some nodes may be missing references, so this method may
return an empty list
Since address space may have circular references, a max length is specified
"""
path = []
node = self
while True:
refs = node.get_references(refs=ua.ObjectIds.HierarchicalReferences, direction=ua.BrowseDirection.Inverse)
if len(refs) > 0:
path.insert(0, refs[0].BrowseName.to_string())
path.insert(0, refs[0])
node = Node(self.server, refs[0].NodeId)
if len(path) >= max_length:
if len(path) >= (max_length -1):
return path
else:
return path
......
......@@ -14,3 +14,5 @@ class Shortcuts(object):
self.base_object_type = Node(server, ObjectIds.BaseObjectType)
self.base_data_type = Node(server, ObjectIds.BaseDataType)
self.base_event_type = Node(server, ObjectIds.BaseEventType)
self.base_variable_type = Node(server, ObjectIds.BaseVariableType)
self.folder_type = Node(server, ObjectIds.FolderType)
......@@ -6,6 +6,7 @@ from datetime import timedelta
import math
from opcua import ua
from opcua import Node
from opcua import uamethod
from opcua import instantiate
from opcua import copy_node
......@@ -539,15 +540,27 @@ class CommonTests(object):
self.assertEqual(p.get_parent(), o)
self.assertEqual(p.get_type_definition().Identifier, ua.ObjectIds.PropertyType)
def test_path(self):
def test_path_string(self):
o = self.opc.nodes.objects.add_folder(1, "titif").add_object(3, "opath")
path = o.get_path()
path = o.get_path_as_string()
self.assertEqual(["0:Root", "0:Objects", "1:titif", "3:opath"], path)
path = o.get_path(2)
path = o.get_path_as_string(2)
self.assertEqual(["1:titif", "3:opath"], path)
self.opc.get_node("i=27")
path = self.opc.get_node("i=13387").get_path()
self.assertEqual(['0:BaseObjectType', '0:FolderType', '0:FileDirectoryType', '0:CreateDirectory'], path) # FIXME this is wrong in our server! BaseObjectType is missing an inverse reference to its parent! seems xml definition is wrong
path = self.opc.get_node("i=13387").get_path_as_string()
# FIXME this is wrong in our server! BaseObjectType is missing an inverse reference to its parent! seems xml definition is wrong
self.assertEqual(['0:BaseObjectType', '0:FolderType', '0:FileDirectoryType', '0:CreateDirectory'], path)
def test_path(self):
of = self.opc.nodes.objects.add_folder(1, "titif")
op = of.add_object(3, "opath")
path = op.get_path()
self.assertEqual([self.opc.nodes.root, self.opc.nodes.objects, of, op], path)
path = op.get_path(2)
self.assertEqual([of, op], path)
target = self.opc.get_node("i=13387")
path = target.get_path()
# FIXME this is wrong in our server! BaseObjectType is missing an inverse reference to its parent! seems xml definition is wrong
self.assertEqual([self.opc.nodes.base_object_type, self.opc.nodes.folder_type, self.opc.get_node(ua.ObjectIds.FileDirectoryType), target], path)
def test_get_endpoints(self):
endpoints = self.opc.get_endpoints()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment