vibe-sketcherDocumentation
Source code for vibesketcher.document.pages
"""Store or clear the physical page setup of a document's figures (#163).
The setup lives in ``style.page``. It is additive: a document without one is
exported cropped to its drawing, as before. Where the drawing falls on the
pages is :func:`vibesketcher.render.pages.page_layout`.
"""
from __future__ import annotations
from typing import Any
from pydantic import ValidationError
from vibesketcher.document.model import Document, PageSetup
[docs]
def with_page(document: Document, setup: PageSetup | dict[str, Any] | None) -> Document:
"""``document`` with ``setup`` as its page setup; ``None`` clears it."""
page = PageSetup.model_validate(setup) if isinstance(setup, dict) else setup
style = document.style.model_copy(update={"page": page})
return document.model_copy(update={"style": style})
[docs]
def page_summary(setup: PageSetup | None) -> str:
"""A short description of a page setup, for edit summaries and the CLI."""
if setup is None:
return "no page setup (exports are cropped to the drawing)"
width, height = setup.size_mm()
name = setup.size if isinstance(setup.size, str) else f"{width:g} x {height:g} mm"
turned = f" {setup.orientation}" if setup.orientation else ""
top, right, bottom, left = setup.margins()
margins = (
f"{top:g} mm margins"
if top == right == bottom == left
else f"margins {top:g}, {right:g}, {bottom:g}, {left:g} mm"
)
grid = f", {setup.columns} x {setup.rows} pages" if setup.columns * setup.rows > 1 else ""
centred = ", centred" if setup.center else ""
return f"{name}{turned}, {margins}{grid}{centred}"
[docs]
def page_errors(error: ValidationError) -> str:
"""A refused page setup in one line, each problem named by its field."""
parts = []
for item in error.errors():
# Union members add their type to the location; keep the field names.
kept = [str(p) for p in item["loc"] if not str(p).startswith(("literal[", "tuple["))]
where = ".".join(kept)
message = str(item["msg"]).removeprefix("Value error, ")
parts.append(f"{where}: {message}" if where else message)
return "; ".join(dict.fromkeys(parts))