Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for included files #155

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions k4MarlinWrapper/scripts/convertMarlinSteeringToGaudi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from copy import deepcopy
import re
import sys
import os.path

from xml.etree.ElementTree import fromstring, ElementTree

Expand Down Expand Up @@ -268,6 +269,25 @@ def escapeIllegalChars(file_str):
return file_str.replace("&&", "&&")


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:
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</%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:
print("Exception when including file: %r " % ex)


def run(inputfile, outputfile):
with open(inputfile, "r+") as infile:
escaped_str = escapeIllegalChars(infile.read())
Expand All @@ -278,6 +298,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, inputfile)

with open(outputfile, 'w') as wf_file:
wf_file.write("\n".join(generateGaudiSteering(tree)))

Expand Down
Loading