Skip to content

Commit

Permalink
Merge b291998 into 7cf9ab6
Browse files Browse the repository at this point in the history
  • Loading branch information
Pro committed May 28, 2019
2 parents 7cf9ab6 + b291998 commit de4c95f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
8 changes: 6 additions & 2 deletions tools/nodeset_compiler/backend_open62541_nodes.py
Expand Up @@ -78,7 +78,7 @@ def generateObjectNodeCode(node):
def setNodeDatatypeRecursive(node, nodeset):

if not isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
raise RuntimeError("DataType can only be set for VariableNode and VariableTypeNode")
raise RuntimeError("Node {}: DataType can only be set for VariableNode and VariableTypeNode".format(str(node.id)))

if node.dataType is not None:
return
Expand Down Expand Up @@ -110,7 +110,7 @@ def setNodeDatatypeRecursive(node, nodeset):
def setNodeValueRankRecursive(node, nodeset):

if not isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
raise RuntimeError("ValueRank can only be set for VariableNode and VariableTypeNode")
raise RuntimeError("Node {}: ValueRank can only be set for VariableNode and VariableTypeNode".format(str(node.id)))

if node.valueRank is not None:
return
Expand All @@ -127,6 +127,10 @@ def setNodeValueRankRecursive(node, nodeset):
if typeDefNode is None:
# Use the parent type.
raise RuntimeError("Cannot get node for HasTypeDefinition of VariableNode " + node.browseName.name + " " + str(node.id))
if not isinstance(typeDefNode, VariableTypeNode):
raise RuntimeError("Node {} ({}) has an invalid type definition. {} is not a VariableType node.".format(
str(node.id), node.browseName.name, str(typeDefNode.id)))


setNodeValueRankRecursive(typeDefNode, nodeset)

Expand Down
46 changes: 29 additions & 17 deletions tools/nodeset_compiler/datatypes.py
Expand Up @@ -18,6 +18,7 @@

import sys
import logging
import re
from datetime import datetime
import xml.dom.minidom as dom
from base64 import *
Expand Down Expand Up @@ -308,6 +309,17 @@ def __repr__(self):
# Builtin Types #
#################


def getXmlTextTrimmed(xmlNode):
if xmlNode is None or xmlNode.data is None:
return None
content = xmlNode.data
# Check for empty string (including newlines)
if not re.sub(r"[\s\n\r]", "", content).strip():
return None
return unicode(content.strip())


class Boolean(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
Expand All @@ -318,7 +330,8 @@ def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect <Boolean>value</Boolean> or
# <Aliasname>value</Aliasname>
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:
val = getXmlTextTrimmed(xmlvalue.firstChild)
if val is None:
self.value = "false" # Catch XML <Boolean /> by setting the value to a default
else:
if "false" in unicode(xmlvalue.firstChild.data).lower():
Expand All @@ -336,10 +349,8 @@ def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect <Int16>value</Int16> or any other valid number type, or
# <Aliasname>value</Aliasname>
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:
self.value = 0 # Catch XML <Int16 /> by setting the value to a default
else:
self.value = int(unicode(xmlvalue.firstChild.data))
val = getXmlTextTrimmed(xmlvalue.firstChild)
self.value = val if val is not None else 0

class Integer(Number):
def __init__(self, xmlelement=None):
Expand Down Expand Up @@ -411,10 +422,8 @@ def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect <Float>value</Float> or
# <Aliasname>value</Aliasname>
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:
self.value = 0.0 # Catch XML <Float /> by setting the value to a default
else:
self.value = float(unicode(xmlvalue.firstChild.data))
val = getXmlTextTrimmed(xmlvalue.firstChild)
self.value = val if val is not None else 0.0

class Double(Float):
def __init__(self, xmlelement=None):
Expand All @@ -440,10 +449,9 @@ def parseXML(self, xmlvalue, namespaceMapping=None):
self.value = xmlvalue
return
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:
self.value = "" # Catch XML <String /> by setting the value to a default
else:
self.value = unicode(xmlvalue.firstChild.data)
val = getXmlTextTrimmed(xmlvalue.firstChild)
self.value = val if val is not None else ""


class XmlElement(String):
def __init__(self, xmlelement=None):
Expand Down Expand Up @@ -628,11 +636,12 @@ def parseXML(self, xmlvalue, namespaceMapping=None):
# 2013-08-13T21:00:05.0000L
# </DateTime> or </AliasName>
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:
timestr = getXmlTextTrimmed(xmlvalue.firstChild)

if timestr is None:
# Catch XML <DateTime /> by setting the value to a default
self.value = datetime(2001, 1, 1)
else:
timestr = unicode(xmlvalue.firstChild.data)
# .NET tends to create this garbage %Y-%m-%dT%H:%M:%S.0000z
# strip everything after the "." away for a posix time_struct
if "." in timestr:
Expand Down Expand Up @@ -712,10 +721,13 @@ def __init__(self, xmlelement=None):

def parseXML(self, xmlvalue, namespaceMapping=None):
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:

val = getXmlTextTrimmed(xmlvalue.firstChild)

if val is None:
self.value = [0, 0, 0, 0] # Catch XML <Guid /> by setting the value to a default
else:
self.value = unicode(xmlvalue.firstChild.data)
self.value = val
self.value = self.value.replace("{", "")
self.value = self.value.replace("}", "")
self.value = self.value.split("-")
Expand Down
2 changes: 1 addition & 1 deletion tools/nodeset_compiler/nodeset.py
Expand Up @@ -320,7 +320,7 @@ def getBaseDataType(self, node):
def getNodeTypeDefinition(self, node):
for ref in node.references:
# 40 = HasTypeDefinition
if ref.referenceType.i == 40:
if ref.referenceType.i == 40 and ref.isForward:
return self.nodes[ref.target]
return None

Expand Down

0 comments on commit de4c95f

Please sign in to comment.