-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_shell.py
More file actions
324 lines (281 loc) · 11.8 KB
/
Copy pathapp_shell.py
File metadata and controls
324 lines (281 loc) · 11.8 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from __future__ import annotations
import re
from html import escape
from pathlib import Path
from typing import TypedDict
from backend.payload_types import AppDefaultsPayload, TopologyCatalogEntryPayload
ROOT_DIR = Path(__file__).resolve().parents[1]
SHARED_APP_SHELL_BODY_PATH = ROOT_DIR / "frontend" / "shell" / "app-shell-body.html"
PICKER_GROUP_ORDER = ("Classic", "Periodic Mixed", "Aperiodic")
UNRESOLVED_PLACEHOLDER_PATTERN = re.compile(r"__[A-Z0-9_]+__")
class AppShellRenderValues(TypedDict):
tiling_family_options: str
cell_size_min: str
cell_size_max: str
cell_size_value: str
cell_size_label: str
patch_depth_min: str
patch_depth_max: str
patch_depth_value: str
patch_depth_label: str
speed_min: str
speed_max: str
speed_value: str
speed_label: str
adjacency_field_hidden: str
topology_mode_label: str
adjacency_mode_options: str
_STANDALONE_DOCUMENT_TEMPLATE = """<!DOCTYPE html>
<!-- Generated from frontend/shell/app-shell-body.html by python -m tools build standalone-shell. -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cellular Automaton Lab</title>
<link rel="icon" type="image/svg+xml" href="./favicon.svg">
<script>
(function () {
try {
var storedTheme = window.localStorage.getItem("cellular-automaton-theme");
if (storedTheme === "light" || storedTheme === "dark") {
document.documentElement.dataset.theme = storedTheme;
return;
}
document.documentElement.dataset.theme = "light";
} catch (error) {
void error;
}
}());
</script>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
__APP_SHELL__
<script type="module" src="/frontend/standalone.ts"></script>
</body>
</html>
"""
_STANDALONE_GRID_VIEWPORT_MARKER = '<div id="grid-viewport" class="grid-viewport">'
_STANDALONE_STARTUP_MARKUP = """
<div
id="standalone-startup-overlay"
class="standalone-startup-status"
aria-hidden="false"
aria-live="polite"
>
<div class="standalone-startup-status-card">
<div class="blocking-activity-spinner standalone-startup-spinner" aria-hidden="true"></div>
<div class="standalone-startup-copy">
<strong id="standalone-startup-message" class="blocking-activity-message">Loading app data</strong>
<span id="standalone-startup-detail" class="blocking-activity-detail">
Reading bundled defaults and topology catalog.
</span>
</div>
</div>
</div>"""
def load_shared_app_shell_body() -> str:
return SHARED_APP_SHELL_BODY_PATH.read_text(encoding="utf-8")
def _format_number(value: int | float) -> str:
numeric_value = float(value)
if numeric_value.is_integer():
return str(int(numeric_value))
return str(value)
def _build_option(value: str, label: str, *, selected: bool = False) -> str:
selected_attribute = ' selected="selected"' if selected else ""
return (
f'<option value="{escape(value, quote=True)}"{selected_attribute}>{escape(label)}</option>'
)
def _fallback_topology_mode_label(topology: TopologyCatalogEntryPayload, mode: str) -> str:
formatted_mode = f"{mode[:1].upper()}{mode[1:]}" if mode else "Mode"
if str(topology.get("mode_type") or "adjacency") == "adjacency":
return f"{formatted_mode} adjacency"
return formatted_mode
def _topology_mode_label(topology: TopologyCatalogEntryPayload, mode: str) -> str:
labels = topology.get("mode_labels", {})
if labels.get(mode):
return str(labels[mode])
return _fallback_topology_mode_label(topology, mode)
def _topology_mode_field_label(
topology_catalog: list[TopologyCatalogEntryPayload],
tiling_family: str,
) -> str:
for topology in topology_catalog:
if str(topology["tiling_family"]) == str(tiling_family):
return str(topology.get("mode_label") or "Mode")
return "Mode"
def _group_topologies(
topology_catalog: list[TopologyCatalogEntryPayload],
) -> list[tuple[str, list[TopologyCatalogEntryPayload]]]:
grouped: dict[str, list[TopologyCatalogEntryPayload]] = {
group_name: [] for group_name in PICKER_GROUP_ORDER
}
extra_group_order: list[str] = []
for topology in topology_catalog:
group_name = str(topology.get("picker_group") or "Other")
if group_name not in grouped:
grouped[group_name] = []
extra_group_order.append(group_name)
grouped[group_name].append(topology)
ordered_groups = list(PICKER_GROUP_ORDER) + extra_group_order
return [(group_name, grouped[group_name]) for group_name in ordered_groups]
def build_tiling_family_options_html(
topology_catalog: list[TopologyCatalogEntryPayload],
*,
selected_tiling_family: str | None = None,
) -> str:
rendered_groups: list[str] = []
for group_name, topologies in _group_topologies(topology_catalog):
rendered_options = "\n".join(
_build_option(
str(topology["tiling_family"]),
str(topology["label"]),
selected=str(topology["tiling_family"]) == str(selected_tiling_family),
)
for topology in topologies
)
rendered_groups.append(
"\n".join(
[
f'<optgroup label="{escape(group_name, quote=True)}">',
rendered_options,
"</optgroup>",
]
).rstrip()
)
return "\n".join(rendered_groups)
def build_adjacency_mode_options_html(
topology_catalog: list[TopologyCatalogEntryPayload],
*,
tiling_family: str | None = None,
selected_adjacency_mode: str | None = None,
) -> str:
if not tiling_family:
return ""
for topology in topology_catalog:
if str(topology["tiling_family"]) != str(tiling_family):
continue
supported_modes = topology.get("supported_adjacency_modes", [])
return "\n".join(
_build_option(
str(adjacency_mode),
_topology_mode_label(topology, str(adjacency_mode)),
selected=str(adjacency_mode) == str(selected_adjacency_mode),
)
for adjacency_mode in supported_modes
)
return ""
def _render_shared_app_shell(render_values: AppShellRenderValues) -> str:
rendered = load_shared_app_shell_body()
replacements = {
"__TILING_FAMILY_OPTIONS__": render_values["tiling_family_options"],
"__CELL_SIZE_MIN__": render_values["cell_size_min"],
"__CELL_SIZE_MAX__": render_values["cell_size_max"],
"__CELL_SIZE_VALUE__": render_values["cell_size_value"],
"__CELL_SIZE_LABEL__": render_values["cell_size_label"],
"__PATCH_DEPTH_MIN__": render_values["patch_depth_min"],
"__PATCH_DEPTH_MAX__": render_values["patch_depth_max"],
"__PATCH_DEPTH_VALUE__": render_values["patch_depth_value"],
"__PATCH_DEPTH_LABEL__": render_values["patch_depth_label"],
"__SPEED_MIN__": render_values["speed_min"],
"__SPEED_MAX__": render_values["speed_max"],
"__SPEED_VALUE__": render_values["speed_value"],
"__SPEED_LABEL__": render_values["speed_label"],
"__ADJACENCY_FIELD_HIDDEN__": render_values["adjacency_field_hidden"],
"__TOPOLOGY_MODE_LABEL__": render_values["topology_mode_label"],
"__ADJACENCY_MODE_OPTIONS__": render_values["adjacency_mode_options"],
}
for token, value in replacements.items():
rendered = rendered.replace(token, value)
unresolved_placeholders = sorted(set(UNRESOLVED_PLACEHOLDER_PATTERN.findall(rendered)))
if unresolved_placeholders:
unresolved = ", ".join(unresolved_placeholders)
raise ValueError(f"App shell template contains unresolved placeholders: {unresolved}")
return rendered
def _default_adjacency_hidden_attribute(
*,
topology_catalog: list[TopologyCatalogEntryPayload],
tiling_family: str,
) -> str:
for topology in topology_catalog:
if str(topology["tiling_family"]) != str(tiling_family):
continue
if len(topology.get("supported_adjacency_modes", [])) > 1:
return ""
return "hidden"
def render_server_app_shell(
app_defaults: AppDefaultsPayload,
topology_catalog: list[TopologyCatalogEntryPayload],
) -> str:
simulation_defaults = app_defaults["simulation"]
topology_spec = simulation_defaults["topology_spec"]
selected_tiling_family = str(topology_spec["tiling_family"])
selected_adjacency_mode = str(topology_spec["adjacency_mode"])
cell_size_value = _format_number(app_defaults["ui"]["cell_size"])
patch_depth_value = _format_number(topology_spec["patch_depth"])
speed_value = _format_number(simulation_defaults["speed"])
render_values: AppShellRenderValues = {
"tiling_family_options": build_tiling_family_options_html(
topology_catalog,
selected_tiling_family=selected_tiling_family,
),
"cell_size_min": _format_number(app_defaults["ui"]["min_cell_size"]),
"cell_size_max": _format_number(app_defaults["ui"]["max_cell_size"]),
"cell_size_value": cell_size_value,
"cell_size_label": f"{cell_size_value}px",
"patch_depth_min": _format_number(simulation_defaults["min_patch_depth"]),
"patch_depth_max": _format_number(simulation_defaults["max_patch_depth"]),
"patch_depth_value": patch_depth_value,
"patch_depth_label": f"Depth {patch_depth_value}",
"speed_min": _format_number(simulation_defaults["min_speed"]),
"speed_max": _format_number(simulation_defaults["max_speed"]),
"speed_value": speed_value,
"speed_label": f"Target {speed_value} gen/s",
"adjacency_field_hidden": _default_adjacency_hidden_attribute(
topology_catalog=topology_catalog,
tiling_family=selected_tiling_family,
),
"topology_mode_label": _topology_mode_field_label(
topology_catalog,
selected_tiling_family,
),
"adjacency_mode_options": build_adjacency_mode_options_html(
topology_catalog,
tiling_family=selected_tiling_family,
selected_adjacency_mode=selected_adjacency_mode,
),
}
return _render_shared_app_shell(render_values)
def render_standalone_app_shell() -> str:
render_values: AppShellRenderValues = {
"tiling_family_options": "",
"cell_size_min": "4",
"cell_size_max": "24",
"cell_size_value": "12",
"cell_size_label": "12px",
"patch_depth_min": "0",
"patch_depth_max": "6",
"patch_depth_value": "0",
"patch_depth_label": "Depth 0",
"speed_min": "1",
"speed_max": "30",
"speed_value": "5",
"speed_label": "Target 5 gen/s",
"adjacency_field_hidden": "hidden",
"topology_mode_label": "Mode",
"adjacency_mode_options": "",
}
rendered = _render_shared_app_shell(render_values)
if _STANDALONE_GRID_VIEWPORT_MARKER not in rendered:
raise ValueError("Standalone shell template is missing the grid viewport marker.")
return rendered.replace(
_STANDALONE_GRID_VIEWPORT_MARKER,
"\n".join(
[
_STANDALONE_GRID_VIEWPORT_MARKER,
_STANDALONE_STARTUP_MARKUP,
]
),
1,
)
def render_standalone_document() -> str:
return _STANDALONE_DOCUMENT_TEMPLATE.replace("__APP_SHELL__", render_standalone_app_shell())