"""Pre-generation hook for the backend cookiecutter template. Validates that ``project_name`` is a valid Python identifier so the generated package dir + imports (``from .X import Y``) do not raise ``SyntaxError``. Hyphens, dots, spaces and leading digits are rejected with a hint to use an underscore-separated name instead. See issue #262: default ``my-project`` used to render as ``from my-project.config.settings import settings`` which is a ``SyntaxError`` (``-`` is not allowed in a Python identifier). """ from __future__ import annotations import re import sys _VALID_IDENTIFIER = re.compile(r"^[a-z][a-z0-9_]*$") def main() -> None: project_name = "{{ cookiecutter.project_name }}" if not _VALID_IDENTIFIER.fullmatch(project_name): sys.exit( f"Invalid project_name: {project_name!r}\n" "project_name must be a valid Python identifier matching " "^[a-z][a-z0-9_]*$ (lowercase, no hyphens/dots/spaces, " "no leading digit).\n" f"Use 'my_project' instead of 'my-project' (or 'my.project', " "'my project')." ) if __name__ == "__main__": main()