2 parse xml file from opcua-spec 8 import xml.etree.ElementTree
as ET
12 return val
in (
"True",
"true",
"on",
"On",
"1")
16 if uatype.startswith(
"Int")
or uatype.startswith(
"UInt"):
18 elif uatype.lower().startswith(
"bool"):
20 elif uatype
in (
"Double",
"Float"):
22 elif uatype ==
"String":
24 elif uatype
in (
"Bytes",
"Bytes",
"ByteString",
"ByteArray"):
25 if sys.version_info.major > 2:
26 return bytes(val,
'utf8')
30 raise Exception(
"uatype nopt handled", uatype,
" for val ", val)
68 return "NodeData(nodeid:{0})".format(self.
nodeid)
89 return "ExtObj({0}, {1})".format(self.
objname, self.
body)
96 self.
logger = logging.getLogger(__name__)
97 self.
_retag = re.compile(
r"(\{.*\})(.*)")
101 self.
root = self.tree.getroot()
104 'base':
"http://opcfoundation.org/UA/2011/03/UANodeSet.xsd",
105 'uax':
"http://opcfoundation.org/UA/2008/02/Types.xsd",
106 'xsd':
"http://www.w3.org/2001/XMLSchema",
107 'xsi':
"http://www.w3.org/2001/XMLSchema-instance" 112 Return the used namespace uris in this import file 115 for child
in self.
root:
116 tag = self._retag.match(child.tag).groups()[1]
117 if tag ==
'NamespaceUris':
118 namespaces_uris = [ns_element.text
for ns_element
in child]
120 return namespaces_uris
124 Return the used node aliases in this import file 127 for child
in self.
root:
128 tag = self._retag.match(child.tag).groups()[1]
131 aliases[el.attrib[
"Alias"]] = el.text
137 for child
in self.
root:
138 tag = self._retag.match(child.tag).groups()[1]
139 if tag
not in [
"Aliases",
"NamespaceUris",
"Extensions",
"Models"]:
146 Parse a XML node and create a NodeData object. 149 obj.nodetype = nodetype
150 for key, val
in child.attrib.items():
152 self.logger.info(
"Parsing node: %s %s", obj.nodeid, obj.browsename)
153 obj.displayname = obj.browsename
161 elif key ==
"BrowseName":
163 elif key ==
"SymbolicName":
165 elif key ==
"ParentNodeId":
167 elif key ==
"DataType":
169 elif key ==
"IsAbstract":
171 elif key ==
"Executable":
173 elif key ==
"EventNotifier":
174 obj.eventnotifier = int(val)
175 elif key ==
"ValueRank":
177 elif key ==
"ArrayDimensions":
178 obj.dimensions = [int(i)
for i
in val.split(
",")]
179 elif key ==
"MinimumSamplingInterval":
180 obj.minsample = int(val)
181 elif key ==
"AccessLevel":
182 obj.accesslevel = int(val)
183 elif key ==
"UserAccessLevel":
184 obj.useraccesslevel = int(val)
185 elif key ==
"Symmetric":
188 self.logger.info(
"Attribute not implemented: %s:%s", key, val)
191 tag = self._retag.match(el.tag).groups()[1]
193 if tag ==
"DisplayName":
194 obj.displayname = el.text
195 elif tag ==
"Description":
197 elif tag ==
"References":
201 elif tag ==
"InverseName":
202 obj.inversename = el.text
203 elif tag ==
"Definition":
205 obj.definition.append(field)
207 self.logger.info(
"Not implemented tag: %s", el)
210 child_el = val_el.find(
".//")
211 if child_el
is not None:
212 ntag = self._retag.match(child_el.tag).groups()[1]
217 if ntag
in (
"Int8",
"UInt8",
"Int16",
"UInt16",
"Int32",
"UInt32",
"Int64",
"UInt64"):
218 obj.value = int(child_el.text)
219 elif ntag
in (
"Float",
"Double"):
220 obj.value = float(child_el.text)
221 elif ntag ==
"Boolean":
223 elif ntag
in (
"ByteString",
"String"):
224 mytext = child_el.text
227 mytext = mytext.replace(
'\n',
'').replace(
'\r',
'')
229 elif ntag ==
"DateTime":
230 obj.value = child_el.text
233 obj.valuetype = obj.datatype
234 elif ntag ==
"LocalizedText":
236 elif ntag ==
"NodeId":
237 id_el = child_el.find(
"uax:Identifier", self.
ns)
238 if id_el
is not None:
239 obj.value = id_el.text
240 elif ntag ==
"ListOfExtensionObject":
242 elif ntag ==
"ListOfLocalizedText":
244 elif ntag.startswith(
"ListOf"):
246 elif ntag ==
"ExtensionObject":
251 self.logger.warning(
"Parsing value of type '%s' not implemented", ntag)
254 txtlist = [txt.strip()
for txt
in el.itertext()]
255 return "".join(txtlist)
260 ntag = self._retag.match(val_el.tag).groups()[1]
261 if ntag.startswith(
"ListOf"):
270 for localized_text
in el:
271 ntag = self._retag.match(localized_text.tag).groups()[1]
272 for child
in localized_text:
273 ntag = self._retag.match(child.tag).groups()[1]
280 Parse a uax:ListOfExtensionObject Value 281 Return an list of ExtObj 284 for extension_object
in el:
286 value.append(ext_obj)
291 for extension_object_part
in el:
292 ntag = self._retag.match(extension_object_part.tag).groups()[1]
294 ntag = self._retag.match(extension_object_part.find(
'*').tag).groups()[1]
295 ext.typeid = self.
_get_text(extension_object_part)
297 ext.objname = self._retag.match(extension_object_part.find(
'*').tag).groups()[1]
300 print(
"Uknown ndtag", ntag)
306 otag = self._retag.match(body_item.tag).groups()[1]
307 childs = [i
for i
in body_item]
313 body.append((otag, val))
318 if ref.attrib[
"ReferenceType"] ==
"HasTypeDefinition":
319 obj.typedef = ref.text
320 elif "IsForward" in ref.attrib
and ref.attrib[
"IsForward"]
in (
"false",
"False"):
323 obj.parent = ref.text
324 obj.parentlink = ref.attrib[
"ReferenceType"]
327 if "IsForward" in ref.attrib:
328 struct.forward = ref.attrib[
"IsForward"]
329 struct.target = ref.text
330 struct.reftype = ref.attrib[
"ReferenceType"]
331 obj.refs.append(struct)
def __init__(self, xmlpath)
def _set_attr(self, key, val, obj)
def _parse_list_of_localized_text(self, el)
def _parse_body(self, el)
def get_used_namespaces(self)
def _parse_list(self, el)
def _parse_attr(self, el, obj)
def _parse_ext_obj(self, el)
def _parse_value(self, val_el, obj)
def _parse_node(self, nodetype, child)
def ua_type_to_python(val, uatype)
def _parse_refs(self, el, obj)
def _parse_list_of_extension_object(self, el)