Skip to content

Commit f725923

Browse files
committed
better default display name when instantiating node, logging fix
1 parent 958b457 commit f725923

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

opcua/common/copy_node.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import logging
2+
13
from opcua import ua
24
from opcua.common.node import Node
35

46

7+
logger = logging.getLogger(__name__)
8+
9+
510
def copy_node(parent, node, nodeid=None, recursive=True):
611
"""
712
Copy a node or node tree as child of parent node
813
"""
9-
print("Copying", node, "into ", parent)
1014
rdesc = _rdesc_from_node(parent, node)
1115

1216
if nodeid is None:
@@ -73,5 +77,5 @@ def _read_and_copy_attrs(node_type, struct, addnode):
7377
else:
7478
setattr(struct, name, results[idx].Value.Value)
7579
else:
76-
print("Instantiate: while copying attributes from node type {0!s}, attribute {1!s}, statuscode is {2!s}".format(node_type, name, results[idx].StatusCode))
80+
logger.warning("Instantiate: while copying attributes from node type {0!s}, attribute {1!s}, statuscode is {2!s}".format(node_type, name, results[idx].StatusCode))
7781
addnode.NodeAttributes = struct

opcua/common/instantiate.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
Instantiate a new node and its child nodes from a node type.
33
"""
44

5+
import logging
6+
57

68
from opcua import Node
79
from opcua import ua
810
from opcua.common import ua_utils
911
from opcua.common.copy_node import _rdesc_from_node, _read_and_copy_attrs
1012

1113

12-
def instantiate(parent, node_type, nodeid=None, bname=None, idx=0):
14+
logger = logging.getLogger(__name__)
15+
16+
17+
def instantiate(parent, node_type, nodeid=None, bname=None, dname=None, idx=0):
1318
"""
1419
instantiate a node type under a parent node.
1520
nodeid and browse name of new node can be specified, or just namespace index
@@ -26,11 +31,11 @@ def instantiate(parent, node_type, nodeid=None, bname=None, idx=0):
2631
elif isinstance(bname, str):
2732
bname = ua.QualifiedName.from_string(bname)
2833

29-
nodeids = _instantiate_node(parent.server, parent.nodeid, rdesc, nodeid, bname)
34+
nodeids = _instantiate_node(parent.server, parent.nodeid, rdesc, nodeid, bname, dname=dname)
3035
return [Node(parent.server, nid) for nid in nodeids]
3136

3237

33-
def _instantiate_node(server, parentid, rdesc, nodeid, bname, recursive=True):
38+
def _instantiate_node(server, parentid, rdesc, nodeid, bname, dname=None, recursive=True):
3439
"""
3540
instantiate a node type under parent
3641
"""
@@ -59,8 +64,10 @@ def _instantiate_node(server, parentid, rdesc, nodeid, bname, recursive=True):
5964
addnode.NodeClass = ua.NodeClass.Method
6065
_read_and_copy_attrs(node_type, ua.MethodAttributes(), addnode)
6166
else:
62-
print("Instantiate: Node class not supported: ", rdesc.NodeClass)
67+
logger.warning("Instantiate: Node class not supported: %s", rdesc.NodeClass)
6368
return
69+
if dname is not None:
70+
addnode.NodeAttributes.DisplayName = dname
6471

6572
res = server.add_nodes([addnode])[0]
6673
added_nodes = [res.AddedNodeId]

opcua/common/manage_nodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def create_object(parent, nodeid, bname, objecttype=None):
5151
nodeid, qname = _parse_nodeid_qname(nodeid, bname)
5252
if objecttype is not None:
5353
objecttype = node.Node(parent.server, objecttype)
54-
nodes = instantiate(parent, objecttype, nodeid, bname)[0]
54+
dname = ua.LocalizedText(bname)
55+
nodes = instantiate(parent, objecttype, nodeid, bname=qname, dname=dname)[0]
5556
return nodes
5657
else:
5758
return node.Node(parent.server, _create_object(parent.server, parent.nodeid, nodeid, qname, ua.ObjectIds.BaseObjectType))

0 commit comments

Comments
 (0)