Skip to content

Commit 7f41c04

Browse files
authored
Merge pull request #1290 from nschloe/rename-sets
fixes for flac3d, vtk, vtu
2 parents 584a142 + 6902766 commit 7f41c04

File tree

10 files changed

+261
-138
lines changed

10 files changed

+261
-138
lines changed

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = meshio
3-
version = 5.3.0
3+
version = 5.3.1
44
author = Nico Schlömer et al.
55
author_email = [email protected]
66
description = I/O for many mesh formats

src/meshio/_common.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from xml.etree import ElementTree as ET
24

35
import numpy as np
@@ -120,6 +122,10 @@ def _pick_first_int_data(data):
120122
return key, other
121123

122124

125+
def info(string, highlight: bool = True) -> None:
126+
Console(stderr=True).print(f"[bold]Info:[/bold] {string}", highlight=highlight)
127+
128+
123129
def warn(string, highlight: bool = True) -> None:
124130
Console(stderr=True).print(
125131
f"[yellow][bold]Warning:[/bold] {string}[/yellow]", highlight=highlight
@@ -130,3 +136,34 @@ def error(string, highlight: bool = True) -> None:
130136
Console(stderr=True).print(
131137
f"[red][bold]Error:[/bold] {string}[/red]", highlight=highlight
132138
)
139+
140+
141+
def is_in_any(string: str, strings: list[str]) -> bool:
142+
"""True if `string` is contained in any of `strings`."""
143+
for s in strings:
144+
if string in s:
145+
return True
146+
return False
147+
148+
149+
def join_strings(strings: list[str]) -> tuple[str, str]:
150+
"""Join strings such that they can be uniquely split again afterwards."""
151+
possible_join_chars = ["-", "_", "#", "+", "/"]
152+
char = None
153+
for c in possible_join_chars:
154+
if not is_in_any(c, strings):
155+
char = c
156+
break
157+
assert char is not None
158+
return char.join(strings), char
159+
160+
161+
def replace_space(string: str) -> tuple[str, str]:
162+
possible_chars = ["_", "-", "+", "X", "/", "#"]
163+
char = None
164+
for c in possible_chars:
165+
if c not in string:
166+
char = c
167+
break
168+
assert char is not None
169+
return string.replace(" ", char), char

src/meshio/_helpers.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,16 @@ def _read_file(path: Path, file_format: str | None):
101101

102102
try:
103103
return reader_map[file_format](str(path))
104-
except ReadError:
105-
pass
104+
except ReadError as e:
105+
print(e)
106+
107+
if len(possible_file_formats) == 1:
108+
msg = f"Couldn't read file {path} as {possible_file_formats[0]}"
109+
else:
110+
lst = ", ".join(possible_file_formats)
111+
msg = f"Couldn't read file {path} as either of {lst}"
106112

107-
error(f"Couldn't read file {path} as either of {', '.join(possible_file_formats)}")
113+
error(msg)
108114
sys.exit(1)
109115

110116

src/meshio/_mesh.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def read(cls, path_or_buf, file_format=None):
313313
warn("meshio.Mesh.read is deprecated, use meshio.read instead")
314314
return read(path_or_buf, file_format)
315315

316-
def cell_sets_to_data(self):
316+
def cell_sets_to_data(self, data_name: str | None = None):
317317
# If possible, convert cell sets to integer cell data. This is possible if all
318318
# cells appear exactly in one group.
319319
default_value = -1
@@ -337,11 +337,12 @@ def cell_sets_to_data(self):
337337
)
338338
break
339339

340-
data_name = "-".join(self.cell_sets.keys())
340+
if data_name is None:
341+
data_name = "-".join(self.cell_sets.keys())
341342
self.cell_data[data_name] = intfun
342343
self.cell_sets = {}
343344

344-
def point_sets_to_data(self):
345+
def point_sets_to_data(self, join_char: str = "-") -> None:
345346
# now for the point sets
346347
# Go for -1 as the default value. (NaN is not int.)
347348
default_value = -1
@@ -356,7 +357,7 @@ def point_sets_to_data(self):
356357
f"Using default value {default_value}."
357358
)
358359

359-
data_name = "-".join(self.point_sets.keys())
360+
data_name = join_char.join(self.point_sets.keys())
360361
self.point_data[data_name] = intfun
361362
self.point_sets = {}
362363

0 commit comments

Comments
 (0)