Skip to content

Commit

Permalink
Combine Initialiser if previous XML existed
Browse files Browse the repository at this point in the history
  • Loading branch information
sevisal committed Sep 15, 2023
1 parent 1667f40 commit aedc426
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/vai_lab/Data/xml_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,37 @@ def load_XML(self, filename: str) -> None:
self.set_filename(filename)
if hasattr(self, 'tree'):
prev_tree = self.tree
self.tree = self._combine_XML(ET.parse(self.filename).getroot(), prev_tree.getroot())
self.tree = self._combine_Initialiser(ET.parse(self.filename).getroot(), prev_tree.getroot(), False)
else:
self.tree = ET.parse(self.filename)
self._parse_XML()

def _combine_Initialiser(self, tree1, tree2, module = True):

# Create a mapping from tag name to element, as that's what we are fltering with
mapping = {el.tag: el for el in tree1}
for el in tree2:
if el.tag == 'Initialiser' or module:
if len(el) == 0:
# Not nested
try:
# Update the text
mapping[el.tag].text = el.text
except KeyError:
# An element with this name is not in the mapping
mapping[el.tag] = el
# Add it
tree1.append(el)
else:
try:
# Recursively process the element, and update it in the same way
self._combine_XML(mapping[el.tag], el)
except KeyError:
# Not in the mapping
mapping[el.tag] = el
tree1.append(el)
return ET.ElementTree(tree1)

def _combine_XML(self, tree1, tree2):
"""
This function recursively updates either the text or the children
Expand All @@ -119,14 +145,13 @@ def _combine_XML(self, tree1, tree2):
mapping[el.tag] = el
# Add it
tree1.append(el)
else:
else :
try:
# Recursively process the element, and update it in the same way
self._combine_XML(mapping[el.tag], el)
except KeyError:
# Not in the mapping
mapping[el.tag] = el
# Just add it
tree1.append(el)
return ET.ElementTree(tree1)

Expand Down

0 comments on commit aedc426

Please sign in to comment.