Skip to content

Commit 478081a

Browse files
committed
fix: Preprocess xml content before sending it to ElementTree parser
1 parent 8aba502 commit 478081a

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

src/ansys/fluent/core/filereader/case_file.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545
from pathlib import Path
4646
from typing import Dict, List
4747

48-
from defusedxml.ElementTree import parse
48+
import defusedxml.ElementTree as ET
4949
import numpy as np
5050

5151
from ansys.fluent.core.solver.error_message import allowed_name_error_message
5252

5353
from . import lispy
54+
from .pre_processor import remove_unsupported_xml_chars
5455

5556
try:
5657
import h5py
@@ -731,15 +732,17 @@ def _get_processed_string(input_string: bytes) -> str:
731732

732733

733734
def _get_case_file_name_from_flprj(flprj_file):
734-
tree = parse(flprj_file)
735-
root = tree.getroot()
736-
folder_name = root.find("Metadata").find("CurrentSimulation").get("value")[5:-1]
737-
# If the project file name begins with a digit then the node to find will be prepended
738-
# with "_". Rather than making any assumptions that this is a hard rule, or what
739-
# the scope of the rule is, simply retry with the name prepended:
740-
folder_obj = (
741-
root.find(folder_name)
742-
if root.find(folder_name) and len(root.find(folder_name)) > 0
743-
else root.find("_" + folder_name)
744-
)
745-
return folder_obj.find("Input").find("Case").find("Target").get("value")
735+
with open(flprj_file, "r") as file:
736+
content = file.read()
737+
content = remove_unsupported_xml_chars(content)
738+
root = ET.fromstring(content)
739+
folder_name = root.find("Metadata").find("CurrentSimulation").get("value")[5:-1]
740+
# If the project file name begins with a digit then the node to find will be prepended
741+
# with "_". Rather than making any assumptions that this is a hard rule, or what
742+
# the scope of the rule is, simply retry with the name prepended:
743+
folder_obj = (
744+
root.find(folder_name)
745+
if root.find(folder_name) and len(root.find(folder_name)) > 0
746+
else root.find("_" + folder_name)
747+
)
748+
return folder_obj.find("Input").find("Case").find("Target").get("value")

src/ansys/fluent/core/filereader/data_file.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
from os.path import dirname
4040
from pathlib import Path
4141

42-
from defusedxml.ElementTree import parse
42+
import defusedxml.ElementTree as ET
4343
import numpy as np
4444

4545
from . import lispy
46+
from .pre_processor import remove_unsupported_xml_chars
4647

4748
try:
4849
import h5py
@@ -224,7 +225,15 @@ def get_face_vector_field_data(self, phase_name: str, surface_id: int) -> np.arr
224225

225226

226227
def _get_data_file_name_from_flprj(flprj_file):
227-
tree = parse(flprj_file)
228-
root = tree.getroot()
229-
folder_name = root.find("Metadata").find("CurrentSimulation").get("value")[5:-1]
230-
return root.find(folder_name).find("Input").find("Case").find("Target").get("value")
228+
with open(flprj_file, "r") as file:
229+
content = file.read()
230+
content = remove_unsupported_xml_chars(content)
231+
root = ET.fromstring(content)
232+
folder_name = root.find("Metadata").find("CurrentSimulation").get("value")[5:-1]
233+
return (
234+
root.find(folder_name)
235+
.find("Input")
236+
.find("Case")
237+
.find("Target")
238+
.get("value")
239+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Pre-processor for XML content from Fluent project files.
3+
"""
4+
5+
import re
6+
7+
8+
def remove_unsupported_xml_chars(content: str):
9+
"""Remove unsupported XML characters from the content."""
10+
# Replace double colons in tag names with double underscores
11+
content = re.sub(r"</?([^> ]+)::", r"<\1__", content)
12+
13+
return content

tests/test_casereader.py

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import pathlib
2525
import shutil
2626

27+
import defusedxml.ElementTree as ET
2728
import pytest
2829

2930
from ansys.fluent.core import examples
@@ -35,6 +36,7 @@
3536
_get_processed_string,
3637
)
3738
from ansys.fluent.core.filereader.case_file import CaseFile as CaseReader
39+
from ansys.fluent.core.filereader.pre_processor import remove_unsupported_xml_chars
3840

3941

4042
def call_casereader(
@@ -344,3 +346,25 @@ def test_mesh_reader():
344346
assert mesh_reader_2d.precision() is None
345347
assert mesh_reader_3d.precision() is None
346348
assert case_reader.precision() == 2
349+
350+
351+
def test_preprocessor():
352+
content = """
353+
<Project type="object" class="PFolder">
354+
<Metadata type="object" class="ansys::Project::MetadataHoof">
355+
<ProjectStoragePolicy::FolderEnabled class="string" value="true"/>
356+
</Metadata>
357+
</Project>
358+
"""
359+
expected = """
360+
<Project type="object" class="PFolder">
361+
<Metadata type="object" class="ansys::Project::MetadataHoof">
362+
<ProjectStoragePolicy__FolderEnabled class="string" value="true"/>
363+
</Metadata>
364+
</Project>
365+
"""
366+
with pytest.raises(ET.ParseError):
367+
ET.fromstring(content)
368+
pre_processed = remove_unsupported_xml_chars(content)
369+
assert pre_processed == expected
370+
assert ET.fromstring(pre_processed)

0 commit comments

Comments
 (0)