Coverage for app / documents / visuals / render.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""A visual fence made ready for the PDF: a figure, or a note saying why not. 

2 

3Everything is done here, before the visual joins the story: read, laid out 

4at the frame's width. A failure can then only be a note, never an error 

5inside ``doc.build``. Nothing raised here carries a fence's content: an 

6unexpected failure is logged by kind and exception type. 

7""" 

8 

9from __future__ import annotations 

10 

11from dataclasses import dataclass 

12 

13from app.util import vocab 

14from app.util.logger import get_logger 

15 

16from . import chart, maps, network, plot # noqa: F401 — they register 

17from .mermaid import visual as _mermaid # noqa: F401 — it registers 

18from .fence import VisualError 

19from .flowable import VisualFlowable 

20from .kinds import VisualKind 

21from .registry import VISUALS 

22from .theme import Theme 

23 

24logger = get_logger(__name__) 

25 

26vocab.check({kind: visual.noun for kind, visual in VISUALS.items()}, VisualKind) 

27 

28#: The most visuals one document draws; the rest are notes. 

29MAX_VISUALS_PER_DOCUMENT = 40 

30#: What a document's force layouts may cost together, in nodes squared: 

31#: three graphs of 200 nodes, about 4.5 s of layout. 

32LAYOUT_BUDGET = 3 * 200**2 

33 

34 

35@dataclass(frozen=True) 

36class Figure: 

37 title: str | None 

38 flowable: VisualFlowable 

39 credit: str | None 

40 

41 

42@dataclass(frozen=True) 

43class Unshown: 

44 #: "Chart", "Plot", …: the note reads "Chart not shown — <reason>". 

45 noun: str 

46 title: str | None 

47 reason: str 

48 

49 

50@dataclass 

51class Budget: 

52 """What one document has left to draw.""" 

53 

54 visuals: int = MAX_VISUALS_PER_DOCUMENT 

55 layout: int = LAYOUT_BUDGET 

56 

57 

58def prepare( 

59 kind: str, 

60 source: str, 

61 *, 

62 theme: Theme, 

63 width: float, 

64 max_height: float, 

65 budget: Budget, 

66) -> Figure | Unshown: 

67 """The figure the fence draws, or why it is not drawn.""" 

68 visual = VISUALS[kind] 

69 if budget.visuals <= 0: 

70 return Unshown( 

71 visual.noun, 

72 None, 

73 f"a document draws at most {MAX_VISUALS_PER_DOCUMENT} visuals", 

74 ) 

75 budget.visuals -= 1 

76 title: str | None = None 

77 try: 

78 spec = visual.read(source) 

79 title = visual.title(spec) 

80 cost = visual.cost(spec) 

81 if cost > budget.layout: 

82 raise VisualError("it is too large to lay out in this document") 

83 budget.layout -= cost 

84 layout = visual.layout(spec, width, max_height, theme) 

85 except VisualError as exc: 

86 return Unshown(visual.noun, title, str(exc)) 

87 except Exception as exc: # noqa: BLE001 — a visual never stops a PDF 

88 logger.warning( 

89 "[documents] a %s could not be drawn (%s)", kind, type(exc).__name__ 

90 ) 

91 return Unshown(visual.noun, title, "it could not be drawn") 

92 flowable = VisualFlowable(visual, spec, theme, layout, max_height) 

93 return Figure(title, flowable, layout.credit or visual.credit(spec))