Skip to content

Commit

Permalink
Merge pull request #1290 from nschloe/rename-sets
Browse files Browse the repository at this point in the history
fixes for flac3d, vtk, vtu
  • Loading branch information
nschloe authored Feb 24, 2022
2 parents 584a142 + 6902766 commit 7f41c04
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 138 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = meshio
version = 5.3.0
version = 5.3.1
author = Nico Schlömer et al.
author_email = nico.schloemer@gmail.com
description = I/O for many mesh formats
Expand Down
37 changes: 37 additions & 0 deletions src/meshio/_common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from xml.etree import ElementTree as ET

import numpy as np
Expand Down Expand Up @@ -120,6 +122,10 @@ def _pick_first_int_data(data):
return key, other


def info(string, highlight: bool = True) -> None:
Console(stderr=True).print(f"[bold]Info:[/bold] {string}", highlight=highlight)


def warn(string, highlight: bool = True) -> None:
Console(stderr=True).print(
f"[yellow][bold]Warning:[/bold] {string}[/yellow]", highlight=highlight
Expand All @@ -130,3 +136,34 @@ def error(string, highlight: bool = True) -> None:
Console(stderr=True).print(
f"[red][bold]Error:[/bold] {string}[/red]", highlight=highlight
)


def is_in_any(string: str, strings: list[str]) -> bool:
"""True if `string` is contained in any of `strings`."""
for s in strings:
if string in s:
return True
return False


def join_strings(strings: list[str]) -> tuple[str, str]:
"""Join strings such that they can be uniquely split again afterwards."""
possible_join_chars = ["-", "_", "#", "+", "/"]
char = None
for c in possible_join_chars:
if not is_in_any(c, strings):
char = c
break
assert char is not None
return char.join(strings), char


def replace_space(string: str) -> tuple[str, str]:
possible_chars = ["_", "-", "+", "X", "/", "#"]
char = None
for c in possible_chars:
if c not in string:
char = c
break
assert char is not None
return string.replace(" ", char), char
12 changes: 9 additions & 3 deletions src/meshio/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ def _read_file(path: Path, file_format: str | None):

try:
return reader_map[file_format](str(path))
except ReadError:
pass
except ReadError as e:
print(e)

if len(possible_file_formats) == 1:
msg = f"Couldn't read file {path} as {possible_file_formats[0]}"
else:
lst = ", ".join(possible_file_formats)
msg = f"Couldn't read file {path} as either of {lst}"

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


Expand Down
9 changes: 5 additions & 4 deletions src/meshio/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def read(cls, path_or_buf, file_format=None):
warn("meshio.Mesh.read is deprecated, use meshio.read instead")
return read(path_or_buf, file_format)

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

data_name = "-".join(self.cell_sets.keys())
if data_name is None:
data_name = "-".join(self.cell_sets.keys())
self.cell_data[data_name] = intfun
self.cell_sets = {}

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

data_name = "-".join(self.point_sets.keys())
data_name = join_char.join(self.point_sets.keys())
self.point_data[data_name] = intfun
self.point_sets = {}

Expand Down
Loading

0 comments on commit 7f41c04

Please sign in to comment.