diff --git a/src/robofish/io/app.py b/src/robofish/io/app.py index 8c9229a67591d1061fb6db0c44144c2c76851c7f..be1690d62049354a292a0d06627affd2c410d397 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 c523840881fed70947183654948bcdf178b3d54a..1bf1ed173f0a25f84963a9f7e90f477d2c7e44e7 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 e8809a63bceffa3fbce68ba5f19019a92f8ac9ae..ea4933f28ae92e86b0bbc0f9e75b0890ae5fba3b 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))