Esempio n. 1
0
def run_init() -> None:
    """CLI runner for init()"""
    try:
        init()
        write_msg(
            success_msg("Your application is ready!"),
            NewLine.both,
        )
        write_msg(
            "Run " + success_msg("pantam serve") + " to begin...\n",
            NewLine.both,
        )
    except CancelError:
        write_error(error_msg("Setup cancelled..."))
    except Exception as error:
        write_error(error_msg(str(error)))
Esempio n. 2
0
def run_action(action_file: str) -> None:
    """CLI runner for action()"""
    try:
        action(action_file)
        write_msg(success_msg("Your new action `%s` is ready!" % action_file))
    except Exception as error:
        write_error(error_msg(str(error)))
Esempio n. 3
0
def load_pantamrc_file() -> CliOptions:
    """Load pantamrc.json file"""
    cwd = Path(getcwd())
    try:
        config = open(cwd / ".pantamrc.json", "r")
        options: CliOptions = loads(config.read())
        return options
    except:
        write_error(
            error_msg("Cannot find .pantamrc.json file. Trying running `pantam init`")
        )
        sys.exit(1)
Esempio n. 4
0
def action(action_file: str) -> None:
    """Create action file"""
    clear()

    options = load_pantamrc_file()

    try:
        actions_folder = options["actions_folder"]
        write_msg(
            info_msg("Creating %s/%s file..." % (actions_folder, action_file)))
        cwd = Path(getcwd())
        create_file(
            cwd / actions_folder / action_file,
            action_template(make_class_name(action_file)),
        )
        write_msg(
            success_msg(" Done!"),
            NewLine.after,
        )
    except FileExistsError:
        write_msg(
            error_msg(" file exists, skipping"),
            NewLine.after,
        )
Esempio n. 5
0
def init() -> None:
    """Setup Pantam project"""
    clear()

    write_msg(welcome_msg())

    folder_name = path.basename(getcwd())

    index_file = create_prompt(name_index_file_msg(), "%s.py" % folder_name)
    actions_folder = create_prompt(name_actions_folder_msg(), "actions")
    do_create_action_files = create_prompt(create_actions_file_msg(False),
                                           True)

    action_files: List[str] = []

    while do_create_action_files:
        actions_file = create_prompt(
            name_actions_file_msg(),
            "index.py" if len(action_files) == 0 else "")
        if actions_file not in action_files:
            action_files.append(actions_file)
        do_create_action_files = create_prompt(create_actions_file_msg(True),
                                               False)

    action_files_map = list(
        map(lambda file_name: "|  |  %s" % file_name, action_files))
    action_files_flat = "\n".join(action_files_map)

    structure = """
| {index_file}
| {actions_folder}
{actions_files}""".format(
        index_file=index_file,
        actions_folder=actions_folder,
        actions_files=action_files_flat,
    )

    confirm = create_prompt(confirm_structure_msg(structure), True)

    if not confirm:
        raise CancelError("Cancelled!")

    write_msg(
        info_msg("Building your app! 🚀"),
        NewLine.both,
    )

    try:
        write_msg(info_msg("Creating %s file..." % index_file), )
        create_file(index_file, index_template())
        create_file("__init__.py", "")
        write_msg(success_msg(" done!"), NewLine.after)
    except FileExistsError:
        write_msg(error_msg(" file exists, skipping"), NewLine.after)

    try:
        write_msg(info_msg("Creating %s folder..." % actions_folder))
        create_folder(actions_folder)
        file_path = Path(getcwd())
        create_file(file_path / actions_folder / "__init__.py", "")
        write_msg(success_msg(" done!"), NewLine.after)
    except:
        write_msg(error_msg(" folder exists, skipping"), NewLine.after)

    for action_file in action_files:
        try:
            write_msg(
                info_msg("Creating %s/%s file..." %
                         (actions_folder, action_file)))
            file_path = Path(getcwd())
            create_file(
                file_path / actions_folder / action_file,
                action_template(make_class_name(action_file)),
            )
            write_msg(success_msg(" done!"), NewLine.after)
        except:
            write_msg(error_msg(" file exists, skipping"), NewLine.after)

    try:
        write_msg(info_msg("Creating .pantamrc.json file..."))
        create_pantamrc_file({
            "actions_folder": actions_folder,
            "entrypoint": index_file,
            "port": 5000,
            "dev_port": 5000,
        })
        write_msg(success_msg(" done!"), NewLine.after)
    except:
        write_msg(error_msg(" file exists, skipping"), NewLine.after)