How to use the cookiecutter.generate.generate_context function in cookiecutter

To help you get started, we’ve selected a few cookiecutter examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cookiecutter / cookiecutter / tests / test_generate_context.py View on Github external
def test_choices(context_file, default_context, extra_context):
    """Make sure that the default for list variables is based on the user
    config and the list as such is not changed to a single value.
    """
    expected_context = {
        'choices_template': OrderedDict([
            ('full_name', 'Raphael Pierzina'),
            ('github_username', 'hackebrot'),
            ('project_name', 'Kivy Project'),
            ('repo_name', '{{cookiecutter.project_name|lower}}'),
            ('orientation', ['landscape', 'all', 'portrait']),
        ])
    }

    generated_context = generate.generate_context(
        context_file, default_context, extra_context
    )

    assert generated_context == expected_context
github cookiecutter / cookiecutter / tests / test_generate_context.py View on Github external
def test_generate_context(input_params, expected_context):
    """
    Test the generated context for several input parameters against the
    according expected context.
    """
    assert generate.generate_context(**input_params) == expected_context
github microsoft / PTVS / Python / Product / Cookiecutter / cookiecutter_load.py View on Github external
def main():
    repo_dir = sys.argv[1]
    user_config_path = sys.argv[2]
    user_config_path = user_config_path if os.path.isfile(user_config_path) else None
    context_file = os.path.join(repo_dir, 'cookiecutter.json')
    config_dict = get_user_config(user_config_path)
    context = generate_context(context_file, config_dict['default_context'])
    print(json.dumps(context))
github cookiecutter / cookiecutter / cookiecutter / main.py View on Github external
# TODO: find a better way to tell if it's a repo URL
    if "git@" in input_dir or "https://" in input_dir:
        repo_dir = clone(
            repo_url=input_dir,
            checkout=checkout,
            clone_to_dir=config_dict['cookiecutters_dir']
        )
    else:
        # If it's a local repo, no need to clone or copy to your cookiecutters_dir
        repo_dir = input_dir

    context_file = os.path.join(repo_dir, 'cookiecutter.json')
    logging.debug('context_file is {0}'.format(context_file))

    context = generate_context(
        context_file=context_file,
        default_context=config_dict['default_context']
    )

    # prompt the user to manually configure at the command line.
    # except when 'no-input' flag is set
    if not no_input:
        cookiecutter_dict = prompt_for_config(context)
        context['cookiecutter'] = cookiecutter_dict

    # Create project from local context and project template.
    generate_files(
        repo_dir=repo_dir,
        context=context
    )
github timothycrosley / cruft / cruft / api.py View on Github external
raise InvalidCookiecutterRepository(e)

        main_cookiecutter_directory: Optional[Path] = None
        for dir_item in cookiecutter_template_dir.glob("*cookiecutter.*"):
            if dir_item.is_dir() and "{{" in dir_item.name and "}}" in dir_item.name:
                main_cookiecutter_directory = dir_item
                break

        if not main_cookiecutter_directory:  # pragma: no cover
            raise UnableToFindCookiecutterTemplate(cookiecutter_template_dir)

        context_file = cookiecutter_template_dir / "cookiecutter.json"

        config_dict = get_user_config(config_file=config_file, default_config=default_config)

        context = generate_context(
            context_file=context_file,
            default_context=config_dict["default_context"],
            extra_context=extra_context,
        )

        # prompt the user to manually configure at the command line.
        # except when 'no-input' flag is set
        context["cookiecutter"] = prompt_for_config(context, no_input)
        context["cookiecutter"]["_template"] = template_git_url

        if use_latest or no_input:
            use_commit = last_commit
        else:  # pragma: no cover
            print("")
            print(f"The latest commit to the template is {last_commit}")
            print("Press enter to link against this commit or provide an alternative commit.")
github timothycrosley / cruft / cruft / api.py View on Github external
def _generate_output(
    context_file: str,
    cruft_state: dict,
    cookiecutter_input: bool,
    template_dir: str,
    output_dir: str,
) -> dict:
    context = generate_context(
        context_file=context_file, extra_context=cruft_state["context"]["cookiecutter"]
    )
    context["cookiecutter"] = prompt_for_config(context, not cookiecutter_input)
    context["cookiecutter"]["_template"] = cruft_state["template"]

    generate_files(
        repo_dir=template_dir, context=context, overwrite_if_exists=True, output_dir=output_dir
    )
    return context
github cookiecutter / cookiecutter / cookiecutter / main.py View on Github external
abbreviations=config_dict['abbreviations'],
        clone_to_dir=config_dict['cookiecutters_dir'],
        checkout=checkout,
        no_input=no_input,
        password=password
    )

    template_name = os.path.basename(os.path.abspath(repo_dir))

    if replay:
        context = load(config_dict['replay_dir'], template_name)
    else:
        context_file = os.path.join(repo_dir, 'cookiecutter.json')
        logger.debug('context_file is {}'.format(context_file))

        context = generate_context(
            context_file=context_file,
            default_context=config_dict['default_context'],
            extra_context=extra_context,
        )

        # prompt the user to manually configure at the command line.
        # except when 'no-input' flag is set
        context['cookiecutter'] = prompt_for_config(context, no_input)

        # include template dir or url in the context dict
        context['cookiecutter']['_template'] = template

        dump(config_dict['replay_dir'], template_name, context)

    # Create project from local context and project template.
    result = generate_files(
github praekeltfoundation / molo / molo / core / cookiecutter.py View on Github external
if 'git@' in template or 'https://' in template:
        repo_dir = clone(
            repo_url=template,
            checkout=checkout,
            clone_to_dir=config_dict['cookiecutters_dir'],
            no_input=no_input
        )
    else:
        # If it's a local repo, no need to clone or copy to your
        # cookiecutters_dir
        repo_dir = template

    context_file = os.path.join(repo_dir, 'cookiecutter.json')
    logging.debug('context_file is {0}'.format(context_file))

    context = generate_context(
        context_file=context_file,
        default_context=config_dict['default_context'],
        extra_context=extra_context,
    )

    # Create project from local context and project template.
    generate_files(
        repo_dir=repo_dir,
        context=context
    )