Skip to content

Commit a827699

Browse files
improve: attempt to patch broken xml
1 parent a5bc2e4 commit a827699

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

kmm/header/header.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22
from xml.etree import ElementTree
3+
34
from pydantic import validate_arguments
45

56
import kmm
@@ -18,8 +19,8 @@ def from_path(path: Path):
1819
"""
1920
try:
2021
tree = ElementTree.parse(path)
21-
except ElementTree.ParseError as e:
22-
raise ValueError("Unable to parse header file, invalid XML.") from e
22+
except ElementTree.ParseError:
23+
tree = attempt_to_patch_xml(path)
2324

2425
position, sync = kmm.header.position_sync(tree)
2526
return Header(
@@ -29,6 +30,33 @@ def from_path(path: Path):
2930
)
3031

3132

33+
def attempt_to_patch_xml(path: Path):
34+
"""
35+
Attempts to patch a broken header file by adding missing end tags.
36+
"""
37+
lines = path.read_text().splitlines()
38+
start_tags = []
39+
for line in lines:
40+
if line.startswith("</") and line.endswith(">"):
41+
try:
42+
index = max(
43+
i
44+
for i, start_tag in enumerate(start_tags)
45+
if start_tag == line[2:-1]
46+
)
47+
except ValueError:
48+
raise ElementTree.ParseError(f"Missing start tag for {line}")
49+
start_tags.pop(index)
50+
elif line.startswith("<") and line.endswith(">") and not line.startswith("<?"):
51+
start_tags.append(line[1:-1])
52+
for start_tag in start_tags[::-1]:
53+
lines.append(f"</{start_tag}>")
54+
try:
55+
return ElementTree.ElementTree(ElementTree.fromstring("\n".join(lines)))
56+
except ElementTree.ParseError as e:
57+
raise ValueError("Unable to parse header file, invalid XML.") from e
58+
59+
3260
def test_header():
3361
Header.from_path("tests/ascending_B.hdr")
3462

0 commit comments

Comments
 (0)