def format_output(data, fmt='table'):  # pylint: disable=R0911,R0912
    """Given some data, will format it for console output.

    :param data: One of: String, Table, FormattedItem, List, Tuple,
                 SequentialOutput
    :param string fmt (optional): One of: table, raw, json, python
    """
    if isinstance(data, utils.string_types):
        if fmt in ('json', 'jsonraw'):
            return json.dumps(data)
        return data

    # responds to .prettytable()
    if hasattr(data, 'prettytable'):
        if fmt == 'table':
            return str(format_prettytable(data))
        elif fmt == 'raw':
            return str(format_no_tty(data))

    # responds to .to_python()
    if hasattr(data, 'to_python'):
        if fmt == 'json':
            return json.dumps(
                format_output(data, fmt='python'),
                indent=4,
                cls=CLIJSONEncoder)
        elif fmt == 'jsonraw':
            return json.dumps(format_output(data, fmt='python'),
                              cls=CLIJSONEncoder)
        elif fmt == 'python':
            return data.to_python()

    # responds to .formatted
    if hasattr(data, 'formatted'):
        if fmt == 'table':
            return data.formatted

    # responds to .separator
    if hasattr(data, 'separator'):
        output = [format_output(d, fmt=fmt) for d in data if d]
        return str(SequentialOutput(data.separator, output))

    # is iterable
    if isinstance(data, list) or isinstance(data, tuple):
        output = [format_output(d, fmt=fmt) for d in data]
        if fmt == 'python':
            return output
        return format_output(listing(output, separator=os.linesep))

    # fallback, convert this odd object to a string
    return data
def format_output ( data , fmt = 'table' ) : if isinstance ( data , utils . string_types ) : if fmt in ( 'json' , 'jsonraw' ) : return json . dumps ( data ) return data if hasattr ( data , 'prettytable' ) : if fmt == 'table' : return str ( format_prettytable ( data ) ) elif fmt == 'raw' : return str ( format_no_tty ( data ) ) if hasattr ( data , 'to_python' ) : if fmt == 'json' : return json . dumps ( format_output ( data , fmt = 'python' ) , indent = 4 , cls = CLIJSONEncoder ) elif fmt == 'jsonraw' : return json . dumps ( format_output ( data , fmt = 'python' ) , cls = CLIJSONEncoder ) elif fmt == 'python' : return data . to_python ( ) if hasattr ( data , 'formatted' ) : if fmt == 'table' : return data . formatted if hasattr ( data , 'separator' ) : output = [ format_output ( d , fmt = fmt ) for d in data if d ] return str ( SequentialOutput ( data . separator , output ) ) if isinstance ( data , list ) or isinstance ( data , tuple ) : output = [ format_output ( d , fmt = fmt ) for d in data ] if fmt == 'python' : return output return format_output ( listing ( output , separator = os . linesep ) ) return data
Given some data, will format it for console output.

    :param data: One of: String, Table, FormattedItem, List, Tuple,
                 SequentialOutput
    :param string fmt (optional): One of: table, raw, json, python