Skip to content
Snippets Groups Projects
Commit 906c6289 authored by Bonnin Xavier's avatar Bonnin Xavier
Browse files

Merge branch 'develop'

parents e9c7c4ab b5239ad1
Branches
Tags 0.6.2
No related merge requests found
CHANGELOG
====================
0.6.2
-----
* Add make_param_xml() method
0.6.1
-----
* Rename etree_to_dict() to xml_to_dict() and use dicttoxml module instead
......
......@@ -7,7 +7,7 @@ import xmltodict
from edds_process.globals import LOGGER
__all__ = ['make_tmraw_xml', 'make_tcreport_xml']
__all__ = ['make_tmraw_xml', 'make_tcreport_xml', 'make_param_xml']
def make_tmraw_xml(packets, output_file,
overwrite=False,
......@@ -102,3 +102,51 @@ def make_tcreport_xml(tcreport_list, output_file,
return os.path.isfile(output_file)
def make_param_xml(param_list, output_file,
overwrite=False,
logger=LOGGER):
"""
Create a MOC DDS Param XML file with input ParamSampleListElement elements.
:param param_list: List of ParamSampleListElement XML Elements
to save into the DDS XML file (provided as dictionary)
:param output_file: Path and name of the output DDS XML file
:param overwrite: If True then replace existing output file, else abort output file production
:return: True if output file has been successfully generated, False otherwise.
"""
if os.path.isfile(output_file) and not overwrite:
logger.warning(f'{output_file} already exists, skip output file production!')
return output_file
elif os.path.isfile(output_file) and overwrite:
logger.debug(f'{output_file} already exists and will be replaced!')
else:
logger.debug(f'Writing {output_file} with {len(param_list)} element(s)')
# Prepare input dictionary to be serialized by dicttoxml
param_data= {'Response':
{'ParamResponse':
{'ParamSampleList':
{'ParamSampleListElement':param_list}}}}
# Build XML snippet from input param element list
param_xml = xmltodict.unparse(param_data)
# Remove the first line (<?xml version="1.0" encoding="utf-8"?>)
# automatically added by xmltodict.unparse() method
param_xml = param_xml.split('\n')[1]
# Add header and root parts
param_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' \
f'<ns2:ResponsePart xmlns:ns2="http://edds.egos.esa/model">' \
f'{param_xml}</ns2:ResponsePart>'
# Create output Param XML file
logger.debug(f'Saving output EDDS Param XML file {output_file}')
with open(output_file, "w") as fw:
fw.write(param_xml)
return os.path.isfile(output_file)
......@@ -221,4 +221,5 @@ def count_packets(xml_file, file_type=None,
return []
tree = ET.parse(xml_file)
return len(tree.findall(f'.//{element_name}'))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment