From aaa78007aff7f921a6838eac3b419ea26966de79 Mon Sep 17 00:00:00 2001 From: Mathis Hocke <mathis.hocke@fu-berlin.de> Date: Tue, 15 Mar 2022 14:22:56 +0100 Subject: [PATCH] Add full_attrs option to stringify --- src/robofish/io/app.py | 8 +++++++- src/robofish/io/file.py | 30 ++++++++++++++++++++---------- tests/robofish/io/test_app_io.py | 9 ++++++--- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/robofish/io/app.py b/src/robofish/io/app.py index 8c9229a..be1690d 100644 --- a/src/robofish/io/app.py +++ b/src/robofish/io/app.py @@ -35,12 +35,18 @@ def print_file(args=None): default="shape", help="Choose how datasets are printed, either the shapes or the full content is printed", ) + parser.add_argument( + "--full_attrs", + default=False, + action="store_true", + help="Show full unabbreviated values for attributes", + ) if args is None: args = parser.parse_args() with robofish.io.File(path=args.path, strict_validate=False) as f: - print(f.to_string(args.output_format)) + print(f.to_string(output_format=args.output_format, full_attrs=args.full_attrs)) print() valid = f.validate(strict_validate=False)[0] print("Valid file" if valid else "Invalid file") diff --git a/src/robofish/io/file.py b/src/robofish/io/file.py index c523840..1bf1ed1 100644 --- a/src/robofish/io/file.py +++ b/src/robofish/io/file.py @@ -627,10 +627,17 @@ class File(h5py.File): """ return robofish.io.validate(self, strict_validate) - def to_string(self, output_format: str = "shape", max_width: int = 120) -> str: + def to_string( + self, + output_format: str = "shape", + max_width: int = 120, + full_attrs: bool = False, + ) -> str: """The file is formatted to a human readable format. Args: output_format: ['shape', 'full'] show the shape, or the full content of datasets + max_width: set the width in characters after which attribute values get abbreviated + full_attrs: do not abbreviate attribute values if True Returns: A human readable string, representing the file """ @@ -696,11 +703,13 @@ class File(h5py.File): index = 0 if obj.attrs: for key, value in obj.attrs.items(): - value = str(value).replace("\n", " ").strip() - if len(value) > max_width - max_key_len - len(lines()): - value = ( - value[: max_width - max_key_len - len(lines()) - 3] + "..." - ) + if not full_attrs: + value = str(value).replace("\n", " ").strip() + if len(value) > max_width - max_key_len - len(lines()): + value = ( + value[: max_width - max_key_len - len(lines()) - 3] + + "..." + ) s += f"{lines()}{key: <{max_key_len}} {value}\n" index += 1 if hasattr(obj, "items"): @@ -728,10 +737,11 @@ class File(h5py.File): if len(d_value) > max_width - d_max_key_len - len( lines(True) ): - d_value = d_value[ - : max_width - d_max_key_len - len(lines(True)) - ] - d_value = d_value[:-3] + "..." + if not full_attrs: + d_value = d_value[ + : max_width - d_max_key_len - len(lines(True)) + ] + d_value = d_value[:-3] + "..." s += f"{lines(True)}{d_key: <{d_max_key_len}} {d_value}\n" if isinstance(value, h5py.Group): s += f"{lines()}{key}\n" + recursive_stringify( diff --git a/tests/robofish/io/test_app_io.py b/tests/robofish/io/test_app_io.py index e8809a6..ea4933f 100644 --- a/tests/robofish/io/test_app_io.py +++ b/tests/robofish/io/test_app_io.py @@ -30,9 +30,12 @@ def test_app_print(): """This tests the function of the robofish-io-validate command""" class DummyArgs: - def __init__(self, path, output_format): + def __init__(self, path, output_format, full_attrs): self.path = path self.output_format = output_format + self.full_attrs = full_attrs - app.print_file(DummyArgs(h5py_file, "full")) - app.print_file(DummyArgs(h5py_file, "shape")) + app.print_file(DummyArgs(h5py_file, "full", True)) + app.print_file(DummyArgs(h5py_file, "shape", True)) + app.print_file(DummyArgs(h5py_file, "full", False)) + app.print_file(DummyArgs(h5py_file, "shape", False)) -- GitLab