Skip to content

layers

Layer management.

Bases: BaseModel

Configuration for a layer of a composition.

initial_context: MutableMapping[str, Any] = Field(
    default_factory=dict
)

Dictionary that will provide values for input.

layer_name: str

The name of the template layer.

merge_strategies: Dict[str, str] = Field(
    default_factory=lambda: {"*": DO_NOT_MERGE}
)

The method to merge specific paths or glob patterns.

no_input: bool = False

Do not prompt for parameters and only use cookiecutter.json file content.

This is only used for initial generation. After initial generation, the results are stored in the context.

overwrite: List[str] = Field(default_factory=list)

Paths or glob patterns to always overwrite.

overwrite_exclude: List[str] = Field(default_factory=list)

Paths or glob patterns to exclude from overwriting.

skip_generation: List[str] = Field(default_factory=list)

Paths or glob patterns to skip attempting to generate.

skip_hooks: bool = False

Skip the template hooks.

skip_if_file_exists: bool = True

Skip the files in the corresponding directories if they already exist.

template: Template

Information about the template.

generate_context(
    default_context: MutableMapping[str, Any]
) -> OrderedDict

Get the context for prompting the user for values.

The order of precedence is:

  1. initial_context from the composition or command-line
  2. default_context from the user_config
  3. raw context from the template

Equivalent to cookiecutter.generate.generate_context but with the following differences:

  1. Reading the raw context file is handled by the layer’s template
  2. The layer’s initial context is treated as the extra_context
  3. Does not namespace the context with {"cookiecutter": ...}

Parameters:

default_context

The default context from the user_config

TYPE: MutableMapping[str, Any]

Returns:

OrderedDict

A dict containing the context for prompting the user

Bases: BaseModel

Information about a rendered layer.

latest_commit: Optional[str]

The latest commit checked out if the layer source was a git repo.

layer: LayerConfig

The original layer configuration that was rendered.

location: DirectoryPath

The directory where the layer was rendered.

rendered_commit: Optional[str] = None

If a git template, this is the commit of the template that was rendered.

rendered_context: MutableMapping[str, Any]

The context based on questions asked.

rendered_name: Optional[str] = None

The name of the rendered template directory.

set_rendered_name(values: Dict[str, Any]) -> Dict[str, Any]

Set the [cookie_composer.layers.RenderedLayer.layer_name] to the name of the rendered template directory.

Bases: Enum

How to deal with a file.

MERGE = 3

Merge the file with an existing file, or write a new file.

SKIP = 2

Skip the file.

WRITE = 1

Write or overwrite the file.

get_accept_hooks_per_layer(
    accept_hooks: str, num_layers: int
) -> list

Convert a single accept_hooks value into a value for every layer based on num_layers.

get_layer_context(
    template_repo_dir: Path,
    context_for_prompting: dict,
    initial_context: MutableMapping[str, Any],
    full_context: Context,
    no_input: bool = False,
) -> dict

Get the context for a layer pre-rendering values using previous layers contexts as defaults.

The layer context is the combination of several things:

  • raw layer context (contents of the cookiecutter.json file)
  • The user’s default context (from the user’s cookiecutter config file)
  • initial context set in the template composition (or {} if not a composition or not set)
  • initial context passed in by user (as set from the command line. This is merged into the layer’s inital context when the layer is deserialized. See cookie_composer.io.get_composition_from_path_or_url)
  • context from previous layers

Parameters:

template_repo_dir

The location of the template repo to use for rendering

TYPE: Path

context_for_prompting

The raw context from the cookiecutter.json file with user defaults applied

TYPE: dict

initial_context

The initial context from the layer configuration

TYPE: MutableMapping[str, Any]

full_context

A full context from previous layers.

TYPE: Context

no_input

If False do not prompt for missing values and use defaults instead.

TYPE: bool

DEFAULT: False

Returns:

dict

A dict containing the context for rendering the layer

get_template_rendered_name(
    template: Template, context: MutableMapping
) -> str

Find and render the template’s root directory’s name.

get_write_strategy(
    origin: Path,
    destination: Path,
    rendered_layer: RenderedLayer,
) -> WriteStrategy

Based on the layer_config rules, determine if we should overwrite an existing path.

Parameters:

origin

Path within the rendered layer that we are evaluating.

TYPE: Path

destination

Path to which we would write this file (may not actually exist)

TYPE: Path

rendered_layer

Rendered layer configuration.

TYPE: RenderedLayer

Returns:

WriteStrategy

The appropriate way to handle writing this file.

merge_layers(
    destination: Path, rendered_layer: RenderedLayer
) -> None

Merge a layer into another layer using the rules specified in the layer_config.

Parameters:

destination

The root path to merge into.

TYPE: Path

rendered_layer

The information about the rendered layer.

TYPE: RenderedLayer

render_layer(
    layer_config: LayerConfig,
    render_dir: Path,
    full_context: Optional[Context] = None,
    commit: Optional[str] = None,
    accept_hooks: str = "yes",
) -> RenderedLayer

Process one layer of the template composition.

Renders the template using cookiecutter.

Parameters:

layer_config

The configuration of the layer to render

TYPE: LayerConfig

render_dir

Where to render the template

TYPE: Path

full_context

The extra context from all layers in the composition

TYPE: Optional[Context]

DEFAULT: None

commit

The commit to checkout if the template is a git repo

TYPE: Optional[str]

DEFAULT: None

accept_hooks

Accept pre- and post-hooks if set to True

TYPE: str

DEFAULT: 'yes'

Returns:

RenderedLayer

The rendered layer information

render_layers(
    layers: List[LayerConfig],
    destination: Path,
    initial_context: Optional[dict] = None,
    no_input: bool = False,
    accept_hooks: str = "all",
) -> List[RenderedLayer]

Render layers to a destination.

Parameters:

layers

A list of LayerConfig to render

TYPE: List[LayerConfig]

destination

The location to merge the rendered layers to

TYPE: Path

initial_context

An initial context to pass to the rendering

TYPE: Optional[dict]

DEFAULT: None

no_input

If True force each layer’s no_input attribute to True

TYPE: bool

DEFAULT: False

accept_hooks

How to process pre/post hooks.

TYPE: str

DEFAULT: 'all'

Returns:

List[RenderedLayer]

A list of the rendered layer information