layers
Layer management.
Attributes¶
Classes¶
LayerConfig
¶
Bases: BaseModel
Configuration for a layer of a composition.
Attributes¶
initial_context
class-attribute
instance-attribute
¶
initial_context: MutableMapping[str, Any] = Field(
default_factory=dict
)
Dictionary that will provide values for input.
merge_strategies
class-attribute
instance-attribute
¶
merge_strategies: Dict[str, str] = Field(
default_factory=lambda: {"*": DO_NOT_MERGE}
)
The method to merge specific paths or glob patterns.
no_input
class-attribute
instance-attribute
¶
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
class-attribute
instance-attribute
¶
overwrite: List[str] = Field(default_factory=list)
Paths or glob patterns to always overwrite.
overwrite_exclude
class-attribute
instance-attribute
¶
overwrite_exclude: List[str] = Field(default_factory=list)
Paths or glob patterns to exclude from overwriting.
skip_generation
class-attribute
instance-attribute
¶
skip_generation: List[str] = Field(default_factory=list)
Paths or glob patterns to skip attempting to generate.
skip_if_file_exists
class-attribute
instance-attribute
¶
skip_if_file_exists: bool = True
Skip the files in the corresponding directories if they already exist.
Functions¶
generate_context
¶
generate_context(
default_context: MutableMapping[str, Any]
) -> OrderedDict
Get the context for prompting the user for values.
The order of precedence is:
initial_contextfrom the composition or command-linedefault_contextfrom the user_configraw contextfrom the template
Equivalent to cookiecutter.generate.generate_context but with the following differences:
- Reading the raw context file is handled by the layer’s template
- The layer’s initial context is treated as the
extra_context - 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
RenderedLayer
¶
Bases: BaseModel
Information about a rendered layer.
Attributes¶
latest_commit
property
¶
latest_commit: Optional[str]
The latest commit checked out if the layer source was a git repo.
rendered_commit
class-attribute
instance-attribute
¶
rendered_commit: Optional[str] = None
If a git template, this is the commit of the template that was rendered.
rendered_context
instance-attribute
¶
rendered_context: MutableMapping[str, Any]
The context based on questions asked.
rendered_name
class-attribute
instance-attribute
¶
rendered_name: Optional[str] = None
The name of the rendered template directory.
Functions¶
set_rendered_name
classmethod
¶
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.
Functions¶
get_accept_hooks_per_layer
¶
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
¶
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
Falsedo not prompt for missing values and use defaults instead.TYPE:
boolDEFAULT:
False
Returns:
-
dict -
A dict containing the context for rendering the layer
get_template_rendered_name
¶
get_template_rendered_name(
template: Template, context: MutableMapping
) -> str
Find and render the template’s root directory’s name.
get_write_strategy
¶
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
¶
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
¶
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
DEFAULT:
None commit-
The commit to checkout if the template is a git repo
DEFAULT:
None accept_hooks-
Accept pre- and post-hooks if set to
TrueTYPE:
strDEFAULT:
'yes'
Returns:
-
RenderedLayer -
The rendered layer information
render_layers
¶
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
LayerConfigto renderTYPE:
List[LayerConfig] destination-
The location to merge the rendered layers to
TYPE:
Path initial_context-
An initial context to pass to the rendering
DEFAULT:
None no_input-
If
Trueforce each layer’sno_inputattribute toTrueTYPE:
boolDEFAULT:
False accept_hooks-
How to process pre/post hooks.
TYPE:
strDEFAULT:
'all'
Returns:
-
List[RenderedLayer] -
A list of the rendered layer information