From a8a4183041946ee438508913c2019d21643ffecf Mon Sep 17 00:00:00 2001 From: Paolo Andreetto Date: Mon, 30 Oct 2023 12:08:37 +0100 Subject: [PATCH 1/3] Changes for included files --- .../scripts/convertMarlinSteeringToGaudi.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py b/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py index 5413bce4..4c3ed6e4 100755 --- a/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py +++ b/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py @@ -268,6 +268,20 @@ def escapeIllegalChars(file_str): return file_str.replace("&&", "&&") +def includeFiles(tree): + for p_item in tree.findall('.//include/..'): + for if_item in p_item.findall('./include'): + try: + with open(if_item.attrib['ref'], "r+") as iFile: + # workaround: part files are not xml compliant + fStr = r'<%s>%s' % (p_item.tag, escapeIllegalChars(iFile.read()), p_item.tag) + iTree = ElementTree(fromstring(fStr)) + for e_item in iTree.findall('./*'): + p_item.append(e_item) + except Exception as ex: + print("Exception when including file: %r " % ex) + + def run(inputfile, outputfile): with open(inputfile, "r+") as infile: escaped_str = escapeIllegalChars(infile.read()) @@ -278,6 +292,9 @@ def run(inputfile, outputfile): print("Exception when getting trees: %r " % ex) sys.exit(1) + # TODO check compliance with https://www.w3.org/TR/xinclude/ + includeFiles(tree) + with open(outputfile, 'w') as wf_file: wf_file.write("\n".join(generateGaudiSteering(tree))) From b8e9047001521fa2333c5ea635dffab231b18efc Mon Sep 17 00:00:00 2001 From: Paolo Andreetto Date: Mon, 30 Oct 2023 17:01:32 +0100 Subject: [PATCH 2/3] Recursion for nested includes --- k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py b/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py index 4c3ed6e4..0a0cf5a6 100755 --- a/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py +++ b/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py @@ -268,14 +268,17 @@ def escapeIllegalChars(file_str): return file_str.replace("&&", "&&") -def includeFiles(tree): +def includeFiles(tree, infile): for p_item in tree.findall('.//include/..'): for if_item in p_item.findall('./include'): try: - with open(if_item.attrib['ref'], "r+") as iFile: + #TODO resolve path for part-files + nested_file = if_item.attrib['ref'] + with open(nested_file, "r+") as iFile: # workaround: part files are not xml compliant fStr = r'<%s>%s' % (p_item.tag, escapeIllegalChars(iFile.read()), p_item.tag) iTree = ElementTree(fromstring(fStr)) + includeFiles(iTree, nested_file) for e_item in iTree.findall('./*'): p_item.append(e_item) except Exception as ex: @@ -293,7 +296,7 @@ def run(inputfile, outputfile): sys.exit(1) # TODO check compliance with https://www.w3.org/TR/xinclude/ - includeFiles(tree) + includeFiles(tree, inputfile) with open(outputfile, 'w') as wf_file: wf_file.write("\n".join(generateGaudiSteering(tree))) From 54ccdf4996fe454d6c4f41290c7d6434145c601f Mon Sep 17 00:00:00 2001 From: Paolo Andreetto Date: Tue, 31 Oct 2023 15:04:32 +0100 Subject: [PATCH 3/3] Resolved paths in part-files --- k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py b/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py index 0a0cf5a6..54eb1625 100755 --- a/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py +++ b/k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py @@ -22,6 +22,7 @@ from copy import deepcopy import re import sys +import os.path from xml.etree.ElementTree import fromstring, ElementTree @@ -269,11 +270,13 @@ def escapeIllegalChars(file_str): def includeFiles(tree, infile): + wdir = os.path.dirname(os.path.abspath(infile)) for p_item in tree.findall('.//include/..'): for if_item in p_item.findall('./include'): try: - #TODO resolve path for part-files nested_file = if_item.attrib['ref'] + if not os.path.isabs(nested_file): + nested_file = os.path.join(wdir, nested_file) with open(nested_file, "r+") as iFile: # workaround: part files are not xml compliant fStr = r'<%s>%s' % (p_item.tag, escapeIllegalChars(iFile.read()), p_item.tag)