Coverage for src/retrospeedlab/docs_site/render.py: 85%

48 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-12-02 22:44 +0000

1from datetime import datetime 

2from html import escape 

3 

4from retrospeedlab.docs_site.cells import Bar, Cell, Column, Pill, Table, Tile 

5from retrospeedlab.docs_site.labels import PERCENT, fixed 

6from retrospeedlab.docs_site.stack import IssueCounts, LiveGroup 

7 

8RESTING = "These numbers are resting right now; they come back with the API." 

9TILE_RESTING = "Back with the API" 

10READ_AT_FORMAT = "%Y-%m-%d %H:%M UTC" 

11 

12 

13def quiet(message: str) -> str: 

14 return f'<p class="live-quiet">{escape(message)}</p>' 

15 

16 

17def cell_html(cell: Cell) -> str: 

18 if isinstance(cell, str): 

19 return escape(cell) 

20 if isinstance(cell, Pill): 

21 return f'<span class="live-pill {cell.tone}">{escape(cell.text)}</span>' 

22 return bar_html(cell) 

23 

24 

25def bar_html(cell: Bar) -> str: 

26 fill = f'<span class="live-bar-fill" style="width: {fixed(cell.share * PERCENT, 1)}%"></span>' 

27 track = f'<span class="live-bar-track" aria-hidden="true">{fill}</span>' 

28 return f'<span class="live-bar">{track}{escape(cell.text)}</span>' 

29 

30 

31def numeric(flag: bool) -> str: 

32 return ' class="numeric"' if flag else "" 

33 

34 

35def row_html(columns: list[Column], row: list[Cell]) -> str: 

36 cells = "".join( 

37 f"<td{numeric(column.numeric)}>{cell_html(cell)}</td>" for column, cell in zip(columns, row, strict=True) 

38 ) 

39 return f"<tr>{cells}</tr>" 

40 

41 

42def grid_html(table: Table) -> str: 

43 head = "".join(f"<th{numeric(column.numeric)}>{escape(column.label)}</th>" for column in table.columns) 

44 body = "".join(row_html(table.columns, row) for row in table.rows) 

45 return f'<div class="live-scroll"><table><thead><tr>{head}</tr></thead><tbody>{body}</tbody></table></div>' 

46 

47 

48def table_html(table: Table) -> str: 

49 takeaway = "" if table.takeaway is None else f'<p class="live-takeaway">{escape(table.takeaway)}</p>' 

50 grid = grid_html(table) if table.rows else quiet(table.empty) 

51 note = "" if table.note is None else quiet(table.note) 

52 return f'<div class="live">{takeaway}{grid}{note}</div>' 

53 

54 

55def tile_html(tile: Tile | None) -> str: 

56 if tile is None: 

57 return f'<div class="live-tile"><p class="live-tile-detail">{TILE_RESTING}</p></div>' 

58 return ( 

59 f'<div class="live-tile"><p class="live-tile-label">{escape(tile.label)}</p>' 

60 f'<p class="live-tile-value">{escape(tile.value)}</p>' 

61 f'<p class="live-tile-detail">{escape(tile.detail)}</p></div>' 

62 ) 

63 

64 

65def tiles_html(tiles: list[Tile | None], read_at: datetime) -> str: 

66 grid = f'<div class="live-tiles">{"".join(tile_html(tile) for tile in tiles)}</div>' 

67 return grid + quiet(f"Read from the API at {read_at.strftime(READ_AT_FORMAT)}.") 

68 

69 

70def stack_html(module: str, issues: IssueCounts | None, groups: list[LiveGroup]) -> str: 

71 counts = "" if issues is None else f": {issues.open} open and {issues.resolved} resolved Bugsink issues" 

72 lines = [f"<p><code>{escape(module)}</code>{counts}</p>"] 

73 for group in groups: 

74 rows = "".join( 

75 f"<tr><td>{escape(row.label)}</td><td>{escape(row.value)}</td><td>{escape(row.window)}</td></tr>" 

76 for row in group.rows 

77 ) 

78 lines += ["", f"### {group.title}", "", f"<table><tbody>{rows}</tbody></table>"] 

79 return "\n".join(lines)