def xlsx_xlrd(fp):
    """Read and convert a xlsx file to JSON format using the xlrd library
    :param fp: File pointer object
    :return: tuple of table headers and data
    """

    max_size = config['max_size']

    wb = xlrd.open_workbook(fp.name)

    # Currently only displays the first sheet if there are more than one.
    sheet = wb.sheets()[0]

    if sheet.ncols > max_size or sheet.nrows > max_size:
        raise TableTooBigException("Table is too large to render.")

    if sheet.ncols < 1 or sheet.nrows < 1:
        raise EmptyTableException("Table is empty or corrupt.")

    fields = sheet.row_values(0) if sheet.nrows else []

    data = [dict(zip(fields, sheet.row_values(row_index)))
            for row_index in range(1, sheet.nrows)]

    header = header_population(fields)

    return header, data
def data_from_dataframe(dataframe):
    """Convert a dataframe object to a list of dictionaries
    :param fp: File pointer object
    :return: tuple of table headers and data
    """

    fields = dataframe.keys()
    header = header_population(fields)
    data = [data.to_dict() for _, data in dataframe.iterrows()]

    return header, data
def ods_ezodf(fp):
    """Read and convert a ods file to JSON format using the ezodf library
    :param fp: File pointer object
    :return: tuple of table headers and data
    """

    workbook = ezodf.opendoc(fp.name)
    sheet = workbook.sheets[0]

    list_data = [[cell.value for cell in row] for row in sheet.rows()]

    header = header_population(list_data[0])
    data = data_population(list_data)

    return header, data
def csv_csv(fp):
    """Read and convert a csv file to JSON format using the csv library
    :param fp: File pointer object
    :return: tuple of table headers and data
    """

    dialect = csv.Sniffer().sniff((fp).read(1024))
    fp.seek(0)
    reader = csv.reader(fp, dialect)
    complete_data = [row for row in reader]

    # Assume that the first line is the header, other lines are data
    header = header_population(complete_data[0])
    data = data_population(complete_data[1:], complete_data[0])

    return header, data
def test_header_population_returns_list_of_dicts():
    fake_headers = ['one', 'two', 'three']
    populated = utilities.header_population(fake_headers)
    assert type(populated) == list
    assert type(populated[0]) == dict
    assert len(populated) == 3