Completed
Push — master ( f88dce...61e652 )
by Olivier
07:43 queued 03:32
created

copy_node()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
c 2
b 1
f 0
dl 0
loc 11
rs 9.4285
1
from opcua import ua
2
from opcua.common.node import Node
3
4
5
def copy_node(parent, node, nodeid=None, recursive=True):
6
    """
7
    Copy a node or node tree as child of parent node
8
    """
9
    print("Copying", node, "into ", parent)
10
    rdesc = _rdesc_from_node(parent, node)
11
12
    if nodeid is None:
13
        nodeid = ua.NodeId(namespaceidx=node.nodeid.NamespaceIndex)
14
    added_nodeids = _copy_node(parent.server, parent.nodeid, rdesc, nodeid, recursive)
15
    return [Node(parent.server, nid) for nid in added_nodeids]
16
17
18
def _copy_node(server, parent_nodeid, rdesc, nodeid, recursive):
19
    addnode = ua.AddNodesItem()
20
    addnode.RequestedNewNodeId = nodeid
21
    addnode.BrowseName = rdesc.BrowseName
22
    addnode.ParentNodeId = parent_nodeid
23
    addnode.ReferenceTypeId = rdesc.ReferenceTypeId
24
    addnode.TypeDefinition = rdesc.TypeDefinition
25
    addnode.NodeClass = rdesc.NodeClass
26
27
    node_to_copy = Node(server, rdesc.NodeId)
28
    
29
    attrObj = getattr(ua, rdesc.NodeClass.name + "Attributes")
30
    _read_and_copy_attrs(node_to_copy, attrObj(), addnode)
31
    
32
    res = server.add_nodes([addnode])[0]
33
34
    added_nodes = [res.AddedNodeId]
35
        
36
    if recursive:
37
        descs = node_to_copy.get_children_descriptions()
38
        for desc in descs:
39
            nodes = _copy_node(server, res.AddedNodeId, desc, nodeid=ua.NodeId(namespaceidx=desc.NodeId.NamespaceIndex), recursive=True)
40
            added_nodes.extend(nodes)
41
42
    return added_nodes
43
44
45
def _rdesc_from_node(parent, node):
46
    results = node.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName])
47
    nclass, qname, dname = [res.Value.Value for res in results]
48
49
    rdesc = ua.ReferenceDescription()
50
    rdesc.NodeId = node.nodeid
51
    rdesc.BrowseName = qname
52
    rdesc.DisplayName = dname
53
    rdesc.NodeClass = nclass
54
    if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType):
55
        rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes)
56
    else:
57
        rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent)
58
    rdesc.TypeDefinition = node.nodeid
59
    return rdesc
60
61
62
63
def _read_and_copy_attrs(node_type, struct, addnode):
64
    names = [name for name in struct.__dict__.keys() if not name.startswith("_") and name not in ("BodyLength", "TypeId", "SpecifiedAttributes", "Encoding", "IsAbstract", "EventNotifier")]
65
    attrs = [getattr(ua.AttributeIds, name) for name in names]            
66
    for name in names:
67
        results = node_type.get_attributes(attrs)
68
    for idx, name in enumerate(names):
69
        if results[idx].StatusCode.is_good():
70
            if name == "Value":
71
                setattr(struct, name, results[idx].Value)
72
            else:
73
                setattr(struct, name, results[idx].Value.Value)
74
        else:
75
            print("Instantiate: while copying attributes from node type %s, attribute %s, statuscode is %s" % (node_type, name, results[idx].StatusCode))            
76
    addnode.NodeAttributes = struct
77