Exemplo n.º 1
0
def format_tabular_list(f_out, columns, rows, options=None):
    """Format a list in a pretty grid format.

    This method will format and write a list of rows in a grid or CSV list.

    f_out[in]          file to print to (e.g. sys.stdout)
    columns[in]        list of column names
    rows[in]           list of rows to print
    options[in]        options controlling list:
        print_header   if False, do not print header
        separator      if set, use the char specified for a CSV output
        quiet          if True, do not print the grid text (no borders)
        print_footer   if False, do not print footer
        none_to_null   if True converts None values to NULL
    """
    if options is None:
        options = {}
    print_header = options.get("print_header", True)
    separator = options.get("separator", None)
    quiet = options.get("quiet", False)
    print_footer = options.get("print_footer", True)
    none_to_null = options.get("none_to_null", False)
    convert_to_sql = options.get('to_sql', False)

    # do nothing if no rows.
    if len(rows) == 0:
        return
    if separator is not None:
        if os.name == "posix":
            # Use \n as line terminator in POSIX (non-Windows) systems.
            csv_writer = csv.writer(f_out, delimiter=separator,
                                    lineterminator='\n')
        else:
            # Use the default line terminator '\r\n' on Windows.
            csv_writer = csv.writer(f_out, delimiter=separator)
        if print_header:
            csv_writer.writerow(columns)
        for row in rows:
            if convert_to_sql:
                # Convert value to SQL (i.e. add quotes if needed).
                row = ['NULL' if col is None else to_sql(col) for col in row]
            if none_to_null:
                # Convert None values to 'NULL'
                row = ['NULL' if val is None else val for val in row]
            csv_writer.writerow(row)
    else:
        # Calculate column width for each column
        col_widths = []
        for col in columns:
            size = len(col)
            col_widths.append(size + 1)

        stop = len(columns)
        for row in rows:
            # if there is one column, just use row.
            if stop == 1:
                col_size = len(str(row[0])) + 1
                if col_size > col_widths[0]:
                    col_widths[0] = col_size
            else:
                for i in range(0, stop):
                    col_size = len(str(row[i])) + 1
                    if col_size > col_widths[i]:
                        col_widths[i] = col_size

        # print header
        if print_header:
            _format_col_separator(f_out, columns, col_widths, quiet)
            _format_row_separator(f_out, columns, col_widths, columns, quiet)
        _format_col_separator(f_out, columns, col_widths, quiet)
        for row in rows:
            # Note: lists need to be converted to tuple as expected by
            # next method (to handle single column rows correctly)
            if convert_to_sql:
                # Convert value to SQL (i.e. add quotes if needed).
                row = tuple(('NULL' if col is None else to_sql(col)
                             for col in row))
            if none_to_null:
                # Convert None values to 'NULL'
                row = tuple(('NULL' if val is None else val for val in row))
            _format_row_separator(f_out, columns, col_widths, row, quiet)
        if print_footer:
            _format_col_separator(f_out, columns, col_widths, quiet)
Exemplo n.º 2
0
def format_tabular_list(f_out, columns, rows, options=None):
    """Format a list in a pretty grid format.

    This method will format and write a list of rows in a grid or CSV list.

    f_out[in]          file to print to (e.g. sys.stdout)
    columns[in]        list of column names
    rows[in]           list of rows to print
    options[in]        options controlling list:
        print_header   if False, do not print header
        separator      if set, use the char specified for a CSV output
        quiet          if True, do not print the grid text (no borders)
        print_footer   if False, do not print footer
        none_to_null   if True converts None values to NULL
    """
    if options is None:
        options = {}
    print_header = options.get("print_header", True)
    separator = options.get("separator", None)
    quiet = options.get("quiet", False)
    print_footer = options.get("print_footer", True)
    none_to_null = options.get("none_to_null", False)
    convert_to_sql = options.get('to_sql', False)

    # do nothing if no rows.
    if len(rows) == 0:
        return
    if separator is not None:
        if os.name == "posix":
            # Use \n as line terminator in POSIX (non-Windows) systems.
            csv_writer = UnicodeWriter(f_out,
                                       delimiter=separator,
                                       lineterminator='\n')
        else:
            # Use the default line terminator '\r\n' on Windows.
            csv_writer = UnicodeWriter(f_out, delimiter=separator)
        if print_header:
            csv_writer.writerow(columns)
        for row in rows:
            row = [
                val.encode("utf-8") if isinstance(val, unicode) else val
                for val in row
            ]
            if convert_to_sql:
                # Convert value to SQL (i.e. add quotes if needed).
                row = ['NULL' if col is None else to_sql(col) for col in row]
            if none_to_null:
                # Convert None values to 'NULL'
                row = ['NULL' if val is None else val for val in row]
            csv_writer.writerow(row)
    else:
        # Calculate column width for each column
        col_widths = options.get('col_widths', None)
        if not col_widths:
            col_widths = get_col_widths(columns, rows)

        # print header
        if print_header:
            _format_col_separator(f_out, columns, col_widths, quiet)
            _format_row_separator(f_out, columns, col_widths, columns, quiet)
        _format_col_separator(f_out, columns, col_widths, quiet)
        for row in rows:
            # Note: lists need to be converted to tuple as expected by
            # next method (to handle single column rows correctly)
            if convert_to_sql:
                # Convert value to SQL (i.e. add quotes if needed).
                row = tuple(
                    ('NULL' if col is None else to_sql(col) for col in row))
            if none_to_null:
                # Convert None values to 'NULL'
                row = tuple(('NULL' if val is None else val for val in row))
            _format_row_separator(f_out, columns, col_widths, row, quiet)
        if print_footer:
            _format_col_separator(f_out, columns, col_widths, quiet)
Exemplo n.º 3
0
def format_tabular_list(f_out, columns, rows, options=None):
    """Format a list in a pretty grid format.

    This method will format and write a list of rows in a grid or CSV list.

    f_out[in]          file to print to (e.g. sys.stdout)
    columns[in]        list of column names
    rows[in]           list of rows to print
    options[in]        options controlling list:
        print_header   if False, do not print header
        separator      if set, use the char specified for a CSV output
        quiet          if True, do not print the grid text (no borders)
        print_footer   if False, do not print footer
        none_to_null   if True converts None values to NULL
    """
    if options is None:
        options = {}
    print_header = options.get("print_header", True)
    separator = options.get("separator", None)
    quiet = options.get("quiet", False)
    print_footer = options.get("print_footer", True)
    none_to_null = options.get("none_to_null", False)
    convert_to_sql = options.get('to_sql', False)

    # do nothing if no rows.
    if len(rows) == 0:
        return
    if separator is not None:
        if os.name == "posix":
            csv_writer = csv.writer(f_out, delimiter=separator)
        else:
            csv_writer = csv.writer(f_out,
                                    delimiter=separator,
                                    lineterminator='\n')
        if print_header:
            csv_writer.writerow(columns)
        for row in rows:
            if convert_to_sql:
                # Convert value to SQL (i.e. add quotes if needed).
                row = ['NULL' if col is None else to_sql(col) for col in row]
            if none_to_null:
                # Convert None values to 'NULL'
                row = ['NULL' if val is None else val for val in row]
            csv_writer.writerow(row)
    else:
        # Calculate column width for each column
        col_widths = []
        for col in columns:
            size = len(col)
            col_widths.append(size + 1)

        stop = len(columns)
        for row in rows:
            # if there is one column, just use row.
            if stop == 1:
                col_size = len(str(row[0])) + 1
                if col_size > col_widths[0]:
                    col_widths[0] = col_size
            else:
                for i in range(0, stop):
                    col_size = len(str(row[i])) + 1
                    if col_size > col_widths[i]:
                        col_widths[i] = col_size

        # print header
        if print_header:
            _format_col_separator(f_out, columns, col_widths, quiet)
            _format_row_separator(f_out, columns, col_widths, columns, quiet)
        _format_col_separator(f_out, columns, col_widths, quiet)
        for row in rows:
            # Note: lists need to be converted to tuple as expected by
            # next method (to handle single column rows correctly)
            if convert_to_sql:
                # Convert value to SQL (i.e. add quotes if needed).
                row = tuple(
                    ('NULL' if col is None else to_sql(col) for col in row))
            if none_to_null:
                # Convert None values to 'NULL'
                row = tuple(('NULL' if val is None else val for val in row))
            _format_row_separator(f_out, columns, col_widths, row, quiet)
        if print_footer:
            _format_col_separator(f_out, columns, col_widths, quiet)