mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-27 21:55:41 -04:00
Sort XML attributes to preserve pre-Python 3.8 XML writing behavior
This commit is contained in:
parent
2979cfa2f4
commit
16d7ec0641
7 changed files with 30 additions and 5 deletions
|
|
@ -43,3 +43,21 @@ def get_text(elem, name, default=None):
|
|||
else:
|
||||
child = elem.find(name)
|
||||
return child.text if child is not None else default
|
||||
|
||||
|
||||
def reorder_attributes(root):
|
||||
"""Sort attributes in XML to preserve pre-Python 3.8 behavior
|
||||
|
||||
Parameters
|
||||
----------
|
||||
root : xml.etree.ElementTree.Element
|
||||
Root element
|
||||
|
||||
"""
|
||||
for el in root.iter():
|
||||
attrib = el.attrib
|
||||
if len(attrib) > 1:
|
||||
# adjust attribute order, e.g. by sorting
|
||||
attribs = sorted(attrib.items())
|
||||
attrib.clear()
|
||||
attrib.update(attribs)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import pathlib
|
|||
import h5py
|
||||
|
||||
from openmc.mixin import EqualityMixin
|
||||
from openmc._xml import clean_indentation
|
||||
from openmc._xml import clean_indentation, reorder_attributes
|
||||
|
||||
|
||||
class DataLibrary(EqualityMixin):
|
||||
|
|
@ -112,6 +112,7 @@ class DataLibrary(EqualityMixin):
|
|||
clean_indentation(root)
|
||||
|
||||
# Write XML file
|
||||
reorder_attributes(root) # TODO: Remove when support is Python 3.8+
|
||||
tree = ET.ElementTree(root)
|
||||
tree.write(str(path), xml_declaration=True, encoding='utf-8',
|
||||
method='xml')
|
||||
|
|
|
|||
|
|
@ -110,6 +110,7 @@ class Geometry:
|
|||
p /= 'geometry.xml'
|
||||
|
||||
# Write the XML Tree to the geometry.xml file
|
||||
xml.reorder_attributes(root_element) # TODO: Remove when support is Python 3.8+
|
||||
tree = ET.ElementTree(root_element)
|
||||
tree.write(str(p), xml_declaration=True, encoding='utf-8')
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import numpy as np
|
|||
import openmc
|
||||
import openmc.data
|
||||
import openmc.checkvalue as cv
|
||||
from ._xml import clean_indentation
|
||||
from ._xml import clean_indentation, reorder_attributes
|
||||
from .mixin import IDManagerMixin
|
||||
|
||||
|
||||
|
|
@ -1239,6 +1239,7 @@ class Materials(cv.CheckedList):
|
|||
clean_indentation(element, level=1)
|
||||
element.tail = element.tail.strip(' ')
|
||||
fh.write(' ')
|
||||
reorder_attributes(element) # TODO: Remove when support is Python 3.8+
|
||||
ET.ElementTree(element).write(fh, encoding='unicode')
|
||||
|
||||
# Write the <material> elements.
|
||||
|
|
@ -1247,6 +1248,7 @@ class Materials(cv.CheckedList):
|
|||
clean_indentation(element, level=1)
|
||||
element.tail = element.tail.strip(' ')
|
||||
fh.write(' ')
|
||||
reorder_attributes(element) # TODO: Remove when support is Python 3.8+
|
||||
ET.ElementTree(element).write(fh, encoding='unicode')
|
||||
|
||||
# Write the closing tag for the root element.
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import numpy as np
|
|||
|
||||
import openmc
|
||||
import openmc.checkvalue as cv
|
||||
from ._xml import clean_indentation
|
||||
from ._xml import clean_indentation, reorder_attributes
|
||||
from .mixin import IDManagerMixin
|
||||
|
||||
|
||||
|
|
@ -844,5 +844,6 @@ class Plots(cv.CheckedList):
|
|||
p /= 'plots.xml'
|
||||
|
||||
# Write the XML Tree to the plots.xml file
|
||||
reorder_attributes(self._plots_file) # TODO: Remove when support is Python 3.8+
|
||||
tree = ET.ElementTree(self._plots_file)
|
||||
tree.write(str(p), xml_declaration=True, encoding='utf-8')
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from xml.etree import ElementTree as ET
|
|||
|
||||
import openmc.checkvalue as cv
|
||||
from . import VolumeCalculation, Source, RegularMesh
|
||||
from ._xml import clean_indentation, get_text
|
||||
from ._xml import clean_indentation, get_text, reorder_attributes
|
||||
|
||||
|
||||
class RunMode(Enum):
|
||||
|
|
@ -1375,6 +1375,7 @@ class Settings:
|
|||
p /= 'settings.xml'
|
||||
|
||||
# Write the XML Tree to the settings.xml file
|
||||
reorder_attributes(root_element) # TODO: Remove when support is Python 3.8+
|
||||
tree = ET.ElementTree(root_element)
|
||||
tree.write(str(p), xml_declaration=True, encoding='utf-8')
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import scipy.sparse as sps
|
|||
|
||||
import openmc
|
||||
import openmc.checkvalue as cv
|
||||
from ._xml import clean_indentation
|
||||
from ._xml import clean_indentation, reorder_attributes
|
||||
from .mixin import IDManagerMixin
|
||||
|
||||
|
||||
|
|
@ -3143,5 +3143,6 @@ class Tallies(cv.CheckedList):
|
|||
p /= 'tallies.xml'
|
||||
|
||||
# Write the XML Tree to the tallies.xml file
|
||||
reorder_attributes(root_element) # TODO: Remove when support is Python 3.8+
|
||||
tree = ET.ElementTree(root_element)
|
||||
tree.write(str(p), xml_declaration=True, encoding='utf-8')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue