-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_gen_project.py
More file actions
executable file
·44 lines (35 loc) · 1.31 KB
/
pre_gen_project.py
File metadata and controls
executable file
·44 lines (35 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
"""
Pre-project generation hook for validation
"""
import re
import sys
from logging import basicConfig, getLogger
basicConfig(level="WARNING", format="%(levelname)s: %(message)s")
LOG = getLogger("pre_generation_hook")
# Cookiecutter variables
PROJECT_NAME = "{{ cookiecutter.project_name }}"
PROJECT_SLUG = "{{ cookiecutter.project_slug }}"
def validate_project_name() -> None:
"""Validate that project_name starts with an alphabetical character and contains no spaces."""
if not re.match(r"^[a-zA-Z]", PROJECT_NAME):
LOG.error(
"Invalid project name '%s': must start with an alphabetical character (a-z or A-Z).",
PROJECT_NAME,
)
sys.exit(1)
if " " in PROJECT_NAME:
LOG.error(
"Invalid project name '%s': must not contain spaces. Use hyphens instead (e.g. 'my-project').",
PROJECT_NAME,
)
sys.exit(1)
def validate_project_slug() -> None:
"""Validate that project_slug is a valid Python identifier."""
# Check if project_slug is a valid Python identifier
if not PROJECT_SLUG.isidentifier():
LOG.error("Invalid project slug '%s': Must be a valid Python identifier.", PROJECT_SLUG)
sys.exit(1)
if __name__ == "__main__":
validate_project_name()
validate_project_slug()