Skip to content

Commit 0eb1175

Browse files
committed
better read failure messages
1 parent 60c4535 commit 0eb1175

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/meshio/_helpers.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

3+
import sys
34
from pathlib import Path
45

56
import numpy as np
67
from numpy.typing import ArrayLike
78

8-
from ._common import num_nodes_per_cell, warn
9+
from ._common import error, num_nodes_per_cell
910
from ._exceptions import ReadError, WriteError
1011
from ._files import is_buffer
1112
from ._mesh import CellBlock, Mesh
@@ -15,14 +16,17 @@
1516
_writer_map = {}
1617

1718

18-
def register_format(format_name: str, extensions: list[str], reader, writer_map):
19+
def register_format(
20+
format_name: str, extensions: list[str], reader, writer_map
21+
) -> None:
1922
for ext in extensions:
2023
if ext not in extension_to_filetypes:
2124
extension_to_filetypes[ext] = []
2225
extension_to_filetypes[ext].append(format_name)
2326

2427
if reader is not None:
2528
reader_map[format_name] = reader
29+
2630
_writer_map.update(writer_map)
2731

2832

@@ -86,19 +90,22 @@ def _read_file(path: Path, file_format: str | None):
8690
raise ReadError(f"File {path} not found.")
8791

8892
if file_format:
89-
file_formats = [file_format]
93+
possible_file_formats = [file_format]
9094
else:
9195
# deduce possible file formats from extension
92-
file_formats = _filetypes_from_path(path)
96+
possible_file_formats = _filetypes_from_path(path)
9397

94-
for file_format in file_formats:
98+
for file_format in possible_file_formats:
9599
if file_format not in reader_map:
96100
raise ReadError(f"Unknown file format '{file_format}' of '{path}'.")
97101

98102
try:
99103
return reader_map[file_format](str(path))
100104
except ReadError:
101-
warn(f"Failed to read {path} as {file_format}")
105+
pass
106+
107+
error(f"Couldn't read file {path} as either of {', '.join(possible_file_formats)}")
108+
sys.exit(1)
102109

103110

104111
def write_points_cells(

src/meshio/ansys/_ansys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,4 @@ def write(filename, mesh, binary=True):
461461
first_index = last_index + 1
462462

463463

464-
register_format("ansys", [], read, {"ansys": write})
464+
register_format("ansys", [".msh"], read, {"ansys": write})

0 commit comments

Comments
 (0)