def main_menu(all_bmmls):
    while True:
        choice = utils.prompt_choices([
            "Show me new files one at a time, and ask me what to do with them.",
            "Show me a menu of all the files and let me pick which to edit.",
            "Save + Quit. We're done here."
            ])

        if choice == 0:
            ask_new_files(all_bmmls)
        elif choice == 1:
            choose_files(all_bmmls)
        elif choice == 2:
            return
def main():
    project = utils.find_projectjson()

    bmml_path = project['bmml_path']

    # Add a "forms" component to the project if not exists
    if "forms" not in project:
        project['forms'] = []

    # Build a list of all bmml files
    print("Scanning %s for .bmml files" % bmml_path)
    all_bmmls = []
    for dirpath, dirnames, filenames in os.walk(bmml_path):
        for filename in filenames:
            if filename.endswith(".bmml"):
                all_bmmls.append(
                        BMML(
                            os.path.join(dirpath, filename)
                            )
                        )

    # Find and build Form objects for all forms found in in bmml files
    bmml_forms = []
    for bmml_file in all_bmmls:
        forms = bmml_file.forms
        if len(forms) != 0:
            bmml_forms.extend(forms)

    choice = 0
    while(choice != 3 and choice != 4):
        print("\nFound %s possible forms within %s bmml files! How do you want to proceed?" % (len(bmml_forms), len(all_bmmls)))
        choice = utils.prompt_choices([
            "Show me the currently configured forms.",
            "Delete a currently configured forms.",
            "Show me auto-detected forms, and ask me what to do with them.",
            "Save + Quit.  We're done here.",
            "Quit.  Throwing away changes.",
            ])
        print()
    
        if choice == 0:
            display_forms(project)
        elif choice == 1:
            delete_a_form(project)
        elif choice == 2:
            ask_autodetected_forms(project, bmml_forms)
        elif choice == 3:
            utils.save_projectjson(project)
def choose_files(all_bmmls):
    # Build the menu
    options = []
    for b in all_bmmls:
        options.append(
                "%s\n%s\t%s" % (
                    b.path, 
                    b.data['method_types'] if 'method_types' in b.data else "(undef)",
                    b.data['return_types'] if 'return_types' in b.data else "(undef)",
                    )
                )

    print()
    print("Choose which BMML file you want to edit")
    choice = utils.prompt_choices(options)
    edit_bmml(all_bmmls[choice])
def choose_files(all_bmmls):
    # Build the menu
    options = []
    for b in all_bmmls:
        options.append(
                "%s\n%s\tr'%s'" % (
                    b.path, 
                    b.data['name'] if 'name' in b.data else "(needs a name)",
                    b.data['regex'] if 'regex' in b.data else "(needs a url)",
                    )
                )

    print()
    print("Choose which BMML file you want to edit")
    choice = utils.prompt_choices(options)
    edit_bmml(all_bmmls[choice])
def delete_a_form(project):
    if not project['forms']:
        print("0 forms currently configured\n")
        return
    print("%s forms currently configured" % len(project['forms']))
    print("Delete a form by choosing its number\n")
    choices = []
    for form in project['forms']:
        choices.append(form.name)
    choices[-1] = choices[-1] + '\n'
    choices.append("Don't Delete Anything\n")
    choice = utils.prompt_choices(choices)
    if choice == len(choices) - 1:
        return
    yn = utils.prompt_yn('Really Delete Form "%s"?' % project['forms'][choice].name, False)
    if yn:
        project['forms'].pop(choice)