Skip to content

templates

Management of templates.

Classes

InheritanceMap

Bases: ChainMap[str, TemplateFile]

Provides convenience functions for managing template inheritance.

Attributes

is_empty property
is_empty: bool

The context has only one mapping and it is empty.

Functions

inheritance
inheritance(key: str) -> list[TemplateFile]

Show all the values associated with a key, from most recent to least recent.

If the maps were added in the order {"a": Path("1")}, {"a": Path("2")}, {"a": Path("3")}, The output for inheritance("a") would be [Path("3"), Path("2"), Path("1")].

PARAMETER DESCRIPTION
key

The key to look up

TYPE: str

RETURNS DESCRIPTION
list[TemplateFile]

The values for that key with the last value first.

ProcessMode

Bases: IntFlag

Ways to process the template.

This is a bitmask, so you can combine them.

Attributes

ignore class-attribute instance-attribute
ignore = 0

Do not include the template in the inheritance map.

render class-attribute instance-attribute
render = auto()

Process the contents of the template using the template engine.

write class-attribute instance-attribute
write = auto()

Write the contents of the template to the output directory.

TemplateFile dataclass

TemplateFile(
    path: Path,
    process_mode: ProcessMode = ProcessMode.render
    | ProcessMode.write,
)

The template file data model.

Attributes

is_renderable property
is_renderable: bool

Is the template file renderable?

is_writable property
is_writable: bool

Is the template file writable?

path instance-attribute
path: Path

The full path to the template file.

process_mode class-attribute instance-attribute
process_mode: ProcessMode = render | write

How to process the template file.

Functions

catalog_inheritance

catalog_inheritance(
    template_info: Sequence[tuple[Path, ProcessModeFn]]
) -> InheritanceMap

Create an InheritanceMap that reflects the inheritance of all the template paths.

catalog_templates

catalog_templates(
    template_path: Path, process_mode_func: ProcessModeFn
) -> Dict[str, TemplateFile]

Catalog templates into a dictionary.

This creates a mapping of a relative file name to a full path.

For a file structure like:

{{ repo_name }}/
    file1.txt
    subdir/
        file2.txt
    empty-subdir/

A call to catalog_templates(Path("/path-to-templates/{{ repo_name }}/"), process_mode_fn) would return:

{
    "{{ repo_name }}": TemplateFile(
        Path("/path-to-templates/{{ repo_name }}"), <ProcessMode.render|write: 3>
    ),
    "{{ repo_name }}/file1.txt": TemplateFile(
        Path("/path-to-templates/{{ repo_name }}/file1.txt"), <ProcessMode.render|write: 3>
    ),
    "{{ repo_name }}/subdir": TemplateFile(
        Path("/path-to-templates/{{ repo_name }}/subdir"), <ProcessMode.render|write: 3>
    ),
    "{{ repo_name }}/subdir/file2.txt": TemplateFile(
        Path("/path-to-templates/{{ repo_name }}/subdir/file2.txt"), <ProcessMode.render|write: 3>
    ),
    "{{ repo_name }}/empty-subdir": TemplateFile(
        Path("/path-to-templates/{{ repo_name }}/empty-subdir"), <ProcessMode.render|write: 3>
    ),
}
PARAMETER DESCRIPTION
template_path

The directory to catalog

TYPE: Path

process_mode_func

A function that takes a path and returns a ProcessMode

TYPE: ProcessModeFn

RETURNS DESCRIPTION
Dict[str, TemplateFile]

A mapping of the relative path as a string to the full path