Skip to content

_util

_util

Vendored utility functions for filesystem, templates, and logging.

ensure_dir(path)

Create a directory (and parents) if it doesn't exist.

Source code in src/terok_sandbox/_util/_fs.py
def ensure_dir(path: Path) -> None:
    """Create a directory (and parents) if it doesn't exist."""
    path.mkdir(parents=True, exist_ok=True)

ensure_dir_writable(path, label)

Create path if needed and verify it is writable, or exit with an error.

Source code in src/terok_sandbox/_util/_fs.py
def ensure_dir_writable(path: Path, label: str) -> None:
    """Create *path* if needed and verify it is writable, or exit with an error."""
    try:
        path.mkdir(parents=True, exist_ok=True)
    except Exception as e:
        raise SystemExit(f"{label} directory is not writable: {path} ({e})")
    if not path.is_dir():
        raise SystemExit(f"{label} path is not a directory: {path}")
    if not os.access(path, os.W_OK | os.X_OK):
        uid = os.getuid()
        gid = os.getgid()
        raise SystemExit(
            f"{label} directory is not writable: {path}\n"
            f"Fix permissions for the user running terok-sandbox (uid={uid}, gid={gid}). "
            f"Example: sudo chown -R {uid}:{gid} {path}"
        )

log_debug(message)

Append a DEBUG line to the terok-sandbox log.

Source code in src/terok_sandbox/_util/_logging.py
def log_debug(message: str) -> None:
    """Append a DEBUG line to the terok-sandbox log."""
    _log(message, level="DEBUG")

log_warning(message)

Append a WARNING line to the terok-sandbox log.

Source code in src/terok_sandbox/_util/_logging.py
def log_warning(message: str) -> None:
    """Append a WARNING line to the terok-sandbox log."""
    _log(message, level="WARNING")

warn_user(component, message)

Print a structured warning to stderr and log it.

Source code in src/terok_sandbox/_util/_logging.py
def warn_user(component: str, message: str) -> None:
    """Print a structured warning to stderr and log it."""
    import sys

    try:
        print(f"Warning [{component}]: {message}", file=sys.stderr)
    except Exception:
        pass
    log_warning(f"[{component}] {message}")

render_template(template_path, variables)

Read template_path and replace {{KEY}} tokens with variables values.

Source code in src/terok_sandbox/_util/_templates.py
def render_template(template_path: Path, variables: dict) -> str:
    """Read *template_path* and replace ``{{KEY}}`` tokens with *variables* values."""
    content = template_path.read_text()
    for k, v in variables.items():
        content = content.replace(f"{{{{{k}}}}}", str(v))
    return content