Skip to content

types

Datatypes for templates.

Templates are a representation of source templates used to generate projects.

Bases: str, Enum

The locality of a template.

Template(
    repo: TemplateRepo,
    directory: str = "",
    _context: Optional[OrderedDict] = None,
)

A template is the combination of a template repository and a directory containing a cookiecutter.json file.

cached_path: Path

The path to the cached template.

context: dict

The context of the template.

context_file_path: Path

The path to the template’s context file.

directory: str = ''

The directory within the repository that contains the cookiecutter.json file.

name: str

The name of the template.

repo: TemplateRepo

The source of the template.

cleanup() -> None

Remove the cached template if it is a Zipfile.

Bases: str, Enum

The format of a template.

PLAIN = 'plain'

A plain directory that isn’t under version control.

TemplateRepo(
    source: str,
    cached_source: Path,
    format: TemplateFormat,
    locality: Locality,
    checkout: Optional[str] = None,
    password: Optional[str] = None,
)

A template repository is a source of one or more templates.

cached_source: Path

The path to the locally cached template.

checkout: Optional[str] = None

The branch, tag or commit for the template to track.

current_sha: Optional[str]

If the template is a git repository, return the current commit hash.

format: TemplateFormat

The format of the template.

latest_sha: Optional[str]

Return the latest SHA of this template’s repo.

If the template is not a git repository, it will always return None.

Returns:

Optional[str]

The latest hexsha of the template or None if the template isn’t a git repo

locality: Locality

Is the template local or remote?

password: Optional[str] = None

The password to use if template is a password-protected Zip archive.

source: str

The original path or URL to the template.

render_source(
    output_dir: Optional[Path] = None,
    commit: Optional[str] = None,
) -> Iterator[Path]

A context manager that provides the source from which to render the template.

For git repositories, this will create a temporary working tree of the repository and yield the path to the working tree.

For Zip archives, this will extract the archive to a temporary directory and yield the path

For plain repos, it will yield the path to the directory.

Parameters:

output_dir

The directory to extract the template to. If not provided, a temporary directory will be used.

TYPE: Optional[Path]

DEFAULT: None

commit

The commit to checkout if the template is a git repository.

TYPE: Optional[str]

DEFAULT: None

YIELDS DESCRIPTION
Path

The path to the rendered template

get_template_name(
    path_or_url: str,
    directory: Optional[str] = None,
    checkout: Optional[str] = None,
) -> str

Get the name of the template using the path or URL.

Parameters:

path_or_url

The URL or path to the template

TYPE: str

directory

Directory within a git repository template that holds the cookiecutter.json file.

TYPE: Optional[str]

DEFAULT: None

checkout

The branch, tag or commit to use if template is a git repository.

TYPE: Optional[str]

DEFAULT: None

Raises:

ValueError

If the path_or_url is not parsable

Returns:

str

The name of the template without extensions