systemap 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- systemap/__init__.py +48 -0
- systemap/__main__.py +6 -0
- systemap/change.py +328 -0
- systemap/check.py +873 -0
- systemap/cli.py +1006 -0
- systemap/config.py +710 -0
- systemap/delta.py +670 -0
- systemap/describe.py +254 -0
- systemap/evidence.py +157 -0
- systemap/extract.py +938 -0
- systemap/facts.py +218 -0
- systemap/figure.py +370 -0
- systemap/judgement.py +622 -0
- systemap/model.py +732 -0
- systemap/nest.py +199 -0
- systemap/page.py +1003 -0
- systemap/place.py +1053 -0
- systemap/py.typed +0 -0
- systemap/route.py +819 -0
- systemap/scaffold.py +404 -0
- systemap/schematic.py +1915 -0
- systemap/skill/SKILL.md +240 -0
- systemap/skill/references/example.md +201 -0
- systemap/skill/references/journeys-and-invariants.md +63 -0
- systemap/skill/references/layers.md +154 -0
- systemap/skill/references/layout.md +116 -0
- systemap/skill/references/maintenance.md +73 -0
- systemap/skill/references/pitfalls.md +152 -0
- systemap/skill/references/schema.md +336 -0
- systemap/skill/references/second-pass.md +149 -0
- systemap/skill.py +61 -0
- systemap/suggest.py +184 -0
- systemap/theme.py +521 -0
- systemap-1.0.0.dist-info/METADATA +253 -0
- systemap-1.0.0.dist-info/RECORD +38 -0
- systemap-1.0.0.dist-info/WHEEL +4 -0
- systemap-1.0.0.dist-info/entry_points.txt +2 -0
- systemap-1.0.0.dist-info/licenses/LICENSE +21 -0
systemap/facts.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Readable views of the facts file: what `systemap facts` prints.
|
|
2
|
+
|
|
3
|
+
The facts file runs to hundreds of kilobytes on a real tree, and an agent
|
|
4
|
+
that reads it whole spends its context on JSON it will not use. These
|
|
5
|
+
views are what the skill's first step reads instead, one question each:
|
|
6
|
+
|
|
7
|
+
--modules one line per module: the first sentence of its
|
|
8
|
+
docstring, then its public names, imports and
|
|
9
|
+
tests counted
|
|
10
|
+
--docstrings one line per module: the first sentence of its
|
|
11
|
+
docstring, and nothing else
|
|
12
|
+
--module NAME one module's record, rendered: its docstring, its
|
|
13
|
+
public names with their kinds, what it imports,
|
|
14
|
+
what imports it, its third-party imports, and how
|
|
15
|
+
many tests import it (never their names)
|
|
16
|
+
--names NAME one module's public names with their kinds, a
|
|
17
|
+
re-export marked with the module that defines it
|
|
18
|
+
--entry-points where a run can start, the way a person names it,
|
|
19
|
+
each with its target: the function a console
|
|
20
|
+
script calls, or the script a subcommand belongs to
|
|
21
|
+
--external every third-party import, and the modules that use it
|
|
22
|
+
--imports NAME what one module imports, and what imports it
|
|
23
|
+
|
|
24
|
+
Nothing here is a fact the file does not hold; every line is read out of
|
|
25
|
+
the same records `systemap check` reads. No view prints a test's name:
|
|
26
|
+
the map says what, never how much, and a test name is a how-much.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import difflib
|
|
32
|
+
import re
|
|
33
|
+
from typing import Any
|
|
34
|
+
|
|
35
|
+
from systemap.extract import entry_label, is_empty_marker
|
|
36
|
+
from systemap.model import public_names
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class UnknownModule(KeyError):
|
|
40
|
+
"""A module name the facts do not have; `closest` is the nearest they do."""
|
|
41
|
+
|
|
42
|
+
def __init__(self, name: str, closest: str) -> None:
|
|
43
|
+
super().__init__(name)
|
|
44
|
+
self.name = name
|
|
45
|
+
self.closest = closest
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _record(facts: dict[str, Any], name: str) -> dict[str, Any]:
|
|
49
|
+
components: dict[str, Any] = facts.get("components", {})
|
|
50
|
+
if name in components:
|
|
51
|
+
return dict(components[name])
|
|
52
|
+
matches = difflib.get_close_matches(name, sorted(components), n=1, cutoff=0.0)
|
|
53
|
+
raise UnknownModule(name, matches[0] if matches else "")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _plural(n: int, noun: str) -> str:
|
|
57
|
+
return f"{n} {noun}{'' if n == 1 else 's'}"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def first_sentence(text: str) -> str:
|
|
61
|
+
"""The first sentence of a docstring's opening paragraph, or empty."""
|
|
62
|
+
text = " ".join((text or "").split())
|
|
63
|
+
if not text:
|
|
64
|
+
return ""
|
|
65
|
+
return re.split(r"(?<=[.!?])\s+", text, maxsplit=1)[0]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
NO_DOCSTRING = "no docstring"
|
|
69
|
+
EMPTY_MARKER = "empty package marker"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _opening(record: dict[str, Any]) -> str:
|
|
73
|
+
"""What a module's line says about it: its first sentence, or that it has none."""
|
|
74
|
+
return first_sentence(record.get("docstring", "")) or NO_DOCSTRING
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def kinds(record: dict[str, Any]) -> list[tuple[str, str]]:
|
|
78
|
+
"""Every public name with its kind, in source order; a re-export says whence.
|
|
79
|
+
|
|
80
|
+
A facts file from before `names` was recorded offers its functions,
|
|
81
|
+
classes, errors and constants, which is what it has.
|
|
82
|
+
"""
|
|
83
|
+
names = record.get("names")
|
|
84
|
+
if names is not None:
|
|
85
|
+
out: list[tuple[str, str]] = []
|
|
86
|
+
for n in names:
|
|
87
|
+
kind = str(n.get("kind", "object"))
|
|
88
|
+
source = n.get("reexport_of")
|
|
89
|
+
out.append((n["name"], f"{kind}, re-exported from {source}" if source else kind))
|
|
90
|
+
return out
|
|
91
|
+
return (
|
|
92
|
+
[(f["name"], "function") for f in record.get("functions", [])]
|
|
93
|
+
+ [(c["name"], "class") for c in record.get("classes", [])]
|
|
94
|
+
+ [(e["name"], "error") for e in record.get("errors", [])]
|
|
95
|
+
+ [(k["name"], "constant") for k in record.get("constants", [])]
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def modules(facts: dict[str, Any]) -> list[str]:
|
|
100
|
+
"""One line per module, in name order: the first sentence, then the counts."""
|
|
101
|
+
components: dict[str, Any] = facts.get("components", {})
|
|
102
|
+
out = [
|
|
103
|
+
f"modules: {len(components)}; each with the first sentence of its docstring, "
|
|
104
|
+
"then its public names, imports and tests counted"
|
|
105
|
+
]
|
|
106
|
+
for name in sorted(components):
|
|
107
|
+
record = components[name]
|
|
108
|
+
if is_empty_marker(record):
|
|
109
|
+
out.append(f" {name}: {EMPTY_MARKER}")
|
|
110
|
+
continue
|
|
111
|
+
counts = ", ".join(
|
|
112
|
+
(
|
|
113
|
+
_plural(len(public_names(record)), "name"),
|
|
114
|
+
_plural(len(record.get("imports", [])), "import"),
|
|
115
|
+
_plural(int(record.get("tests_total", 0)), "test"),
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
out.append(f" {name}: {_opening(record)} ({counts})")
|
|
119
|
+
return out
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def docstrings(facts: dict[str, Any]) -> list[str]:
|
|
123
|
+
"""One line per module: the first sentence of its docstring."""
|
|
124
|
+
components: dict[str, Any] = facts.get("components", {})
|
|
125
|
+
with_one = sum(1 for r in components.values() if first_sentence(r.get("docstring", "")))
|
|
126
|
+
out = [
|
|
127
|
+
f"docstrings: {with_one} of {len(components)} modules have one; the first sentence of each"
|
|
128
|
+
]
|
|
129
|
+
for name in sorted(components):
|
|
130
|
+
record = components[name]
|
|
131
|
+
out.append(f" {name}: {EMPTY_MARKER if is_empty_marker(record) else _opening(record)}")
|
|
132
|
+
return out
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _listed(items: list[str], none: str) -> str:
|
|
136
|
+
return ", ".join(items) if items else none
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def module(facts: dict[str, Any], name: str) -> list[str]:
|
|
140
|
+
"""One module's record, rendered for a reader; never a test's name."""
|
|
141
|
+
record = _record(facts, name)
|
|
142
|
+
out = [f"{name} ({record.get('file', '')})"]
|
|
143
|
+
if is_empty_marker(record):
|
|
144
|
+
out.append(f" {EMPTY_MARKER}: an __init__ with no public names and no imports")
|
|
145
|
+
return out
|
|
146
|
+
out.append(f" docstring: {_opening(record)}")
|
|
147
|
+
named = kinds(record)
|
|
148
|
+
out.append(f" public names: {len(named)}")
|
|
149
|
+
out += [f" {n}: {kind}" for n, kind in named]
|
|
150
|
+
out.append(f" imports: {_listed(list(record.get('imports', [])), 'nothing from the package')}")
|
|
151
|
+
out.append(
|
|
152
|
+
f" imported by: {_listed(list(record.get('imported_by', [])), 'nothing in the package')}"
|
|
153
|
+
)
|
|
154
|
+
out.append(f" external: {_listed(list(record.get('external', [])), 'none')}")
|
|
155
|
+
total, primary = int(record.get("tests_total", 0)), int(record.get("tests_primary", 0))
|
|
156
|
+
out.append(f" tests: {total} import it ({primary} in a file named after it)")
|
|
157
|
+
return out
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def names(facts: dict[str, Any], name: str) -> list[str]:
|
|
161
|
+
"""One module's public names with their kinds, in source order."""
|
|
162
|
+
record = _record(facts, name)
|
|
163
|
+
named = kinds(record)
|
|
164
|
+
out = [f"{name}: {_plural(len(named), 'public name')}"]
|
|
165
|
+
out += [f" {n}: {kind}" for n, kind in named]
|
|
166
|
+
return out
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def entry_points(facts: dict[str, Any]) -> list[str]:
|
|
170
|
+
"""Where a run can start, each with its target beside it."""
|
|
171
|
+
points: list[dict[str, str]] = facts.get("entry_points", [])
|
|
172
|
+
out = [
|
|
173
|
+
f"entry points: {len(points)}; a journey names each that matters; the target is the "
|
|
174
|
+
"function a console script calls, or the script a subcommand belongs to"
|
|
175
|
+
]
|
|
176
|
+
for p in points:
|
|
177
|
+
target = f", target {p['target']}" if p.get("target") else ""
|
|
178
|
+
out.append(f" {entry_label(p)}: {p['module']}{target}")
|
|
179
|
+
return out
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def external(facts: dict[str, Any]) -> list[str]:
|
|
183
|
+
"""Every third-party import, with the modules that import it."""
|
|
184
|
+
components: dict[str, Any] = facts.get("components", {})
|
|
185
|
+
by_import: dict[str, list[str]] = {}
|
|
186
|
+
for name in sorted(components):
|
|
187
|
+
for imported in components[name].get("external", []):
|
|
188
|
+
by_import.setdefault(imported, []).append(name)
|
|
189
|
+
out = [f"external imports: {len(by_import)}; the model sdk line reads these"]
|
|
190
|
+
out += [f" {imported}: {', '.join(users)}" for imported, users in sorted(by_import.items())]
|
|
191
|
+
return out
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def imports(facts: dict[str, Any], name: str) -> list[str]:
|
|
195
|
+
"""What one module imports from the package, and what imports it."""
|
|
196
|
+
record = _record(facts, name)
|
|
197
|
+
uses: dict[str, list[str]] = record.get("uses", {})
|
|
198
|
+
out = [f"{name} imports {len(uses)} modules of the package"]
|
|
199
|
+
for target, taken_names in sorted(uses.items()):
|
|
200
|
+
taken = "the whole module" if taken_names == ["*"] else ", ".join(taken_names)
|
|
201
|
+
out.append(f" {target} ({taken})")
|
|
202
|
+
importers: list[str] = record.get("imported_by", [])
|
|
203
|
+
out.append(f"{name} is imported by {len(importers)} modules of the package")
|
|
204
|
+
out += [f" {m}" for m in importers]
|
|
205
|
+
return out
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
VIEWS = (
|
|
209
|
+
"views: --modules (one line per module: first sentence, counts), --docstrings (first "
|
|
210
|
+
"sentence only), --module NAME (its record, rendered), --names NAME (its public names "
|
|
211
|
+
"with kinds), --entry-points (with targets), --external, --imports NAME (what it "
|
|
212
|
+
"imports and what imports it)"
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def overview(summary: list[str]) -> list[str]:
|
|
217
|
+
"""What `systemap facts` prints with no option: the extract summary and the views."""
|
|
218
|
+
return [*summary, VIEWS]
|
systemap/figure.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"""Emit a figure of the system for embedding in a lesson or a document.
|
|
2
|
+
|
|
3
|
+
A lesson needs a picture of where a change landed, or of what a plan will
|
|
4
|
+
reach. The map already draws that picture. Drawing a second one by hand
|
|
5
|
+
would produce a rival diagram of the same system, and the hand-drawn one
|
|
6
|
+
would drift the moment a component moved. So this runs the same generator on
|
|
7
|
+
the same data and wraps the result in a figure.
|
|
8
|
+
|
|
9
|
+
The picture is therefore a citation, not a restatement: it is regenerated
|
|
10
|
+
from the facts every time, and it cannot disagree with the map it links to.
|
|
11
|
+
|
|
12
|
+
Three sources for what is marked:
|
|
13
|
+
|
|
14
|
+
base and head refs what a git range changed (mode change)
|
|
15
|
+
component ids what a plan reaches, no git consulted (mode change)
|
|
16
|
+
mode system nothing marked: the plain system figure
|
|
17
|
+
|
|
18
|
+
And one reading or all of them: `layer` draws only the edges the page's
|
|
19
|
+
layer switch shows for that layer (its own filter, `model.reading`), with
|
|
20
|
+
every card, the legend reduced to that layer and the layer's question as
|
|
21
|
+
the title. Structure has no edges at all; the whole map with every layer
|
|
22
|
+
at once is too many arrows for a document, and a reading is the page's
|
|
23
|
+
own answer to that.
|
|
24
|
+
|
|
25
|
+
An interactive figure carries the map's focus interaction as a
|
|
26
|
+
self-contained fragment: clicking a component dims the rest, thickens its
|
|
27
|
+
edges in their layer colours with their labels, tags each neighbour with the
|
|
28
|
+
verb that relates it, frames the component and its neighbours, and draws
|
|
29
|
+
the relationship wheel in a panel under the figure. The figure opens at Fit
|
|
30
|
+
and pans and zooms like the map page (wheel, pinch, drag, Fit / 100% / +
|
|
31
|
+
/ -, Escape to go back). Plain DOM, no libraries.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
from __future__ import annotations
|
|
35
|
+
|
|
36
|
+
import json
|
|
37
|
+
import re
|
|
38
|
+
from collections.abc import Mapping
|
|
39
|
+
from typing import Any
|
|
40
|
+
|
|
41
|
+
from systemap import change as change_mod
|
|
42
|
+
from systemap import nest
|
|
43
|
+
from systemap.config import Config, ConfigError, Figure
|
|
44
|
+
from systemap.model import Layer, Meaning, Model, all_layers
|
|
45
|
+
from systemap.schematic import interactive_script, kind_rows, layer_rows, legend_rows, panel_css
|
|
46
|
+
from systemap.schematic import render as render_schematic
|
|
47
|
+
|
|
48
|
+
GENERATOR = "systemap"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class FigureError(Exception):
|
|
52
|
+
"""The figure cannot be drawn as asked; the message says why."""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def bare_svg(svg: str, t: dict[str, Any]) -> str:
|
|
56
|
+
"""The drawing alone, on its ground, for embedding as an image.
|
|
57
|
+
|
|
58
|
+
The scene draws no background of its own because the figure element
|
|
59
|
+
and the page supply one. An `<img>` on someone else's page supplies
|
|
60
|
+
nothing, so a ground rectangle the size of the viewBox is put behind
|
|
61
|
+
the drawing. Nothing else changes: the same element, the same style,
|
|
62
|
+
the same text at the same size.
|
|
63
|
+
"""
|
|
64
|
+
match = re.search(r'viewBox="([-\d.]+) ([-\d.]+) ([-\d.]+) ([-\d.]+)"', svg)
|
|
65
|
+
if match is None:
|
|
66
|
+
return svg
|
|
67
|
+
x, y, w, h = match.groups()
|
|
68
|
+
end = svg.index(">") + 1
|
|
69
|
+
ground = f'<rect x="{x}" y="{y}" width="{w}" height="{h}" fill="{t["bg"]}"/>'
|
|
70
|
+
return svg[:end] + ground + svg[end:] + "\n"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
MARK_STYLE = {
|
|
74
|
+
"ring": "box-shadow:inset 0 0 0 1.5px {bg},inset 0 0 0 2.5px {ink}",
|
|
75
|
+
"notch": "background-image:linear-gradient(135deg,{ink} 0 38%,transparent 38%)",
|
|
76
|
+
"dotted": "border-style:dotted",
|
|
77
|
+
# A card that opens a map stands on a second card.
|
|
78
|
+
"map": "box-shadow:2px 2px 0 0 {ink}",
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def figure(
|
|
83
|
+
t: dict[str, Any],
|
|
84
|
+
model: Model,
|
|
85
|
+
meaning: Meaning,
|
|
86
|
+
svg: str,
|
|
87
|
+
caption: str,
|
|
88
|
+
legend: list[tuple[str, str, str]],
|
|
89
|
+
svg_id: str,
|
|
90
|
+
detail_json: str | None,
|
|
91
|
+
layer: str = "",
|
|
92
|
+
) -> str:
|
|
93
|
+
"""The figure element. A detail JSON makes it interactive; a layer id
|
|
94
|
+
reduces the line legend to that one reading."""
|
|
95
|
+
swatches = "".join(
|
|
96
|
+
f'<span style="display:inline-flex;align-items:center;gap:.4em;'
|
|
97
|
+
f'margin-right:1.1em;white-space:nowrap">'
|
|
98
|
+
f'<span style="width:.75em;height:.75em;border-radius:2px;'
|
|
99
|
+
f"background:{fill};border:1px solid {stroke};"
|
|
100
|
+
f'display:inline-block"></span>{label}</span>'
|
|
101
|
+
for fill, stroke, label in legend
|
|
102
|
+
)
|
|
103
|
+
swatches += "".join(
|
|
104
|
+
f'<span style="display:inline-flex;align-items:center;gap:.4em;'
|
|
105
|
+
f'margin-right:1.1em;white-space:nowrap">'
|
|
106
|
+
f'<span style="width:.75em;height:.75em;border-radius:2px;'
|
|
107
|
+
f"background:{t['state']['built'][0]};border:1px solid {t['state']['built'][1]};"
|
|
108
|
+
f"{MARK_STYLE[mark].format(bg=t['state']['built'][0], ink=t['state']['built'][1])};"
|
|
109
|
+
f'display:inline-block"></span>{kind}</span>'
|
|
110
|
+
for kind, mark in kind_rows(t, model)
|
|
111
|
+
)
|
|
112
|
+
if model.opening:
|
|
113
|
+
swatches += (
|
|
114
|
+
f'<span style="display:inline-flex;align-items:center;gap:.4em;'
|
|
115
|
+
f'margin-right:1.1em;white-space:nowrap">'
|
|
116
|
+
f'<span style="width:.75em;height:.75em;border-radius:2px;'
|
|
117
|
+
f"background:{t['state']['built'][0]};border:1px solid {t['state']['built'][1]};"
|
|
118
|
+
f"{MARK_STYLE['map'].format(bg=t['state']['built'][0], ink=t['state']['built'][1])};"
|
|
119
|
+
f'display:inline-block"></span>has a map</span>'
|
|
120
|
+
)
|
|
121
|
+
swatches += "".join(
|
|
122
|
+
f'<span style="display:inline-flex;align-items:center;gap:.4em;'
|
|
123
|
+
f'margin-right:1.1em;white-space:nowrap">'
|
|
124
|
+
f'<span style="width:1em;height:3px;border-radius:2px;background:{colour};'
|
|
125
|
+
f'display:inline-block"></span>{label}</span>'
|
|
126
|
+
for lid, colour, label in layer_rows(t, model, meaning)
|
|
127
|
+
if not layer or lid == layer
|
|
128
|
+
)
|
|
129
|
+
if layer != "structure":
|
|
130
|
+
swatches += (
|
|
131
|
+
f'<span style="display:inline-flex;align-items:center;gap:.4em;'
|
|
132
|
+
f'margin-right:1.1em;white-space:nowrap">'
|
|
133
|
+
f'<span style="width:1em;height:0;border-top:2px dashed {t["ink_3"]};'
|
|
134
|
+
f'display:inline-block"></span>declared</span>'
|
|
135
|
+
)
|
|
136
|
+
controls = ""
|
|
137
|
+
panel = ""
|
|
138
|
+
script = ""
|
|
139
|
+
hint = ""
|
|
140
|
+
if detail_json is not None:
|
|
141
|
+
panel_id = f"{svg_id}-panel"
|
|
142
|
+
btn = (
|
|
143
|
+
'style="font:inherit;color:inherit;background:none;border:1px solid '
|
|
144
|
+
f"{t['line']};border-radius:4px;min-height:24px;min-width:2.2em;"
|
|
145
|
+
'padding:0 .5em;cursor:pointer"'
|
|
146
|
+
)
|
|
147
|
+
controls = (
|
|
148
|
+
f'<span style="display:inline-flex;gap:.3em;margin-left:1em;white-space:nowrap">'
|
|
149
|
+
f'<button type="button" data-zoom="fit" data-for="{svg_id}" {btn}>Fit</button>'
|
|
150
|
+
f'<button type="button" data-zoom="actual" data-for="{svg_id}" {btn}>100%</button>'
|
|
151
|
+
f'<button type="button" data-zoom="in" data-for="{svg_id}" {btn} '
|
|
152
|
+
f'aria-label="Zoom in">+</button>'
|
|
153
|
+
f'<button type="button" data-zoom="out" data-for="{svg_id}" {btn} '
|
|
154
|
+
f'aria-label="Zoom out">-</button></span>'
|
|
155
|
+
)
|
|
156
|
+
hint = (
|
|
157
|
+
f'<p style="margin:.4em 0 0;font-size:.76rem;color:{t["ink_3"]}">Scroll to zoom, '
|
|
158
|
+
"drag to pan, click a component to frame it, Escape to go back.</p>"
|
|
159
|
+
)
|
|
160
|
+
panel = (
|
|
161
|
+
f'<div id="{panel_id}" class="systemap-panel" style="margin-top:.9em" '
|
|
162
|
+
f'aria-live="polite"></div>'
|
|
163
|
+
)
|
|
164
|
+
script = (
|
|
165
|
+
f"<style>{panel_css(t)}</style>"
|
|
166
|
+
+ interactive_script(t, svg_id, panel_id, detail_json)
|
|
167
|
+
+ "<script>(function(){"
|
|
168
|
+
f'var svg = document.getElementById("{svg_id}");'
|
|
169
|
+
"if(!svg || !svg.systemap){ return; }"
|
|
170
|
+
f"Array.prototype.slice.call(document.querySelectorAll('[data-for=\"{svg_id}\"]'))"
|
|
171
|
+
".forEach(function(b){ b.addEventListener('click', function(){"
|
|
172
|
+
"var z = b.dataset.zoom, v = svg.systemap.view;"
|
|
173
|
+
"if(z === 'fit'){ v.fit(); } else if(z === 'actual'){ v.actual(); }"
|
|
174
|
+
"else { v.zoomBy(z === 'in' ? 1.25 : 1 / 1.25); } }); });"
|
|
175
|
+
"document.addEventListener('keydown', function(e){"
|
|
176
|
+
"if(e.key === 'Escape'){ svg.systemap.clear(); svg.systemap.view.back(); } });"
|
|
177
|
+
"})();</script>"
|
|
178
|
+
)
|
|
179
|
+
# Legend labels and node names are generated, so a prose linter should
|
|
180
|
+
# skip the figure rather than judge text nobody wrote by hand. The figure
|
|
181
|
+
# opens at Fit, the whole map across the column; text is drawn at 11px
|
|
182
|
+
# and zoom brings it back to size.
|
|
183
|
+
return (
|
|
184
|
+
f'<figure data-generated="systemap" '
|
|
185
|
+
f'style="margin:2.4em 0;padding:1.1em 1.1em .9em;'
|
|
186
|
+
f"background:{t['bg']};border:1px solid {t['line']};border-radius:8px;"
|
|
187
|
+
f'overflow-x:auto;color:{t["ink_2"]};font-family:{t["font_ui"]}">'
|
|
188
|
+
f"{svg}{hint}"
|
|
189
|
+
f'<div style="margin-top:.9em;font-size:.78rem;line-height:1.9;'
|
|
190
|
+
f'color:{t["ink_3"]}">{swatches}{controls}</div>'
|
|
191
|
+
f"{panel}"
|
|
192
|
+
f'<figcaption style="margin-top:.7em;font-size:.82rem;line-height:1.5;'
|
|
193
|
+
f'color:{t["ink_3"]}">{caption}</figcaption>'
|
|
194
|
+
f"{script}"
|
|
195
|
+
f"</figure>\n"
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def make(
|
|
200
|
+
cfg: Config,
|
|
201
|
+
model: Model,
|
|
202
|
+
meaning: Meaning,
|
|
203
|
+
t: dict[str, Any],
|
|
204
|
+
facts: dict[str, Any],
|
|
205
|
+
*,
|
|
206
|
+
mode: str = "",
|
|
207
|
+
components: tuple[str, ...] = (),
|
|
208
|
+
base: str = "",
|
|
209
|
+
head: str = "HEAD",
|
|
210
|
+
caption: str = "",
|
|
211
|
+
svg_id: str = "lessonmap",
|
|
212
|
+
interactive: bool = False,
|
|
213
|
+
bare: bool = False,
|
|
214
|
+
layer: str = "",
|
|
215
|
+
map_id: str = "",
|
|
216
|
+
opens: Mapping[str, Mapping[str, Any]] | None = None,
|
|
217
|
+
) -> tuple[str, list[str]]:
|
|
218
|
+
"""(the figure HTML, the label and header problems the drawing reported).
|
|
219
|
+
|
|
220
|
+
`mode` is "system" or "change"; empty picks "change" when a base ref or
|
|
221
|
+
component ids are given and "system" otherwise. Unknown component ids
|
|
222
|
+
are a ConfigError; an empty git range is a FigureError. `bare` returns
|
|
223
|
+
the SVG alone, on its ground, instead of the figure element. `layer`
|
|
224
|
+
is one reading's id; an id the page does not have is a ConfigError
|
|
225
|
+
naming the ones it does. `map_id` names the map inside a card when
|
|
226
|
+
the figure draws one (the caption cites its page), and `opens` is
|
|
227
|
+
what the panel prints for each card that opens a map.
|
|
228
|
+
"""
|
|
229
|
+
page_url = f"{cfg.out_dir}/{map_id}/index.html" if map_id else f"{cfg.out_dir}/index.html"
|
|
230
|
+
facts_url = f"{cfg.out_dir}/{cfg.facts_file}"
|
|
231
|
+
inside = f"Inside {map_id.rpartition('/')[2]}: " if map_id else ""
|
|
232
|
+
mode = mode or ("change" if (base or components) else "system")
|
|
233
|
+
known = model.ids
|
|
234
|
+
reading = _reading(model, meaning, layer)
|
|
235
|
+
|
|
236
|
+
changed: set[str] = set()
|
|
237
|
+
changed_modules: set[str] = set()
|
|
238
|
+
adjacent: set[str] = set()
|
|
239
|
+
gained: dict[str, dict[str, int]] = {}
|
|
240
|
+
hot: set[str] = set()
|
|
241
|
+
legend_mode = "system"
|
|
242
|
+
|
|
243
|
+
if mode == "change" and components:
|
|
244
|
+
ids = [s.strip() for s in components if s.strip()]
|
|
245
|
+
unknown = sorted(set(ids) - known)
|
|
246
|
+
if unknown:
|
|
247
|
+
raise ConfigError(f"unknown component ids: {', '.join(unknown)}")
|
|
248
|
+
changed = set(ids)
|
|
249
|
+
legend_mode = "reach"
|
|
250
|
+
caption = caption or (
|
|
251
|
+
f"{inside}The system, with the {len(changed)} components a plan reaches as "
|
|
252
|
+
f"the figure. This marks a plan's reach, not a diff. Drawn by "
|
|
253
|
+
f"<code>{GENERATOR}</code>, the same generator the map uses, so "
|
|
254
|
+
f"this cannot disagree with <code>{page_url}</code>."
|
|
255
|
+
)
|
|
256
|
+
elif mode == "change":
|
|
257
|
+
if not base:
|
|
258
|
+
raise FigureError("a base ref is required for a change figure")
|
|
259
|
+
ch = change_mod.compute(cfg, model, base, facts, head)
|
|
260
|
+
if not ch["has_change"]:
|
|
261
|
+
raise FigureError(f"no change between {base} and {head}: nothing to draw")
|
|
262
|
+
changed = ch["direct"]
|
|
263
|
+
changed_modules = ch["modules"]
|
|
264
|
+
adjacent = ch["adjacent"]
|
|
265
|
+
gained = {k: v["gained"] for k, v in ch["per_component"].items()}
|
|
266
|
+
hot = ch["flow_artifacts"]
|
|
267
|
+
legend_mode = "change"
|
|
268
|
+
caption = caption or (
|
|
269
|
+
f"{inside}The system, with this change as the figure. Drawn by "
|
|
270
|
+
f"<code>{GENERATOR}</code>, the same generator the map uses, so "
|
|
271
|
+
f"this cannot disagree with <code>{page_url}</code>."
|
|
272
|
+
)
|
|
273
|
+
elif reading is not None:
|
|
274
|
+
sub = reading.sub[:1].upper() + reading.sub[1:] if reading.sub else ""
|
|
275
|
+
caption = caption or (
|
|
276
|
+
f"{inside}{reading.label}: {reading.question} {sub + '. ' if sub else ''}"
|
|
277
|
+
f"One reading of the system; the page at <code>{page_url}</code> has them "
|
|
278
|
+
f"all. Drawn by <code>{GENERATOR}</code> from <code>{facts_url}</code>; "
|
|
279
|
+
f"every card is code in the tree today."
|
|
280
|
+
)
|
|
281
|
+
else:
|
|
282
|
+
caption = caption or (
|
|
283
|
+
f"{inside}The system as the map describes it. Drawn by <code>{GENERATOR}</code> "
|
|
284
|
+
f"from <code>{facts_url}</code>; every card is code in the tree today. "
|
|
285
|
+
f"Click a component to read what it is to its neighbours."
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
svg, detail = render_schematic(
|
|
289
|
+
model,
|
|
290
|
+
meaning,
|
|
291
|
+
t,
|
|
292
|
+
facts,
|
|
293
|
+
changed=changed,
|
|
294
|
+
changed_modules=changed_modules,
|
|
295
|
+
adjacent=adjacent,
|
|
296
|
+
mode=mode,
|
|
297
|
+
svg_id=svg_id,
|
|
298
|
+
gained=gained,
|
|
299
|
+
hot_artifacts=hot,
|
|
300
|
+
layer=layer,
|
|
301
|
+
observed_by=cfg.observed_by,
|
|
302
|
+
opens=opens,
|
|
303
|
+
)
|
|
304
|
+
meta = json.loads(detail).get("_meta", {})
|
|
305
|
+
collisions: list[str] = list(meta.get("collisions", []))
|
|
306
|
+
if bare:
|
|
307
|
+
return bare_svg(svg, t), collisions
|
|
308
|
+
|
|
309
|
+
if legend_mode == "reach":
|
|
310
|
+
rows = [
|
|
311
|
+
(legend_rows(t, "change")[0][0], t["change"], "in the plan's reach"),
|
|
312
|
+
(t["ghost"][0], t["ghost"][1], "not in the plan"),
|
|
313
|
+
]
|
|
314
|
+
else:
|
|
315
|
+
rows = legend_rows(t, legend_mode)
|
|
316
|
+
out = figure(
|
|
317
|
+
t, model, meaning, svg, caption, rows, svg_id, detail if interactive else None, layer
|
|
318
|
+
)
|
|
319
|
+
return out, collisions
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _reading(model: Model, meaning: Meaning, layer: str) -> Layer | None:
|
|
323
|
+
"""The layer a figure is restricted to, or None for the whole map.
|
|
324
|
+
|
|
325
|
+
The ids are the page's: the standard readings, the agent readings when
|
|
326
|
+
the model has an agent, then the model's own. A wrong id is refused
|
|
327
|
+
with the right ones named, since a figure of a reading that is not on
|
|
328
|
+
the page would be a picture the page cannot back.
|
|
329
|
+
"""
|
|
330
|
+
if not layer:
|
|
331
|
+
return None
|
|
332
|
+
layers = all_layers(model, meaning)
|
|
333
|
+
for lay in layers:
|
|
334
|
+
if lay.id == layer:
|
|
335
|
+
return lay
|
|
336
|
+
raise ConfigError(
|
|
337
|
+
f"unknown layer id: {layer}; the readings the page has are "
|
|
338
|
+
f"{', '.join(lay.id for lay in layers)}"
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def configured(
|
|
343
|
+
cfg: Config,
|
|
344
|
+
tree: nest.Tree,
|
|
345
|
+
m: nest.Map,
|
|
346
|
+
facts: dict[str, Any],
|
|
347
|
+
fig: Figure,
|
|
348
|
+
) -> tuple[str, list[str]]:
|
|
349
|
+
"""One figure from the configuration's `[[figures]]` table, of the map `m`.
|
|
350
|
+
|
|
351
|
+
`systemap refresh` writes it and `systemap check` compares it, through
|
|
352
|
+
this one function, so the two cannot disagree about what the file
|
|
353
|
+
should hold. An `out` ending in `.svg` is the bare drawing.
|
|
354
|
+
"""
|
|
355
|
+
return make(
|
|
356
|
+
cfg,
|
|
357
|
+
m.model,
|
|
358
|
+
m.meaning,
|
|
359
|
+
m.theme,
|
|
360
|
+
facts,
|
|
361
|
+
mode="change" if fig.mode == "reach" else "system",
|
|
362
|
+
components=fig.components,
|
|
363
|
+
caption=fig.caption,
|
|
364
|
+
svg_id=fig.svg_id,
|
|
365
|
+
interactive=fig.interactive,
|
|
366
|
+
bare=fig.out.endswith(".svg"),
|
|
367
|
+
layer=fig.layer,
|
|
368
|
+
map_id=m.id,
|
|
369
|
+
opens=nest.opens(tree, m, links=False),
|
|
370
|
+
)
|