Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nodeid_string(self):
nid0 = ua.NodeId(45)
self.assertEqual(nid0, ua.NodeId.from_string("i=45"))
self.assertEqual(nid0, ua.NodeId.from_string("ns=0;i=45"))
nid = ua.NodeId(45, 10)
self.assertEqual(nid, ua.NodeId.from_string("i=45; ns=10"))
self.assertNotEqual(nid, ua.NodeId.from_string("i=45; ns=11"))
self.assertNotEqual(nid, ua.NodeId.from_string("i=5; ns=10"))
# not sure the next one is correct...
self.assertEqual(nid, ua.NodeId.from_string("i=45; ns=10; srv=serverid"))
nid1 = ua.NodeId("myid.mynodeid", 7)
self.assertEqual(nid1, ua.NodeId.from_string("ns=7; s=myid.mynodeid"))
#with self.assertRaises(ua.UaError):
def test_nodeid_ordering(self):
a = ua.NodeId(2000, 1)
b = ua.NodeId(3000, 1)
c = ua.NodeId(20, 0)
d = ua.NodeId("tititu", 1)
e = ua.NodeId("aaaaa", 1)
f = ua.NodeId("aaaaa", 2)
g = ua.NodeId(uuid.uuid4(), 1)
h = ua.TwoByteNodeId(2001)
i = ua.NodeId(b"lkjkl", 1, ua.NodeIdType.ByteString)
j = ua.NodeId(b"aaa", 5, ua.NodeIdType.ByteString)
mylist = [a, b, c, d, e, f, g, h, i, j]
mylist.sort()
expected = [h, c, a, b, e, d, f, g, i, j]
self.assertEqual(mylist, expected)
def test_reference_generator_2(self):
id1 = ua.NodeId(1, namespaceidx=2, nodeidtype=ua.NodeIdType.Numeric)
id2 = ua.NodeId(2, namespaceidx=2, nodeidtype=ua.NodeIdType.Numeric)
ref = ua.NodeId(ua.ObjectIds.HasEncoding, 0)
result = reference_generator(id1, id2, ref, False)
self.assertFalse(result.IsForward)
self.assertEqual(result.ReferenceTypeId, ref)
self.assertEqual(result.SourceNodeId, id1)
self.assertEqual(result.TargetNodeClass, ua.NodeClass.DataType)
self.assertEqual(result.TargetNodeId, id2)
def test_unicode_string_nodeid(self):
nid = ua.NodeId('hëllò', 1)
self.assertEqual(nid.NamespaceIndex, 1)
self.assertEqual(nid.Identifier, 'hëllò')
self.assertEqual(nid.NodeIdType, ua.NodeIdType.String)
d = nid.to_binary()
new_nid = nid.from_binary(io.BytesIO(d))
self.assertEqual(new_nid, nid)
self.assertEqual(new_nid.Identifier, 'hëllò')
self.assertEqual(new_nid.NodeIdType, ua.NodeIdType.String)
def get_bp_to_component_with_intermediate_organized_path(organized_path_qname_list,
target_component_qname):
bp = get_bp_of_organized_path(organized_path_qname_list)
# Add final component path
target_component_ns, target_component_name = target_component_qname.split(":")
rpe = ua.uaprotocol_auto.RelativePathElement()
rpe.ReferenceTypeId=ua.NodeId(47)
rpe.IsInverse=False
rpe.TargetName.NamespaceIndex=int(target_component_ns)
rpe.TargetName.Name=target_component_name
bp.RelativePath.Elements.append(rpe)
return bp
import base64
from opcua import ua
from opcua.ua import object_ids as o_ids
from opcua.common.ua_utils import get_base_data_type
from opcua.ua.uatypes import extension_object_ids
from opcua.ua.uaerrors import UaError
class XmlExporter(object):
''' If it is required that for _extobj_to_etree members to the value should be written in a certain
order it can be added to the dictionary below.
'''
extobj_ordered_elements = {
ua.NodeId(ua.ObjectIds.Argument) : ['Name',
'DataType',
'ValueRank',
'ArrayDimensions',
'Description']
}
def __init__(self, server):
self.logger = logging.getLogger(__name__)
self.server = server
self.aliases = {}
self._addr_idx_to_xml_idx = {}
node_set_attributes = OrderedDict()
node_set_attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'
node_set_attributes['xmlns:uax'] = 'http://opcfoundation.org/UA/2008/02/Types.xsd'
node_set_attributes['xmlns:xsd'] = 'http://www.w3.org/2001/XMLSchema'
def _to_nodeid(self, nodeid):
if isinstance(nodeid, ua.NodeId):
return nodeid
elif not nodeid:
return ua.NodeId(ua.ObjectIds.String)
elif "=" in nodeid:
return ua.NodeId.from_string(nodeid)
elif hasattr(ua.ObjectIds, nodeid):
return ua.NodeId(getattr(ua.ObjectIds, nodeid))
else:
if nodeid in self.aliases:
return self.aliases[nodeid]
else:
return ua.NodeId(getattr(ua.ObjectIds, nodeid))
_reference_generator(data_type_node_id, ua.NodeId(ua.ObjectIds.Structure, 0),
ua.NodeId(ua.ObjectIds.HasSubtype, 0), False),
# add reverse reference to created data type
_reference_generator(linked_obj_node_id, data_type_node_id,
ua.NodeId(ua.ObjectIds.HasEncoding, 0), False),
# add HasDescription link to dictionary description
_reference_generator(linked_obj_node_id, description_node_id,
ua.NodeId(ua.ObjectIds.HasDescription, 0)),
# add reverse HasDescription link
_reference_generator(description_node_id, linked_obj_node_id,
ua.NodeId(ua.ObjectIds.HasDescription, 0), False),
# add link to the type definition node
_reference_generator(linked_obj_node_id, ua.NodeId(ua.ObjectIds.DataTypeEncodingType, 0),
ua.NodeId(ua.ObjectIds.HasTypeDefinition, 0)),
# add has type definition link
_reference_generator(description_node_id, ua.NodeId(ua.ObjectIds.DataTypeDescriptionType, 0),
ua.NodeId(ua.ObjectIds.HasTypeDefinition, 0)),
# add forward link of dict to description item
_reference_generator(self.dict_id, description_node_id,
ua.NodeId(ua.ObjectIds.HasComponent, 0)),
# add reverse link to dictionary
_reference_generator(description_node_id, self.dict_id,
ua.NodeId(ua.ObjectIds.HasComponent, 0), False)]
self._session_server.add_references(refs)
def nodeid_from_binary(data):
nid = ua.NodeId()
encoding = ord(data.read(1))
nid.NodeIdType = ua.NodeIdType(encoding & 0b00111111)
if nid.NodeIdType == ua.NodeIdType.TwoByte:
nid.Identifier = ord(data.read(1))
elif nid.NodeIdType == ua.NodeIdType.FourByte:
nid.NamespaceIndex, nid.Identifier = struct.unpack("
def _val_to_etree(self, el, dtype, val):
if dtype == ua.NodeId(ua.ObjectIds.NodeId):
id_el = Et.SubElement(el, "uax:Identifier")
id_el.text = val.to_string()
elif dtype == ua.NodeId(ua.ObjectIds.Guid):
id_el = Et.SubElement(el, "uax:String")
id_el.text = str(val)
elif dtype == ua.NodeId(ua.ObjectIds.Boolean):
el.text = 'true' if val else 'false'
elif dtype == ua.NodeId(ua.ObjectIds.ByteString):
if val is None:
val = b""
data = base64.b64encode(val)
el.text = data.decode("utf-8")
elif not hasattr(val, "ua_types"):
if isinstance(val, bytes):
# FIXME: should we also encode this (localized text I guess) using base64??
el.text = val.decode("utf-8")