Files
rengine/shared/utils/slug.py
T
Yogesh OjhaandClaude Opus 5 2cb8d73280 style: strip explanatory comments across the codebase
The comment rule in CLAUDE.md is one line maximum, never narration: no
multi-line blocks, no module-header explainers, no sentences that explain or
justify the code to the reader. The tree had drifted a long way from it, and
most of the drift is mine.

Removed:
  232  lines of multi-line comment blocks
  203  single-line comments that argued instead of stating
  154  comments joined by so/because/otherwise/rather-than
  143  narrating comments in the frontend
  112  test docstrings (the test name already says what it asserts)
   25  test module docstrings and 25 decorative section dividers

Collapsed:
  324  multi-line docstrings to one factual line
  271  one-line docstrings that had a justifying clause bolted on

Kept: functional directives, section dividers, short field labels, alembic
revision headers, and the eight FastAPI route docstrings that render in /docs.
Where a fact was load-bearing it is in CLAUDE.md, which is where it belongs.

284 python tests and 34 frontend tests pass, ruff and ruff-format clean across
ten packages, svelte-check 0 errors, prettier and eslint clean, app.main imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013dKUV96WkQQETZBMoAG98C
2026-09-11 14:11:23 +05:30

82 lines
2.6 KiB
Python

import re
import uuid
from typing import Any
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession
_ASCII = re.compile(r"[^a-z0-9\s-]")
_WORD = re.compile(r"[^\w\s-]", re.UNICODE)
_SEPARATORS = re.compile(r"[\s_]+")
_RUNS = re.compile(r"-+")
MAX_SLUG_ATTEMPTS = 50
FALLBACK_SLUG = "untitled"
def _reduce(pattern: re.Pattern[str], name: str) -> str:
slug = pattern.sub("", name.lower().strip())
slug = _SEPARATORS.sub("-", slug)
return _RUNS.sub("-", slug).strip("-")
def generate_slug(name: str) -> str:
"""A name written in a non-Latin script still has to produce an addressable slug."""
return _reduce(_ASCII, name) or _reduce(_WORD, name)
def _pending[T](
session: AsyncSession, model: type[T], scope: dict[str, Any]
) -> set[str]:
taken = set()
for obj in session.new:
if not isinstance(obj, model):
continue
if all(getattr(obj, key, None) == value for key, value in scope.items()):
taken.add(getattr(obj, "slug", None))
return taken
async def unique_slug[T](
session: AsyncSession, model: type[T], name: str, **scope: Any
) -> str:
"""Suffix a slug until it is free within its scope."""
base = generate_slug(name) or FALLBACK_SLUG
conditions = [getattr(model, key) == value for key, value in scope.items()]
uncommitted = _pending(session, model, scope)
for attempt in range(1, MAX_SLUG_ATTEMPTS + 1):
slug = base if attempt == 1 else f"{base}-{attempt}"
if slug in uncommitted:
continue
rows = await session.execute(
select(model.id).where(model.slug == slug, *conditions).limit(1)
)
if rows.scalar_one_or_none() is None:
return slug
return f"{base}-{uuid.uuid4().hex[:8]}"
def _slug_conflict(exc: IntegrityError) -> bool:
inner = getattr(exc.orig, "__cause__", None) or exc.orig
return "slug" in (getattr(inner, "constraint_name", None) or "")
async def add_with_unique_slug[T](
session: AsyncSession, row: T, name: str, **scope: Any
) -> T:
"""The slug is constrained in the database."""
for _ in range(MAX_SLUG_ATTEMPTS):
row.slug = await unique_slug(session, type(row), name, **scope)
try:
async with session.begin_nested():
session.add(row)
await session.flush()
except IntegrityError as exc:
if not _slug_conflict(exc):
raise
continue
return row
msg = f"Could not derive a free slug for {name!r}"
raise RuntimeError(msg)