28 lines
898 B
Python
28 lines
898 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from editor_app import config
|
|
|
|
DEFAULT_MAIN_PY = 'print("Hello, World!")\n'
|
|
|
|
|
|
def safe_workspace_leaf(username: str, user_id: int) -> str:
|
|
base = re.sub(r"[^a-zA-Z0-9._-]+", "-", username.strip()).strip("-").lower() or "user"
|
|
return f"{base}-{user_id}"
|
|
|
|
|
|
def user_workspace_root(user_id: int, username: str, workspace_root: Path | None = None) -> Path:
|
|
root = (workspace_root or config.WORKSPACE_ROOT).resolve()
|
|
return root / "users" / safe_workspace_leaf(username, user_id)
|
|
|
|
|
|
def ensure_default_code_main(user_root: Path) -> None:
|
|
"""Ensure code/ exists and add a starter main.py when missing."""
|
|
code_dir = user_root / "code"
|
|
code_dir.mkdir(parents=True, exist_ok=True)
|
|
main_py = code_dir / "main.py"
|
|
if not main_py.exists():
|
|
main_py.write_text(DEFAULT_MAIN_PY, encoding="utf-8")
|