import logging


def get_csv_files_from_index(index_file):
    """
    Read the csv filenames from an index file.
    Returns a list of pairs of filename and tablename.
    """

    results = []

    try:
        fh = open(index_file, 'r')
    except Exception as e:
        logging.info("Failed to open index file '%s': %s", index_file, e)
        return

    for lineno, line in enumerate(fh):

        # Skip the first line, its a useless version number.

        if lineno == 0:
            continue

        # Remove newlines at the end.
        line = line.rstrip('\r\n')

        # If the line contains a comma (,) then it has filename and tablename.

        fields = line.split(',')

        if len(fields) > 1:
            filename = fields[0]
            tablename = fields[1]

        else:
            tablename = fields[0]
            filename = fields[0] + ".csv"
            
        results.append( [ filename, tablename ] )

    fh.close()

    return results