Skip to content

Commit ef13971

Browse files
authored
Merge pull request #725 from nschloe/force-ascii-switch
force-ascii switch
2 parents 20b8890 + b5ecb50 commit ef13971

File tree

18 files changed

+52
-88
lines changed

18 files changed

+52
-88
lines changed

meshio/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "4.0.2"
1+
__version__ = "4.0.3"
22
__original_author__ = "Nico Schlömer"
33
__original_author_email__ = "[email protected]"
44
__copyright__ = "Copyright (c) 2015-2020 meshio developers"

meshio/_cli/_ascii.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _get_parser():
5959
"--input-format",
6060
"-i",
6161
type=str,
62-
choices=list(reader_map.keys()),
62+
choices=sorted(list(reader_map.keys())),
6363
help="input file format",
6464
default=None,
6565
)

meshio/_cli/_binary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _get_parser():
5959
"--input-format",
6060
"-i",
6161
type=str,
62-
choices=list(reader_map.keys()),
62+
choices=sorted(list(reader_map.keys())),
6363
help="input file format",
6464
default=None,
6565
)

meshio/_cli/_compress.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _get_parser():
7979
"--input-format",
8080
"-i",
8181
type=str,
82-
choices=list(reader_map.keys()),
82+
choices=sorted(list(reader_map.keys())),
8383
help="input file format",
8484
default=None,
8585
)

meshio/_cli/_convert.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,27 @@ def convert(argv=None):
3737
kwargs = {"file_format": args.output_format}
3838
if args.float_format is not None:
3939
kwargs["float_fmt"] = args.float_format
40+
if args.ascii:
41+
kwargs["binary"] = False
4042

4143
write(args.outfile, mesh, **kwargs)
4244

4345

4446
def _get_convert_parser():
47+
# Avoid repeating format names
48+
# https://stackoverflow.com/a/31124505/353337
49+
class CustomHelpFormatter(argparse.HelpFormatter):
50+
def _format_action_invocation(self, action):
51+
if not action.option_strings or action.nargs == 0:
52+
return super()._format_action_invocation(action)
53+
default = self._get_default_metavar_for_optional(action)
54+
args_string = self._format_args(action, default)
55+
return ", ".join(action.option_strings) + " " + args_string
56+
4557
parser = argparse.ArgumentParser(
4658
description=("Convert between mesh formats."),
47-
formatter_class=argparse.RawTextHelpFormatter,
59+
# formatter_class=argparse.RawTextHelpFormatter,
60+
formatter_class=CustomHelpFormatter,
4861
)
4962

5063
parser.add_argument("infile", type=str, help="mesh file to be read from")
@@ -53,7 +66,7 @@ def _get_convert_parser():
5366
"--input-format",
5467
"-i",
5568
type=str,
56-
choices=list(reader_map.keys()),
69+
choices=sorted(list(reader_map.keys())),
5770
help="input file format",
5871
default=None,
5972
)
@@ -62,11 +75,18 @@ def _get_convert_parser():
6275
"--output-format",
6376
"-o",
6477
type=str,
65-
choices=list(_writer_map.keys()),
78+
choices=sorted(list(_writer_map.keys())),
6679
help="output file format",
6780
default=None,
6881
)
6982

83+
parser.add_argument(
84+
"--ascii",
85+
"-a",
86+
action="store_true",
87+
help="write in ASCII format variant (where applicable, default: binary)",
88+
)
89+
7090
parser.add_argument("outfile", type=str, help="mesh file to be written to")
7191

7292
parser.add_argument(

meshio/_cli/_decompress.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _get_parser():
5353
"--input-format",
5454
"-i",
5555
type=str,
56-
choices=list(reader_map.keys()),
56+
choices=sorted(list(reader_map.keys())),
5757
help="input file format",
5858
default=None,
5959
)

meshio/_cli/_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _get_info_parser():
4343
"--input-format",
4444
"-i",
4545
type=str,
46-
choices=list(reader_map.keys()),
46+
choices=sorted(list(reader_map.keys())),
4747
help="input file format",
4848
default=None,
4949
)

meshio/ansys/_ansys.py

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

463463

464-
register(
465-
"ansys",
466-
[],
467-
read,
468-
{
469-
"ansys": lambda f, m, **kwargs: write(f, m, **kwargs, binary=True),
470-
"ansys-ascii": lambda f, m, **kwargs: write(f, m, **kwargs, binary=False),
471-
"ansys-binary": lambda f, m, **kwargs: write(f, m, **kwargs, binary=True),
472-
},
473-
)
464+
register("ansys", [], read, {"ansys": write})

meshio/gmsh/_gmsh22.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ def _write_elements(fh, cells, tag_data, binary):
378378
form.format(
379379
consecutive_index + k + 1,
380380
" ".join([str(val) for val in fcd[k]]),
381-
" ".join([str(cc + 1) for cc in c]),
381+
# a bit clumsy for `c+1`, but if c is uint64, c+1 is float64
382+
" ".join([str(cc) for cc in c + numpy.array(1, dtype=c.dtype)]),
382383
).encode("utf-8")
383384
)
384385

meshio/gmsh/main.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import struct
22

3-
from .._exceptions import ReadError
3+
from .._exceptions import ReadError, WriteError
44
from .._helpers import register
55
from . import _gmsh22, _gmsh40, _gmsh41
66

7-
_readers = {"2": _gmsh22, "2.2": _gmsh22, "4": _gmsh40, "4.0": _gmsh40, "4.1": _gmsh41}
8-
_writers = {"2": _gmsh22, "2.2": _gmsh22, "4": _gmsh41, "4.0": _gmsh40, "4.1": _gmsh41}
7+
_readers = {"2.2": _gmsh22, "4.0": _gmsh40, "4.1": _gmsh41}
8+
_writers = {"2.2": _gmsh22, "4.0": _gmsh40, "4.1": _gmsh41}
99

1010

1111
def read(filename):
@@ -30,6 +30,7 @@ def read_buffer(f):
3030
if line != "$MeshFormat":
3131
raise ReadError()
3232
fmt_version, data_size, is_ascii = _read_header(f)
33+
print(fmt_version, data_size, is_ascii)
3334

3435
try:
3536
reader = _readers[fmt_version]
@@ -90,9 +91,9 @@ def write(filename, mesh, fmt_version="4.1", binary=True):
9091
writer = _writers[fmt_version]
9192
except KeyError:
9293
try:
93-
writer = _writers[fmt_version.split(".")[0]]
94+
writer = _writers[fmt_version]
9495
except KeyError:
95-
raise ValueError(
96+
raise WriteError(
9697
"Need mesh format in {} (got {})".format(
9798
sorted(_writers.keys()), fmt_version
9899
)
@@ -106,10 +107,7 @@ def write(filename, mesh, fmt_version="4.1", binary=True):
106107
[".msh"],
107108
read,
108109
{
109-
"gmsh": lambda f, m, **kwargs: write(f, m, "4", **kwargs, binary=True),
110-
"gmsh2-ascii": lambda f, m, **kwargs: write(f, m, "2", **kwargs, binary=False),
111-
"gmsh2-binary": lambda f, m, **kwargs: write(f, m, "2", **kwargs, binary=True),
112-
"gmsh4-ascii": lambda f, m, **kwargs: write(f, m, "4", **kwargs, binary=False),
113-
"gmsh4-binary": lambda f, m, **kwargs: write(f, m, "4", **kwargs, binary=True),
110+
"gmsh22": lambda f, m, **kwargs: write(f, m, "2.2", **kwargs),
111+
"gmsh": lambda f, m, **kwargs: write(f, m, "4.1", **kwargs),
114112
},
115113
)

0 commit comments

Comments
 (0)