def tidy(source_dir, dest_dir, tested_function_name):
    """
    Go through every .py file in the source_dir directory and call tidy_one on each.
    Write out the results to a file of the same name in the dest_dir directory.

    source_dir: string, path to folder with data to tidy
    dest_dir: string, path to folder to write tidy data to. Does not need
        to already exist.
    tested_function_name: the name of the function which will be tested. Used
        when tidying to remove debugging function calls.

    If STOP_ON_ERROR is set and an error is encountered while tidying, raises
    that error (and returns nothing).

    returns: list of solution ids (filenames without .py) that could not be tidied
    """
    ensure_folder_exists(dest_dir)

    skipped = []
    for filename in os.listdir(source_dir):
        # Skip non-python files
        if not filename.endswith('.py'):
            continue

        sol_id = filename.split('.')[0]
        print "Tidying", sol_id

        source_path = path.join(source_dir, filename)
        dest_path = path.join(dest_dir, filename)
        try:
            tidy_one(source_path, dest_path, tested_function_name)
        except:
            if STOP_ON_ERROR: raise
            skipped.append(sol_id)
    return skipped
def tidy(source_dir, dest_dir, tested_function_name):
    """
    Go through every .py file in the source_dir directory and call tidy_one on each.
    Write out the results to a file of the same name in the dest_dir directory.

    source_dir: string, path to folder with data to tidy
    dest_dir: string, path to folder to write tidy data to. Does not need
        to already exist.
    tested_function_name: the name of the function which will be tested. Used
        when tidying to remove debugging function calls.

    If STOP_ON_ERROR is set and an error is encountered while tidying, raises
    that error (and returns nothing).

    returns: list of solution ids (filenames without .py) that could not be tidied
    """
    ensure_folder_exists(dest_dir)

    skipped = []
    for filename in os.listdir(source_dir):
        # Skip non-python files
        if not filename.endswith('.py'):
            continue

        sol_id = filename.split('.')[0]
        print "Tidying", sol_id

        source_path = path.join(source_dir, filename)
        dest_path = path.join(dest_dir, filename)
        try:
            tidy_one(source_path, dest_path, tested_function_name)
        except:
            if STOP_ON_ERROR: raise
            skipped.append(sol_id)
    return skipped
def tidy_json(source_dir, json_name, tested_function_name):

    source_json = path.join(source_dir, json_name)
    before_path = path.join(source_dir, 'before.py')
    after_path = path.join(source_dir, 'after.py')

    skipped = []

    print 'opening json now'
    with open(source_json) as data_file:
        solutions = json.load(data_file)

    for id, sol in enumerate(solutions):
        if not sol['IsFixed']:
            print 'not fixed, skipping'
            continue  #if its not fixed, dont modify it
        for key in sol.keys():
            if key.startswith(
                    'py_') or key == 'before' or key == 'SynthesizedAfter':
                print "Tidying ", id, key
                #print sol[key]

                with open(before_path, 'w') as before_file:
                    before_file.write(sol[key])

                try:
                    tidy_one(before_path, after_path, tested_function_name)

                    with open(after_path) as after_file:
                        sol['tidy_' + key] = after_file.read()
                        print id, key, ' tidied'
                        #print sol['tidy_'+key]
                except:
                    if STOP_ON_ERROR: raise
                    skipped.append(str(id) + key)

    print 'writing back out to json now'
    with open(source_json, 'w') as data_file:
        json.dump(solutions, data_file, indent=4)

    return skipped
def tidy_json(source_dir, json_name, tested_function_name):

    source_json = path.join(source_dir, json_name)
    before_path = path.join(source_dir, 'before.py')
    after_path = path.join(source_dir, 'after.py')

    skipped = []

    print 'opening json now'
    with open(source_json) as data_file:
        solutions = json.load(data_file)

    for id,sol in enumerate(solutions):
        if not sol['IsFixed']:
            print 'not fixed, skipping'
            continue #if its not fixed, dont modify it
        for key in sol.keys():
            if key.startswith('py_') or key=='before' or key=='SynthesizedAfter':
                print "Tidying ",id,key
                #print sol[key]
        
                with open(before_path,'w') as before_file:
                    before_file.write(sol[key])

                try:
                    tidy_one(before_path, after_path, tested_function_name)
                    
                    with open(after_path) as after_file:
                        sol['tidy_'+key] = after_file.read()
                        print id,key, ' tidied'
                        #print sol['tidy_'+key]
                except:
                    if STOP_ON_ERROR: raise
                    skipped.append(str(id)+key)

    print 'writing back out to json now'
    with open(source_json,'w') as data_file:
        json.dump(solutions, data_file, indent=4)

    return skipped