import pandas
import argparse
import SQLConnector
import Constants

def get_excel(filepath):
    """
    Returns a Pandas datafile that contains the contents of a Microsoft Excel
    Spreadsheet

    Params:
        filepath - A string containing the path to the file

    Returns:
        A Pandas datafile
    """
    return pandas.read_excel(filepath)

def get_column_names(datafile):
    """
    Returns a list containing the column names of a Pandas datafile as Python
    strings

    Params:
        datafile - A Pandas datafile

    Returns:
        A list of strings
    """
    return [ str(s).replace(" ", "_") for s in datafile.columns ]

def is_table_set_up():
    """
    Returns True if this project's MySQL table is set up, False otherwise
    """
    query = "SELECT table_name FROM information_schema.tables WHERE table_schema='%s' and table_name='%s';"
    query = query % (Constants.PROJECT_DB_NAME, Constants.PROJECT_TABLE_NAME)
    retval = SQLConnector.execute(query)
    return retval != None and len(retval) == 1

def create_project_table(column_names):
    """
    Sets up MySQL with a table with a set schema given a list of column_names.
    Does nothing if the table is already set up.

    Params:
        column_names - A list of strings containing column names

    Returns:
        None
    """
    # Tons of format strings!
    query = "CREATE TABLE IF NOT EXISTS %s (%s);"
    schema = (("%s VARCHAR(64), " * len(column_names))[:-2]) % tuple(column_names)
    query = query % (Constants.PROJECT_TABLE_NAME, schema)
    SQLConnector.execute(query)

def delete_project_table():
    """
    Deletes the table in MySQL that this project uses. Will do nothing if the
    table does not yet exist.
    """
    query = "DROP TABLE IF EXISTS %s;" % (Constants.PROJECT_TABLE_NAME,)
    SQLConnector.execute(query)

def reset_project_table():
    """
    Resets the table for this project by calling 'delete' and 'setup'
    """
    delete_project_table()
    create_project_table()

def load_excel_file(datafile):
    """
    Takes a Pandas datafile and inserts the data into the project's MySQL table.
    If the project's table is not yet created, this function will call 'create'.
    """
    if not is_table_set_up():
        create_project_table()
    # TODO: Fill in this part

def main():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-c",
        "--create",
        help="Creates the table",
        action="store_true"
    )

    parser.add_argument(
        "-d",
        "--delete",
        help="Deletes the table",
        action="store_true"
    )

    parser.add_argument(
        "-r",
        "--reset",
        help="Resets the table",
        action="store_true"
    )

    parser.add_argument(
        "-l",
        "--load",
        help="Load an Excel spreadsheet into the MySQL table",
        action="append"
    )

    parser.add_argument(
        "-t",
        "--test",
        help="Prints True if the table is set up, False otherwise",
        action="store_true"
    )

    args = parser.parse_args()

    if args.create:
        df = get_excel("../sample_data/grad_req.xlsx")
        create_project_table(get_column_names(df))
    elif args.delete:
        delete_project_table()
    elif args.reset:
        reset_project_table()

    if args.test:
        print(is_table_set_up())

    if args.load == None:
        return
    else:
        for f in args.load:
            load_excel_file(f)

if __name__ == "__main__":
    main()


@@ -2207,24 +2207,70 @@
 e'.%0A    %22%22%22%0A
+    column_names = get_column_names(datafile)%0A
     if not i
@@ -2320,39 +2320,760 @@
 ble(
-)%0A    # TODO: Fill in this part
+column_names)%0A    rows, cols = datafile.shape%0A    counter = 0%0A    print %22Inserting %25d rows with %25d data fields each%22 %25 (rows, cols)%0A    for row in datafile.itertuples():%0A        if counter %25 50 == 0:%0A            print %22Progress: %25d of %25d%22 %25 (counter, rows)%0A        data = list(row._asdict().values())%5B1:%5D%0A        assert(len(data) == len(column_names))%0A        # Generate query%0A        query = %22INSERT INTO %25s (%25s) VALUES (%25s);%22%0A        schema = ((%22%25s , %22 * len(column_names))%5B:-2%5D) %25 tuple(column_names)%0A        values = ((%22'%25s' , %22 * len(data))%5B:-2%5D) %25 tuple(( str(it) for it in data))%0A        query = query %25 (Constants.PROJECT_TABLE_NAME, schema, values)%0A        print query%0A        SQLConnector.execute(query)%0A        counter += 1%0A    print %22Done!%22
 %0A%0Ade
@@ -4237,17 +4237,28 @@
 el_file(
-f
+get_excel(f)
 )%0A%0Aif __

Add data insertion method