str_replace

String replacement utilities.

cookiecutter_maker.str_replace.validate_selector(selector: list[str])[source]

Validates that each pattern in the selector list is a substring of the previous pattern.

This function ensures that patterns in a selector form a narrowing hierarchy where each subsequent pattern is more specific (a substring) of the previous pattern.

Parameters:

selector – A list of string patterns to validate. Cannot be empty.

Raises:

ValueError, If the selector is empty or if any pattern is not a substring of the previous pattern in the sequence.

cookiecutter_maker.str_replace.to_placeholder(name: str, selector: list[str]) str[source]

Convert a selector to a placeholder token that can be used in a template.

Example:

>>> to_placeholder(
... name="author",
... selector=['author = "Alice"', '= "Alice"', "Alice"]
... )
'author = "{{ cookiecutter.author }}"'
Parameters:
  • name – The name of the parameter.

  • selector – A list of string patterns to validate. Cannot be empty.

cookiecutter_maker.str_replace.replace_with_placeholders(text: str, replacements: list[tuple[str, str]])[source]

Replace multiple strings in text using temporary placeholders to avoid substring conflicts.

Example:

>>> replace_with_placeholders(
...     text='author = "alice", author_email = "alice@email.com"',
...     replacements=[
...         ("alice@email.com", "{{ cookiecutter.author_email }}"),
...         ("alice", "{{ cookiecutter.author }}"),
...     ]
... )
'author = "{{ cookiecutter.author }}", author_email = "{{ cookiecutter.author_email }}"'
Parameters:
  • text – The input text to perform replacements on

  • replacements – List of tuples (search_string, replace_string)

Returns:

Text with all replacements applied

cookiecutter_maker.str_replace.replace_double_curly_brackets(text: str) str[source]

Replace double curly brackets with raw Jinja2 template syntax.

When converting a seed project to a cookiecutter template, Jinja2 syntax in the original files needs special handling. This function replaces the standard Jinja2 delimiters (‘{{’ and ‘}}’) with raw expressions that will be preserved as literal curly brackets in the generated template, rather than being interpreted as Jinja2/cookiecutter variables.

This ensures that original Jinja2 syntax in the seed project is preserved as literal text in the generated cookiecutter template, preventing conflicts with cookiecutter’s own variable substitution which also uses curly brackets.