Files
rengine/reports/render/pdf.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

69 lines
1.9 KiB
Python

"""WeasyPrint is imported here and nowhere else."""
from __future__ import annotations
import logging
import re
from contextlib import contextmanager
from dataclasses import dataclass, field
from shared.definitions.reports import MAX_IMAGE_PIXELS
@dataclass
class PdfResult:
data: bytes
pages: int
warnings: list[str] = field(default_factory=list)
_DATA_URI = re.compile(r"data:([\w./+-]+);base64,[A-Za-z0-9+/=]+")
class _Collector(logging.Handler):
"""WeasyPrint reports a dropped image or a missing face here."""
def __init__(self) -> None:
super().__init__(level=logging.WARNING)
self.messages: list[str] = []
def emit(self, record: logging.LogRecord) -> None:
text = _DATA_URI.sub(
lambda m: f"{m.group(1)} data, {len(m.group(0)) // 1024} KB",
record.getMessage(),
)
text = text[:300]
if text not in self.messages:
self.messages.append(text)
@contextmanager
def render_limits():
"""A small image may decode to a huge canvas."""
from PIL import Image # noqa: PLC0415
previous = Image.MAX_IMAGE_PIXELS
Image.MAX_IMAGE_PIXELS = MAX_IMAGE_PIXELS
weasy = logging.getLogger("weasyprint")
collector = _Collector()
weasy.addHandler(collector)
try:
yield collector
finally:
weasy.removeHandler(collector)
Image.MAX_IMAGE_PIXELS = previous
def to_pdf(html: str, *, base_url: str) -> PdfResult:
from weasyprint import HTML # noqa: PLC0415
from reports.render.fetcher import build_fetcher # noqa: PLC0415
with render_limits() as collected:
document = HTML(
string=html, base_url=base_url, url_fetcher=build_fetcher()
).render()
data = document.write_pdf()
pages = len(document.pages)
return PdfResult(data=data, pages=pages, warnings=list(collected.messages))