Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#################
# 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)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value or
# value
self.checkXML(xmlvalue)
val = getXmlTextTrimmed(xmlvalue.firstChild)
if val is None:
self.value = "false" # Catch XML by setting the value to a default
else:
if "false" in unicode(xmlvalue.firstChild.data).lower():
self.value = "false"
else:
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value or
# value
if not isinstance(xmlvalue, dom.Element):
self.value = xmlvalue
return
self.checkXML(xmlvalue)
val = getXmlTextTrimmed(xmlvalue.firstChild)
self.value = val if val is not None else ""
class XmlElement(String):
def __init__(self, xmlelement=None):
String.__init__(self, xmlelement)
class ByteString(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value
if not isinstance(xmlvalue, dom.Element):
self.value = xmlvalue
return
self.checkXML(xmlvalue)
if xmlvalue.firstChild is None:
self.value = [] # Catch XML by setting the value to a default
else:
self.value = b64decode(xmlvalue.firstChild.data)
class ExtensionObject(Value):
def __init__(self, xmlelement=None):
class StatusCode(UInt32):
def __init__(self, xmlelement=None):
UInt32.__init__(self, xmlelement)
class DiagnosticInfo(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
self.checkXML(xmlvalue)
logger.warn("Not implemented")
class Guid(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
self.checkXML(xmlvalue)
val = getXmlTextTrimmed(xmlvalue.firstChild)
if val is None:
self.value = [0, 0, 0, 0] # Catch XML by setting the value to a default
else:
self.value = val
self.value = self.value.replace("{", "")
self.value = self.value.replace("}", "")
if "." in timestr:
timestr = timestr[:timestr.index(".")]
# If the last character is not numeric, remove it
while len(timestr) > 0 and not timestr[-1] in "0123456789":
timestr = timestr[:-1]
try:
self.value = datetime.strptime(timestr, "%Y-%m-%dT%H:%M:%S")
except Exception:
try:
self.value = datetime.strptime(timestr, "%Y-%m-%d")
except Exception:
logger.error("Timestring format is illegible. Expected 2001-01-30T21:22:23 or 2001-01-30, but got " + \
timestr + " instead. Time will be defaultet to now()")
self.value = datetime(2001, 1, 1)
class QualifiedName(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
self.ns = 0
self.name = None
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect or
# Int16
# SomeString
# or
if not isinstance(xmlvalue, dom.Element):
colonindex = xmlvalue.find(":")
if colonindex == -1:
self.name = xmlvalue
return str(self)
def __hash__(self):
return hash(str(self))
class ExpandedNodeId(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
self.checkXML(xmlvalue)
logger.debug("Not implemented", LOG_LEVEL_ERR)
class DateTime(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect or
# 2013-08-13T21:00:05.0000L
# or
self.checkXML(xmlvalue)
timestr = getXmlTextTrimmed(xmlvalue.firstChild)
if timestr is None:
# Catch XML by setting the value to a default
self.value = datetime(2001, 1, 1)
else:
def isNone(self):
return self.i is None and self.b is None and self.s is None and self.g is None
def __eq__(self, nodeId2):
return (str(self) == str(nodeId2))
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return str(self)
def __hash__(self):
return hash(str(self))
class ExpandedNodeId(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
self.checkXML(xmlvalue)
logger.debug("Not implemented", LOG_LEVEL_ERR)
class DateTime(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
Float.parseXML(self, xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value or
# value
self.checkXML(xmlvalue)
val = getXmlTextTrimmed(xmlvalue.firstChild)
self.value = val if val is not None else 0.0
class Double(Float):
def __init__(self, xmlelement=None):
Float.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
class String(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def pack(self):
bin = structpack("I", len(unicode(self.value)))
bin = bin + str(self.value)
return bin
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value or
# value
if not isinstance(xmlvalue, dom.Element):
self.value = xmlvalue
return
else:
self.value = b64decode(xmlvalue.firstChild.data)
class ExtensionObject(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlelement, namespaceMapping=None):
pass
def __str__(self):
return "'ExtensionObject'"
class LocalizedText(Value):
def __init__(self, xmlvalue=None):
Value.__init__(self)
self.locale = None
self.text = None
if xmlvalue:
self.parseXML(xmlvalue)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect or
# xx_XX
#
# or
if not isinstance(xmlvalue, dom.Element):
self.text = xmlvalue
return
self.checkXML(xmlvalue)
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value or
# value
self.checkXML(xmlvalue)
val = getXmlTextTrimmed(xmlvalue.firstChild)
if val is None:
self.value = "false" # Catch XML by setting the value to a default
else:
if "false" in unicode(xmlvalue.firstChild.data).lower():
self.value = "false"
else:
self.value = "true"
class Number(Value):
def __init__(self, xmlelement=None):
Value.__init__(self)
if xmlelement:
self.parseXML(xmlelement)
def parseXML(self, xmlvalue, namespaceMapping=None):
# Expect value or any other valid number type, or
# value
self.checkXML(xmlvalue)
val = getXmlTextTrimmed(xmlvalue.firstChild)
self.value = val if val is not None else 0
class Integer(Number):
def __init__(self, xmlelement=None):
Number.__init__(self)
if xmlelement: