Skip to content

Commit 5550289

Browse files
authored
Merge pull request #807 from nschloe/remove-cdata-workaround
Remove cdata workaround
2 parents bcb3bdf + 6ec0e91 commit 5550289

File tree

6 files changed

+47
-68
lines changed

6 files changed

+47
-68
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
2-
ignore = E203, E266, E501, W503, C901
2+
ignore = E203, E266, E501, W503, C901, E741
33
max-line-length = 88
44
max-complexity = 18
55
select = B,C,E,F,W,T4,B9

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tag:
1111
# Always create a github "release" right after tagging so it appears on zenodo
1212
curl -H "Authorization: token `cat $(HOME)/.github-access-token`" -d '{"tag_name": "v$(VERSION)"}' https://api.github.com/repos/nschloe/meshio/releases
1313

14-
upload: clean setup.py
14+
upload: clean
1515
# Make sure we're on the master branch
1616
@if [ "$(shell git rev-parse --abbrev-ref HEAD)" != "master" ]; then exit 1; fi
1717
rm -f dist/*

meshio/_common.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,30 +157,6 @@ def raw_from_cell_data(cell_data):
157157
return {name: numpy.concatenate(value) for name, value in cell_data.items()}
158158

159159

160-
# https://stackoverflow.com/a/30019607/353337
161-
def CDATA(text=None):
162-
element = ET.Element("![CDATA[")
163-
element.text = text
164-
return element
165-
166-
167-
ET._original_serialize_xml = ET._serialize_xml
168-
169-
170-
def _serialize_xml(write, elem, qnames, namespaces, short_empty_elements, **kwargs):
171-
if elem.tag == "![CDATA[":
172-
write("\n<{}{}]]>\n".format(elem.tag, elem.text))
173-
if elem.tail:
174-
write(ET._escape_cdata(elem.tail))
175-
else:
176-
return ET._original_serialize_xml(
177-
write, elem, qnames, namespaces, short_empty_elements, **kwargs
178-
)
179-
180-
181-
ET._serialize_xml = ET._serialize["xml"] = _serialize_xml
182-
183-
184160
def write_xml(filename, root):
185161
tree = ET.ElementTree(root)
186162
tree.write(filename)

meshio/xdmf/main.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy
1010

11-
from .._common import CDATA, cell_data_from_raw, raw_from_cell_data, write_xml
11+
from .._common import cell_data_from_raw, raw_from_cell_data, write_xml
1212
from .._exceptions import ReadError, WriteError
1313
from .._helpers import register
1414
from .._mesh import CellBlock, Mesh
@@ -339,12 +339,12 @@ def __init__(
339339

340340
domain = ET.SubElement(xdmf_file, "Domain")
341341
grid = ET.SubElement(domain, "Grid", Name="Grid")
342-
information = ET.SubElement(
343-
grid, "Information", Name="Information", Value=str(len(mesh.field_data))
344-
)
342+
# information = ET.SubElement(
343+
# grid, "Information", Name="Information", Value=str(len(mesh.field_data))
344+
# )
345345

346346
self.points(grid, mesh.points)
347-
self.field_data(mesh.field_data, information)
347+
# self.field_data(mesh.field_data, information)
348348
self.cells(mesh.cells, grid)
349349
self.point_data(mesh.point_data, grid)
350350
self.cell_data(mesh.cell_data, grid)
@@ -510,13 +510,18 @@ def cell_data(self, cell_data, grid):
510510
)
511511
data_item.text = self.numpy_to_xml_string(data)
512512

513-
def field_data(self, field_data, information):
514-
info = ET.Element("main")
515-
for name, data in field_data.items():
516-
data_item = ET.SubElement(info, "map", key=name, dim=str(data[1]))
517-
data_item.text = str(data[0])
518-
# information.text = ET.CDATA(ET.tostring(info))
519-
information.append(CDATA(ET.tostring(info).decode("utf-8")))
513+
# The original idea was to implement field data as XML CDATA. Unfortunately, in
514+
# Python's XML, CDATA handled poorly. There are workarounds, e.g.,
515+
# <https://stackoverflow.com/a/30019607/353337>, but those mess around with
516+
# ET._serialize_xml and lead to bugs elsewhere
517+
# <https://github.com/nschloe/meshio/issues/806>.
518+
# def field_data(self, field_data, information):
519+
# info = ET.Element("main")
520+
# for name, data in field_data.items():
521+
# data_item = ET.SubElement(info, "map", key=name, dim=str(data[1]))
522+
# data_item.text = str(data[0])
523+
# information.text = ET.CDATA(ET.tostring(info))
524+
# information.append(CDATA(ET.tostring(info).decode("utf-8")))
520525

521526

522527
def write(*args, **kwargs):

setup.cfg

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = meshio
3-
version = 4.0.12
3+
version = 4.0.13
44
author = Nico Schlömer et al.
55
66
description = I/O for many mesh formats
@@ -28,9 +28,8 @@ classifiers =
2828

2929
[options]
3030
packages = find:
31-
# importlib_metadata can be removed when we support Python 3.8+ only
3231
install_requires =
33-
importlib_metadata
32+
importlib_metadata;python_version<"3.8"
3433
numpy
3534
python_requires = >=3.5
3635
setup_requires =

test/test_xdmf.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,32 @@ def test_time_series():
8888
assert numpy.all(numpy.abs(value - point_data[k][key]) < 1.0e-12)
8989

9090

91-
def test_information_xdmf():
92-
mesh_out = meshio.Mesh(
93-
numpy.array(
94-
[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]
95-
)
96-
/ 3,
97-
[("triangle", numpy.array([[0, 1, 2], [0, 2, 3]]))],
98-
field_data={
99-
"bottom": numpy.array([1, 1]),
100-
"right": numpy.array([2, 1]),
101-
"top": numpy.array([3, 1]),
102-
"left": numpy.array([4, 1]),
103-
},
104-
)
105-
# write the data
106-
points, cells, field_data = mesh_out.points, mesh_out.cells, mesh_out.field_data
107-
108-
assert cells[0].type == "triangle"
109-
meshio.write(
110-
"mesh.xdmf",
111-
meshio.Mesh(points=points, cells=[cells[0]], field_data=field_data),
112-
)
113-
114-
# read it back in
115-
mesh_in = meshio.read("mesh.xdmf")
116-
117-
assert len(mesh_in.field_data) == len(mesh_out.field_data)
91+
# def test_information_xdmf():
92+
# mesh_out = meshio.Mesh(
93+
# numpy.array(
94+
# [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]]
95+
# )
96+
# / 3,
97+
# [("triangle", numpy.array([[0, 1, 2], [0, 2, 3]]))],
98+
# field_data={
99+
# "bottom": numpy.array([1, 1]),
100+
# "right": numpy.array([2, 1]),
101+
# "top": numpy.array([3, 1]),
102+
# "left": numpy.array([4, 1]),
103+
# },
104+
# )
105+
# # write the data
106+
# points, cells, field_data = mesh_out.points, mesh_out.cells, mesh_out.field_data
107+
#
108+
# assert cells[0].type == "triangle"
109+
# meshio.write(
110+
# "mesh.xdmf",
111+
# meshio.Mesh(points=points, cells=[cells[0]], field_data=field_data),
112+
# )
113+
#
114+
# # read it back in
115+
# mesh_in = meshio.read("mesh.xdmf")
116+
# assert len(mesh_in.field_data) == len(mesh_out.field_data)
118117

119118

120119
if __name__ == "__main__":

0 commit comments

Comments
 (0)