Skip to content

Commit

Permalink
Change XML attribute order to alphabetical
Browse files Browse the repository at this point in the history
For reproducible output.
Default before Python 3.8.
  • Loading branch information
Rotzbua authored and florianfesti committed Sep 10, 2023
1 parent 26716a4 commit c7edb1c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions boxes/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
RANDOMIZE_COLORS = False # enable to ease check for continuity of paths


def reorder_attributes(root) -> None:
"""
Source: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.remove
"""
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)


def points_equal(x1, y1, x2, y2):
return abs(x1 - x2) < EPS and abs(y1 - y2) < EPS

Expand Down Expand Up @@ -577,6 +590,7 @@ def finish(self, inner_corners="loop"):
t.set("stroke-width", f'{path.params["lw"]:.2f}')
t.tail = "\n "
t.tail = "\n"
reorder_attributes(tree)
with open(self._fname, "wb") as f:
tree.write(f, encoding="utf-8", xml_declaration=True, method="xml")

Expand Down

0 comments on commit c7edb1c

Please sign in to comment.