28 lines
831 B
Python
28 lines
831 B
Python
"""Create new python projects from the versioned template."""
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
NAME_PATTERN = re.compile(r"^[a-z]+(-[a-z]+)*$")
|
|
|
|
NAME_RULE = "lowercase a-z words separated by single hyphens"
|
|
|
|
|
|
class ScaffoldError(Exception):
|
|
"""A refusal caused by invalid input or an unusable target."""
|
|
|
|
|
|
def validate_name(name: str) -> str:
|
|
"""Return *name* when it matches the naming rule, else refuse loudly."""
|
|
if not NAME_PATTERN.match(name):
|
|
raise ScaffoldError(f"invalid name {name!r}: must be {NAME_RULE}")
|
|
return name
|
|
|
|
|
|
def target_dir(name: str, parent: Path) -> Path:
|
|
"""Return *parent* / *name*, refusing when that directory already exists."""
|
|
target = parent / name
|
|
if target.exists():
|
|
raise ScaffoldError(f"target directory already exists: {target}")
|
|
return target
|