-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_report.py
More file actions
266 lines (219 loc) · 8.69 KB
/
generate_report.py
File metadata and controls
266 lines (219 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""
Generate docs/mockups/README.md — a visual report of all steami_screen tutorials.
For each tutorial:
- Runs screenshot.py to refresh the simulator PNG
- Rasterizes the SVG reference mockup
- Computes SSIM similarity score
- Generates a 2-column gallery in Markdown (HTML table)
Usage:
python generate_report.py
Dependencies:
pip install Pillow cairosvg scikit-image numpy
"""
import ast
import os
import subprocess
import sys
from datetime import date
ROOT = os.path.dirname(os.path.abspath(__file__))
MOCKUPS_DIR = os.path.join(ROOT, "docs", "mockups")
TUTORIALS_DIR = os.path.join(ROOT, "tutorials")
PYTHON = sys.executable
THRESHOLD_SSIM = 0.85
# ---------------------------------------------------------------------------
# Discovery and metadata
# ---------------------------------------------------------------------------
def find_tutorials():
"""Return sorted list of tutorial names that have a screenshot.py."""
tutorials = []
if not os.path.isdir(TUTORIALS_DIR):
return tutorials
for name in sorted(os.listdir(TUTORIALS_DIR)):
path = os.path.join(TUTORIALS_DIR, name, "screenshot.py")
if os.path.isfile(path):
tutorials.append(name)
return tutorials
def extract_metadata(tutorial_name):
"""Parse the METADATA dict from a tutorial's screenshot.py using ast."""
path = os.path.join(TUTORIALS_DIR, tutorial_name, "screenshot.py")
with open(path, encoding="utf-8") as f:
source = f.read()
try:
tree = ast.parse(source)
for node in tree.body:
if isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name) and target.id == "METADATA":
return ast.literal_eval(node.value)
except Exception:
pass
return {"title": tutorial_name, "widget": "", "description": ""}
# ---------------------------------------------------------------------------
# Image generation
# ---------------------------------------------------------------------------
def run_screenshot(tutorial_name):
"""Run screenshot.py to refresh the simulator PNG. Returns True on success."""
script = os.path.join(TUTORIALS_DIR, tutorial_name, "screenshot.py")
result = subprocess.run(
[PYTHON, script], capture_output=True, text=True, cwd=ROOT
)
if result.returncode != 0:
print(f" WARNING: screenshot.py failed: {result.stderr[:200]}")
return False
return True
def rasterize_svg(tutorial_name, size=384):
"""Rasterize the SVG reference to PNG. Returns True on success."""
svg_path = os.path.join(MOCKUPS_DIR, f"{tutorial_name}.svg")
if not os.path.isfile(svg_path):
return False
ref_path = os.path.join(MOCKUPS_DIR, f"{tutorial_name}_ref.png")
try:
import cairosvg
cairosvg.svg2png(
url=svg_path,
write_to=ref_path,
output_width=size,
output_height=size,
background_color="black",
)
return True
except ImportError:
print(" WARNING: cairosvg not installed, skipping SVG rasterization")
return False
# ---------------------------------------------------------------------------
# Scoring
# ---------------------------------------------------------------------------
def compute_ssim(tutorial_name):
"""Compute SSIM between sim and ref PNGs. Returns float or None."""
sim_path = os.path.join(MOCKUPS_DIR, f"{tutorial_name}_sim.png")
ref_path = os.path.join(MOCKUPS_DIR, f"{tutorial_name}_ref.png")
if not (os.path.isfile(sim_path) and os.path.isfile(ref_path)):
return None
try:
from PIL import Image
import numpy as np
from skimage.metrics import structural_similarity as ssim
img_a = Image.open(sim_path).convert("RGB")
img_b = Image.open(ref_path).convert("RGB")
if img_a.size != img_b.size:
w = min(img_a.width, img_b.width)
h = min(img_a.height, img_b.height)
img_a = img_a.resize((w, h))
img_b = img_b.resize((w, h))
return ssim(np.array(img_a), np.array(img_b), channel_axis=-1)
except ImportError:
return None
# ---------------------------------------------------------------------------
# Report generation
# ---------------------------------------------------------------------------
def _card(name, meta, score, has_svg):
"""Return the HTML block for a single tutorial card."""
title = meta.get("title", name)
widget = meta.get("widget", "")
description = meta.get("description", "")
main_link = f"../../tutorials/{name}/main.py"
lines = []
lines.append(" <td align='center' valign='top' width='50%'>")
lines.append(f" <strong><a href='{main_link}'>{title}</a></strong><br><br>")
if has_svg:
lines.append(
f" <img src='{name}.svg' width='180' title='SVG reference'> "
f"<img src='{name}_sim.png' width='180' title='Simulation'><br>"
)
lines.append(" <sub>SVG reference · Simulation</sub><br>")
else:
lines.append(f" <img src='{name}_sim.png' width='180' title='Simulation'><br>")
lines.append(" <sub>Simulation</sub><br>")
lines.append(f" <br><code>{widget}</code><br>")
lines.append(f" <sub>{description}</sub><br>")
if score is not None:
badge = "✅" if score >= THRESHOLD_SSIM else "❌"
lines.append(f" <sub>SSIM {score:.4f} {badge}</sub>")
lines.append(" </td>")
return "\n".join(lines)
def generate_report(tutorials, data):
"""Build the full README.md content string."""
today = date.today().isoformat()
passed = sum(1 for d in data.values() if d["score"] is not None and d["score"] >= THRESHOLD_SSIM)
total_scored = sum(1 for d in data.values() if d["score"] is not None)
lines = []
lines.append("# steami\\_screen — Visual Report\n")
lines.append(
f"**{len(tutorials)} tutorials** · "
f"**{passed}/{total_scored} PASS** (SSIM ≥ {THRESHOLD_SSIM}) · "
f"Generated {today}\n"
)
lines.append(
"Each card shows the SVG reference mockup alongside the Pillow simulation, "
"with the SSIM similarity score.\n"
)
lines.append("")
lines.append("<table>")
for i in range(0, len(tutorials), 2):
lines.append(" <tr>")
for j in range(2):
if i + j < len(tutorials):
name = tutorials[i + j]
d = data[name]
lines.append(_card(name, d["meta"], d["score"], d["has_svg"]))
else:
lines.append(" <td></td>")
lines.append(" </tr>")
lines.append("</table>")
lines.append("")
lines.append("---")
lines.append(f"*Generated by [`generate_report.py`](../../generate_report.py)*")
return "\n".join(lines) + "\n"
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
DISPLAY_ORDER = [
"01_temperature",
"02_battery",
"03_comfort_dual",
"04_circular_gauge",
"05_scrolling_graph",
"06_dpad_menu",
"07_compass",
"09_watch",
"08_smiley_happy",
"08_smiley_sad",
"08_smiley_surprised",
"08_smiley_angry",
"08_smiley_sleeping",
"08_smiley_love",
"08_smiley_reactive",
]
def main():
discovered = find_tutorials()
# Apply explicit display order; append any unlisted tutorials at the end
ordered = [t for t in DISPLAY_ORDER if t in discovered]
ordered += [t for t in discovered if t not in ordered]
tutorials = ordered
print(f"Found {len(tutorials)} tutorial(s).")
data = {}
for name in tutorials:
print(f"\n[{name}]")
meta = extract_metadata(name)
print(f" title: {meta.get('title', '?')}")
print(" Running screenshot.py...")
run_screenshot(name)
has_svg = os.path.isfile(os.path.join(MOCKUPS_DIR, f"{name}.svg"))
if has_svg:
print(" Rasterizing SVG...")
rasterize_svg(name)
score = compute_ssim(name)
if score is not None:
status = "PASS" if score >= THRESHOLD_SSIM else "FAIL"
print(f" SSIM: {score:.4f} ({status})")
else:
print(" SSIM: n/a (no ref PNG or missing dependency)")
data[name] = {"meta": meta, "score": score, "has_svg": has_svg}
readme_path = os.path.join(MOCKUPS_DIR, "README.md")
content = generate_report(tutorials, data)
with open(readme_path, "w", encoding="utf-8") as f:
f.write(content)
print(f"\nReport saved: {readme_path}")
if __name__ == "__main__":
main()