Skip to content
Snippets Groups Projects
Commit 753c4a1d authored by Mattia Mancini's avatar Mattia Mancini
Browse files

SW-720: abstract xml parsing to handle both string and bytestring

parent d1e9b712
No related branches found
No related tags found
1 merge request!6Import cobalt2 into lofar4
......@@ -1703,6 +1703,7 @@ LCS/PyCommon/test/t_util.sh -text
LCS/PyCommon/test_utils.py -text
LCS/PyCommon/threading_utils.py -text
LCS/PyCommon/util.py -text
LCS/PyCommon/xmlparse.py -text
LCS/PyStationModel/CMakeLists.txt -text
LCS/PyStationModel/antennasets_parser.py -text
LCS/PyStationModel/test/CMakeLists.txt -text
......
......@@ -25,6 +25,7 @@ set(_py_files
datetimeutils.py
flask_utils.py
subprocess_utils.py
xmlparse.py
test_utils.py)
python_install(${_py_files} DESTINATION lofar/common)
......
from lxml import etree
from io import BytesIO
from typing import Union
def parse_xml_string_or_bytestring(xml_content: Union[str, bytes], parser=None) -> etree:
"""
Parse the given xml content and returns a Etree
:param xml_content: content of the xml file
:return: the parsed content in the form of an element tree
"""
if isinstance(bytes, xml_content):
return etree.parse(BytesIO(xml_content), parser=parser)
else:
return etree.fromstring(xml_content, parser=parser)
\ No newline at end of file
......@@ -51,7 +51,7 @@ import datetime
from .config import VALIDATION_SERVICENAME
from .validation_service_rpc import ValidationRPC
from .specification_service import _parse_relation_tree, make_key, _parse_project_code
from lofar.common.xmlparse import parse_xml_string_or_bytestring
from lofar.messaging import DEFAULT_BROKER, DEFAULT_BUSNAME
from io import BytesIO
......@@ -742,7 +742,7 @@ class LofarXmlToMomXmlTranslator():
# Parse specification
parser = etree.XMLParser(remove_blank_text=True) # <-- prevent that prettyprinting breaks
spectree = etree.parse(BytesIO(spec_xml.encode('UTF-8')), parser=parser).getroot()
spectree = parse_xml_string_or_bytestring(spec_xml, parser=parser).getroot()
nsmap = {"lofar": "http://www.astron.nl/MoM2-Lofar",
"mom2": "http://www.astron.nl/MoM2",
......
......@@ -31,7 +31,7 @@ from lofar.messaging import RPCService, ToBus, EventMessage, DEFAULT_BROKER, DEF
from lofar.mom.momqueryservice.momqueryrpc import MoMQueryRPC
from lofar.specificationservices.translation_service_rpc import TranslationRPC
from lofar.specificationservices.validation_service_rpc import ValidationRPC
from lxml import etree
from lofar.common.xmlparse import parse_xml_string_or_bytestring
from .config import \
SPECIFICATION_SERVICENAME, \
......@@ -170,7 +170,7 @@ class SpecificationHandler(ServiceMessageHandler):
E.g. activities have to be in new folders
"""
doc = etree.parse(BytesIO(lofar_xml.encode('utf-8')))
doc = parse_xml_string_or_bytestring(lofar_xml)
spec = doc.getroot()
if spec.tag != "{http://www.astron.nl/LofarSpecification}specification":
......
......@@ -45,6 +45,8 @@ from lofar.specificationservices.telescope_model_xml_generator_type1 import Tele
logger = logging.getLogger(__name__)
from lxml import etree
from io import BytesIO
from lofar.common.xmlparse import parse_xml_string_or_bytestring
from lofar.messaging import RPCService, ServiceMessageHandler, DEFAULT_BROKER, DEFAULT_BUSNAME
from lofar.common.util import waitForInterrupt
......@@ -73,7 +75,7 @@ class SpecificationTranslationHandler(ServiceMessageHandler):
try:
# pick the specification element
parser = etree.XMLParser(remove_blank_text=True)
doc = etree.parse(BytesIO(trigger_spec.encode('UTF-8')), parser=parser)
doc = parse_xml_string_or_bytestring(trigger_spec, parser=parser)
# spec = doc.getroot().find('{http://www.astron.nl/LofarSpecification}specification')
root = doc.getroot()
if not "trigger" in root.tag:
......
......@@ -30,6 +30,7 @@ from lxml import etree
import os
from lofar.messaging import ServiceMessageHandler, DEFAULT_BROKER, DEFAULT_BUSNAME, RPCService
from lofar.common.util import waitForInterrupt
from lofar.common.xmlparse import parse_xml_string_or_bytestring
from .config import TRIGGER_XSD, LOFARSPEC_XSD, MOMSPEC_XSD, VALIDATION_SERVICENAME
......@@ -39,7 +40,7 @@ def _validateXSD(xml, xsdpath):
'''validates given xml against given xsd file'''
logger.info("Validating against " + str(xsdpath))
logger.debug("XML: "+xml)
logger.debug("XML: %s", xml)
xsdpath = os.path.expandvars(xsdpath)
......@@ -50,7 +51,7 @@ def _validateXSD(xml, xsdpath):
# Try to parse the XML
try:
doc = etree.parse(BytesIO(xml.encode('utf8')))
doc = parse_xml_string_or_bytestring(BytesIO(xml.encode('utf8')))
except etree.LxmlError as err:
logger.error(err)
return {"valid": False, "error": "XML could not be parsed: %s" % (err,)}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment