"""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 def parent_dir(explicit: Path | None) -> Path: """Return the parent for a new project: *explicit* when given, else cwd.""" if explicit is None: return Path.cwd() if not explicit.is_dir(): raise ScaffoldError(f"--dir is not an existing directory: {explicit}") return explicit