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():
nid0 = ua.NodeId(45)
assert nid0 == ua.NodeId.from_string("i=45")
assert nid0 == ua.NodeId.from_string("ns=0;i=45")
nid = ua.NodeId(45, 10)
assert nid == ua.NodeId.from_string("i=45; ns=10")
assert nid != ua.NodeId.from_string("i=45; ns=11")
assert nid != ua.NodeId.from_string("i=5; ns=10")
# not sure the next one is correct...
assert nid == ua.NodeId.from_string("i=45; ns=10; srv=serverid")
nid1 = ua.NodeId("myid.mynodeid", 7)
assert nid1 == ua.NodeId.from_string("ns=7; s=myid.mynodeid")
# with pytest.raises(ua.UaError):
def test_unknown_extension_object():
obj = ua.ExtensionObject()
obj.Body = b'example of data in custom format'
obj.TypeId = ua.NodeId.from_string('ns=3;i=42')
data = ua.utils.Buffer(extensionobject_to_binary(obj))
obj2 = extensionobject_from_binary(data)
assert type(obj2) == ua.ExtensionObject
assert obj2.TypeId == obj.TypeId
assert obj2.Body == b'example of data in custom format'
def test_nodeid_string():
nid0 = ua.NodeId(45)
assert nid0 == ua.NodeId.from_string("i=45")
assert nid0 == ua.NodeId.from_string("ns=0;i=45")
nid = ua.NodeId(45, 10)
assert nid == ua.NodeId.from_string("i=45; ns=10")
assert nid != ua.NodeId.from_string("i=45; ns=11")
assert nid != ua.NodeId.from_string("i=5; ns=10")
# not sure the next one is correct...
assert nid == ua.NodeId.from_string("i=45; ns=10; srv=serverid")
nid1 = ua.NodeId("myid.mynodeid", 7)
assert nid1 == ua.NodeId.from_string("ns=7; s=myid.mynodeid")
# with pytest.raises(ua.UaError):
v.SbyteValue = [1]
v.ByteValue = [2]
v.Int16Value = [3]
v.UInt16Value = [4]
v.Int32Value = [5]
v.UInt32Value = [6]
v.Int64Value = [7]
v.UInt64Value = [8]
v.FloatValue = [9.0]
v.DoubleValue = [10.0]
v.StringValue = ["elleven"]
v.DateTimeValue = [datetime.utcnow()]
# self.GuidValue = uuid.uudib"14"
v.ByteStringValue = [b"fifteen", b"sixteen"]
v.XmlElementValue = [ua.XmlElement("titi")]
v.NodeIdValue = [ua.NodeId.from_string("ns=4;i=9999"), ua.NodeId.from_string("i=6")]
data = struct_to_binary(v)
v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data))
assert v.NodeIdValue == v2.NodeIdValue
# print(v2.NodeIdValue)
# tow possible values:
# either we get value directly
# or a dict if it s an object or a list
if isinstance(val, str):
pval = ua_type_to_python(val, self._get_val_type(obj, attname))
setattr(obj, attname, pval)
else:
# so we have either an object or a list...
obj2 = getattr(obj, attname)
if isinstance(obj2, ua.NodeId): # NodeId representation does not follow common rules!!
for attname2, v2 in val:
if attname2 == "Identifier":
if hasattr(ua.ObjectIds, v2):
obj2 = ua.NodeId(getattr(ua.ObjectIds, v2))
else:
obj2 = ua.NodeId.from_string(v2)
setattr(obj, attname, self._migrate_ns(obj2))
break
elif not hasattr(obj2, "ua_types"):
# we probably have a list
my_list = []
for vtype, v2 in val:
my_list.append(ua_type_to_python(v2, vtype))
setattr(obj, attname, my_list)
else:
for attname2, v2 in val:
self._set_attr(obj2, attname2, v2)
setattr(obj, attname, obj2)
def _to_nodeid(nodeid):
if isinstance(nodeid, int):
return ua.TwoByteNodeId(nodeid)
elif isinstance(nodeid, Node):
return nodeid.nodeid
elif isinstance(nodeid, ua.NodeId):
return nodeid
elif type(nodeid) in (str, bytes):
return ua.NodeId.from_string(nodeid)
else:
raise ua.UaError(f"Could not resolve '{nodeid}' to a type id")
def _to_nodeid(nodeid):
if isinstance(nodeid, int):
return ua.TwoByteNodeId(nodeid)
elif isinstance(nodeid, Node):
return nodeid.nodeid
elif isinstance(nodeid, ua.NodeId):
return nodeid
elif type(nodeid) in (str, bytes):
return ua.NodeId.from_string(nodeid)
else:
raise ua.UaError(f"Could not resolve '{nodeid}' to a type id")
val = 0
else:
val = int(string)
elif vtype in (ua.VariantType.Float, ua.VariantType.Double):
if not string:
val = 0.0
else:
val = float(string)
elif vtype == ua.VariantType.XmlElement:
val = ua.XmlElement(string)
elif vtype == ua.VariantType.String:
val = string
elif vtype == ua.VariantType.ByteString:
val = string.encode()
elif vtype in (ua.VariantType.NodeId, ua.VariantType.ExpandedNodeId):
val = ua.NodeId.from_string(string)
elif vtype == ua.VariantType.QualifiedName:
val = ua.QualifiedName.from_string(string)
elif vtype == ua.VariantType.DateTime:
val = parser.parse(string)
elif vtype == ua.VariantType.LocalizedText:
val = ua.LocalizedText.from_string(string)
elif vtype == ua.VariantType.StatusCode:
val = ua.StatusCode(string)
elif vtype == ua.VariantType.Guid:
val = uuid.UUID(string)
else:
# FIXME: Some types are probably missing!
raise NotImplementedError
return val