def main(argv):
    """
    Executes an action on the database

    Required actions:
        clear database

        add
            add new song
            add batch of songs from folder

    argv:

    param 0:
        action name

    # EXAMPLES:
    # python database.py clear songs

    # python database.py add ~/Music/Dubstep/somesong.wav dubstep
    # python database.py add ~/Music/Dubstep/ -1 dubstep
    #
    # Multi genre:
    # python database.py add ~/Music/Dubstep/somesong.wav dubstep dnb progressive
    #
    :param argv:
    :return:

    """
    # logging.basicConfig(level=logging.DEBUG)
    logs.enable_default_log()

    if len(argv) < 1:
        print "Required parameter action"
        print
        return

    supported_ext = [".wav"]
    action = argv[0]

    if action == "add":
        abs_path = os.path.abspath(argv[1])
        if os.path.isfile(abs_path):
            if len(argv) < 3:
                print "Required parameters path genre0 genre1 ... genre n"
                print

            filename, extension = os.path.splitext(abs_path)
            if extension in supported_ext:
                # file is valid and we can add it to the database
                genres = argv[2:]
                db_control = DatabaseService()
                db_control.add_song(abs_path, genres)
                print "Song added for genres " + ', '.join(genres) + " at:"
                print abs_path
            else:
                print extension + " is not a supported file type."
        elif os.path.isdir(abs_path):

            if len(argv) < 4:
                print "Required parameters path maxcount genre0 genre1 ... genre n"
                print

            all_files = os.listdir(abs_path)
            max_count = int(argv[2])
            genres = argv[3:]

            all_wavs = []
            for file in all_files:
                filename, extension = os.path.splitext(file)
                if extension in supported_ext:
                    # rename the file to remove special characters
                    oldpath = abs_path + '/' + filename + extension
                    newpath = abs_path + '/' + re.sub('[^\w\.\-]+', ' ', filename) + extension
                    os.rename(oldpath, newpath)
                    all_wavs.append(newpath)

            count = 0
            db_control = DatabaseService()

            for wav in all_wavs:
                if max_count == -1 or count < max_count: # -1 means add all files
                    abswav = os.path.join(abs_path, wav)
                    db_control.add_song(abswav, genres)
                count += 1

            print "FINISHED adding " + str(count) + " songs for genre(s) " + ', '.join(genres)
    elif action == "clear":
        if len(argv) < 2:
            print "required param type for clear. Types: 'songs', 'features'"
            return

        db_control = DatabaseService()

        ctype = argv[1]
        if ctype == "songs":
            db_control.delete_all_songs()
        elif ctype == "features":
            db_control.delete_all_features()
        else:
            db_control.delete_song(ctype)

    return
    training_set = db_service.get_training_set(genres, required_features, songs_per_genre, verbose=True)

    cross_val = CrossValidation(training_set, k_folds=6)

    average_hit_rate = 0
    count = 0

    validator = Validator()
    for ts, vs in cross_val:
        learner = LearnerSVC()
        learner.fit(ts)
        results = validator.validate_next(learner, vs)

        average_hit_rate += results.hit_rate()
        count += 1.0
        print results

    print "Total samples: " + str(training_set.total_samples())
    print "Training/Validation: " + str(cross_val.training_size()) + " / " + str(cross_val.validation_size())
    print "Average hit rate: %" + "%.2f" % (100 * average_hit_rate / count)



if __name__ == "__main__":
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        logs.enable_default_log()
        main(sys.argv[1:])
        # create_trained_model()