Commit 54d63786 authored by Helmut Jacob's avatar Helmut Jacob Committed by Christian Bergmiller

Allow importing XML from string

Introduce an optional parameter xmlstring throughout the import_xml
chain (client, server, xmlimporter, xmlparser) to allow specifying
a XML string instead of a path.

If both are given the string takes precendence.
parent 6faf349b
......@@ -524,12 +524,12 @@ class Client(object):
def delete_nodes(self, nodes, recursive=False):
return delete_nodes(self.uaclient, nodes, recursive)
def import_xml(self, path):
def import_xml(self, path=None, xmlstring=None):
"""
Import nodes defined in xml
"""
importer = XmlImporter(self)
return importer.import_xml(path)
return importer.import_xml(path, xmlstring)
def export_xml(self, nodes, path):
"""
......
......@@ -46,12 +46,12 @@ class XmlImporter(object):
aliases_mapped[alias] = self.to_nodeid(node_id)
return aliases_mapped
def import_xml(self, xmlpath):
def import_xml(self, xmlpath=None, xmlstring=None):
"""
import xml and return added nodes
"""
self.logger.info("Importing XML file %s", xmlpath)
self.parser = xmlparser.XMLParser(xmlpath)
self.parser = xmlparser.XMLParser(xmlpath, xmlstring)
self.namespaces = self._map_namespaces(self.parser.get_used_namespaces())
self.aliases = self._map_aliases(self.parser.get_aliases())
......
......@@ -89,13 +89,16 @@ class ExtObj(object):
class XMLParser(object):
def __init__(self, xmlpath):
def __init__(self, xmlpath=None, xmlstring=None):
self.logger = logging.getLogger(__name__)
self._retag = re.compile(r"(\{.*\})(.*)")
self.path = xmlpath
self.tree = ET.parse(xmlpath)
self.root = self.tree.getroot()
if xmlstring:
self.root = ET.fromstring(xmlstring)
else:
self.root = ET.parse(xmlpath).getroot()
# FIXME: hard to get these xml namespaces with ElementTree, we may have to shift to lxml
self.ns = {
'base': "http://opcfoundation.org/UA/2011/03/UANodeSet.xsd",
......
......@@ -433,12 +433,12 @@ class Server:
await custom_t.add_method(idx, method[0], method[1], method[2], method[3])
return custom_t
def import_xml(self, path):
def import_xml(self, path=None, xmlstring=None):
"""
Import nodes defined in xml
"""
importer = XmlImporter(self)
return importer.import_xml(path)
return importer.import_xml(path, xmlstring)
def export_xml(self, nodes, path):
"""
......
......@@ -349,11 +349,7 @@ class XmlTests(object):
</UANodeSet>
"""
fp = open('tmp_test_import-nillable.xml', 'w')
fp.write(xml)
fp.close()
# TODO: when the xml parser also support loading from string, remove write to file
_new_nodes = self.opc.import_xml('tmp_test_import-nillable.xml')
_new_nodes = self.opc.import_xml(xmlstring=xml)
var_string = self.opc.get_node(ua.NodeId('test_xml.string.nillabel', 2))
var_bool = self.opc.get_node(ua.NodeId('test_xml.bool.nillabel', 2))
self.assertEqual(var_string.get_value(), None)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment