def print_single_attribute()

in lib/ramble/ramble/cmd/common/info.py [0:0]


def print_single_attribute(obj, attr, verbose=False, pattern="*", format=supported_formats.text):
    """Handle printing a single attribute

    For a given object, print a single attribute of this based on the given
    format specification and filter pattern.
    """
    internal_attr_name = _map_attr_name(attr)
    internal_attr = getattr(obj, internal_attr_name, None)
    if attr == "registered_phases":
        _print_phases(obj, attr, verbose, pattern, format=format)
        return

    print_attribute_header(attr, verbose)

    indentation = " " * 4

    # If we are not printing verbose output, we suppress most of the output.
    if not verbose:
        # If the attribute is a dictionary, convert the keys to a list, and
        # print them
        # Otherwise, we print the attribute's value directly.
        if isinstance(internal_attr, dict):
            to_print = list(internal_attr.keys())
        else:
            to_print = internal_attr

        # If we are trying to print a list, filter it and print using the
        # format specification
        # Otherwise, print it as a raw string.
        if isinstance(to_print, list):
            to_print = fnmatch.filter(to_print, pattern)
            if format == supported_formats.lists:
                color.cprint("    " + str(list(to_print)))
            elif format == supported_formats.text:
                color.cprint(colified(to_print, tty=True, indent=4))
        else:
            color.cprint(f"    {str(to_print)}\n")
    else:
        # With verbose output, and a dict attribute, we try to print all of the
        # sub items. These need to be iterated over, and we need to escape
        # existing characters that would normally be used to color strings.
        if isinstance(internal_attr, dict):
            for name, val in internal_attr.items():
                if pattern and not fnmatch.fnmatch(name, pattern):
                    continue

                if isinstance(val, dict):
                    color_name = ramble.util.colors.section_title(name)
                    color.cprint(f"{color_name}:")
                    for sub_name, sub_val in val.items():
                        # Avoid showing duplicate names for variables
                        if isinstance(sub_val, WorkloadVariable) and sub_name == sub_val.name:
                            to_print = f"{indentation}{sub_val}"
                        else:
                            color_sub_name = ramble.util.colors.nested_1(sub_name)
                            to_print = f"{indentation}{color_sub_name}: {sub_val}"
                        try:
                            color.cprint(to_print)
                        except color.ColorParseError:
                            escaped_sub_val = sub_val.replace("@", "@@")
                            color.cprint(f"{indentation}{color_sub_name}: {escaped_sub_val}")
                    color.cprint("")
                else:
                    color.cprint(f"{str(val)}")
        # If the attribute is not a dict, print using the existing format rules.
        elif isinstance(internal_attr, list):
            to_print = fnmatch.filter(internal_attr, pattern)
            if format == supported_formats.lists:
                color.cprint("    " + str(list(to_print)))
            elif format == supported_formats.text:
                color.cprint(colified(to_print, tty=True, indent=4))
            color.cprint("")
        else:
            color.cprint(f"{indentation}" + str(internal_attr))