Manage kits: reconcile the schema on save, and a dialog to repin builds
Placing a palette component no longer edits <schema> straight away. The kit is noted as pending, its manifest is compiled so the box can be painted, and the status bar says the kit will be declared on save. The <schema> block is then reconciled when the file is written, in the one place that can report the whole kit list the file will carry, with each kit's version and checksum. What a kit's manifest depends on counts as used too, transitively, so placing a component can bring in more than one entry: hvac pulls in types, ontrolPion pulls in ontrolPlatform. New Kits > Manage Kits... dialog lists every kit in three groups -- needed by the app, declared but unused, and merely installed -- with the pinned build, the newest installed build, and an Action dropdown of every build a kit ships. The tick means the schema declares the kit: grey and fixed for kits the app needs, unticked to remove, ticked to add at the newest build. "Upgrade all kits to latest version" moves every declared kit; "Update kits" applies the ticks and the Action column, additions and removals included. Every schema change is applied as a batch, then verified: a missing manifest or a component type that stops resolving reverts the whole block and reports why, and a kit the app needs cannot be removed. Changes are one undo step, which recompiles the slot registry in both directions since a repin can change a component's slots. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+480
@@ -0,0 +1,480 @@
|
||||
"""The Manage Kits dialog: what the app pins, what Sedona home offers, and repinning.
|
||||
|
||||
The table answers two questions per kit — which build the app is pinned to, and which
|
||||
builds exist — and the Action column is where a different build is chosen. Nothing is
|
||||
written to the file until one of the buttons is pressed, so the whole table can be set up
|
||||
first and reviewed as a batch.
|
||||
|
||||
Rows come in three groups, in this order: kits the app needs — named by a component or
|
||||
depended on by a kit that is — then kits the schema declares without needing, then
|
||||
everything else installed in Sedona home.
|
||||
|
||||
The tick in the first column means "the schema declares this kit". A kit the app needs is
|
||||
ticked and greyed, because removing it would stop the app running; one that is merely
|
||||
declared can be unticked, which turns its Action into REMOVE. An installed kit that is not
|
||||
declared starts unticked, and ticking it adds it at its newest build.
|
||||
"""
|
||||
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox, ttk
|
||||
|
||||
import app_state
|
||||
import palette
|
||||
import sax_file
|
||||
|
||||
NOT_DECLARED = "—" # em dash, for a column with nothing to report
|
||||
NO_CHECKSUM = "(no checksum)"
|
||||
REMOVE_ACTION = "REMOVE from app"
|
||||
|
||||
SECTION_TAG = "section"
|
||||
|
||||
CHECKBOX_SIZE = 13
|
||||
|
||||
# The tick, as filled 2x2 blocks: a short down-stroke into a long up-stroke.
|
||||
TICK_BLOCKS = [
|
||||
(2, 5), (3, 6), (4, 7), (5, 6), (6, 5), (7, 4), (8, 3), (9, 2),
|
||||
]
|
||||
|
||||
|
||||
def make_checkbox_image(checked, enabled):
|
||||
"""A checkbox drawn pixel by pixel, so a locked tick can be greyed out.
|
||||
|
||||
Tk has no disabled state for a cell, and a row tag would grey the whole row including
|
||||
an Action the user can still change — so the box itself carries the distinction.
|
||||
"""
|
||||
image = tk.PhotoImage(width=CHECKBOX_SIZE, height=CHECKBOX_SIZE)
|
||||
|
||||
face = "#ffffff" if enabled else "#f0f0f0"
|
||||
border = "#555555" if enabled else "#a8a8a8"
|
||||
ink = "#1a1a1a" if enabled else "#909090"
|
||||
|
||||
image.put(face, to=(0, 0, CHECKBOX_SIZE, CHECKBOX_SIZE))
|
||||
image.put(border, to=(0, 0, CHECKBOX_SIZE, 1))
|
||||
image.put(border, to=(0, CHECKBOX_SIZE - 1, CHECKBOX_SIZE, CHECKBOX_SIZE))
|
||||
image.put(border, to=(0, 0, 1, CHECKBOX_SIZE))
|
||||
image.put(border, to=(CHECKBOX_SIZE - 1, 0, CHECKBOX_SIZE, CHECKBOX_SIZE))
|
||||
|
||||
if checked:
|
||||
for x, y in TICK_BLOCKS:
|
||||
image.put(ink, to=(x, y, x + 2, y + 2))
|
||||
|
||||
return image
|
||||
|
||||
|
||||
def build_label(version, checksum):
|
||||
"""How a build is named in a sentence: its version, or its checksum when it has none."""
|
||||
return version if version else f"checksum {checksum}"
|
||||
|
||||
|
||||
def column_label(version, checksum):
|
||||
"""How a build is named in the Installed and Latest columns, which are narrow."""
|
||||
return version if version else checksum
|
||||
|
||||
|
||||
def action_options(kit_name, current_checksum, is_declared, builds):
|
||||
"""The Action dropdown for one kit: (label, checksum to apply) pairs, no-op first.
|
||||
|
||||
The no-op comes first so a row does nothing until the user picks something, and the
|
||||
remaining entries read as upgrade or downgrade relative to the pinned build — which is
|
||||
what makes the column scannable when a kit ships thirty builds.
|
||||
"""
|
||||
installed_version = None
|
||||
for version, checksum in builds:
|
||||
if checksum == current_checksum:
|
||||
installed_version = version
|
||||
break
|
||||
|
||||
if not is_declared:
|
||||
options = [("Not in schema", None)]
|
||||
elif current_checksum is None:
|
||||
options = [("Keep with no checksum", None)]
|
||||
elif installed_version is None:
|
||||
# The file pins a build this machine does not hold, so there is nothing to name it
|
||||
# by and no way to tell whether the other builds are newer or older than it.
|
||||
options = [(f"Keep at checksum {current_checksum}", None)]
|
||||
else:
|
||||
options = [(f"Keep at {build_label(installed_version, current_checksum)}", None)]
|
||||
|
||||
installed_rank = palette.version_sort_key(installed_version) if installed_version else None
|
||||
|
||||
for version, checksum in builds:
|
||||
if is_declared and checksum == current_checksum:
|
||||
continue
|
||||
|
||||
label = build_label(version, checksum)
|
||||
if not is_declared:
|
||||
verb = "Add at"
|
||||
elif installed_rank is None or not version:
|
||||
verb = "Set to"
|
||||
elif palette.version_sort_key(version) > installed_rank:
|
||||
verb = "Upgrade to"
|
||||
elif palette.version_sort_key(version) < installed_rank:
|
||||
verb = "Downgrade to"
|
||||
else:
|
||||
verb = "Set to"
|
||||
options.append((f"{verb} {label}", checksum))
|
||||
|
||||
return options
|
||||
|
||||
|
||||
def collect_rows():
|
||||
"""Every kit worth listing, as row dicts in the order they should appear.
|
||||
|
||||
A row carries what the table paints and what the buttons act on, so the dialog never
|
||||
has to re-derive a kit's state from the DOM while it is open.
|
||||
"""
|
||||
declared = sax_file.schema_kit_checksums()
|
||||
declared_names = [name for name, _ in declared]
|
||||
required = sax_file.required_kit_names()
|
||||
|
||||
installed = palette.list_kits()
|
||||
# A kit the schema declares but Sedona home does not hold still gets a row: it is the
|
||||
# clearest place to see that the file points at something missing.
|
||||
others = [name for name in installed if name not in declared_names]
|
||||
|
||||
groups = [
|
||||
("Installed kits", [
|
||||
(name, checksum) for name, checksum in declared if name in required
|
||||
]),
|
||||
("Installed but not used kits", [
|
||||
(name, checksum) for name, checksum in declared if name not in required
|
||||
]),
|
||||
("Kits to be installed", [(name, None) for name in others]),
|
||||
]
|
||||
|
||||
rows = []
|
||||
for title, entries in groups:
|
||||
if not entries:
|
||||
continue
|
||||
rows.append({"section": title})
|
||||
for name, checksum in entries:
|
||||
builds = palette.kit_builds(name)
|
||||
latest = builds[0] if builds else None
|
||||
is_declared = name in declared_names
|
||||
options = action_options(name, checksum, is_declared, builds)
|
||||
|
||||
if not is_declared:
|
||||
installed_text = NOT_DECLARED
|
||||
elif checksum is None:
|
||||
installed_text = NO_CHECKSUM
|
||||
else:
|
||||
installed_text = column_label(
|
||||
next((v for v, c in builds if c == checksum), ""), checksum
|
||||
)
|
||||
|
||||
rows.append({
|
||||
"kit": name,
|
||||
"declared": is_declared,
|
||||
"required": name in required,
|
||||
"checksum": checksum,
|
||||
"builds": builds,
|
||||
"latest": latest,
|
||||
"installed_text": installed_text,
|
||||
"latest_text": column_label(*latest) if latest else NOT_DECLARED,
|
||||
"options": options,
|
||||
"choice": 0,
|
||||
# The tick reflects the file as it stands: declared kits start ticked.
|
||||
"checked": is_declared,
|
||||
})
|
||||
return rows
|
||||
|
||||
|
||||
def describe_changes(changes):
|
||||
"""The applied-changes report, one kit per line, old build to new."""
|
||||
lines = []
|
||||
for kit_name, old_checksum, new_checksum in changes:
|
||||
old_label = (
|
||||
build_label(palette.version_for_checksum(kit_name, old_checksum), old_checksum)
|
||||
if old_checksum else None
|
||||
)
|
||||
|
||||
if new_checksum is None:
|
||||
was = f" (was {old_label})" if old_label else ""
|
||||
lines.append(f" • {kit_name}: removed from the schema{was}")
|
||||
continue
|
||||
|
||||
new_label = build_label(
|
||||
palette.version_for_checksum(kit_name, new_checksum), new_checksum
|
||||
)
|
||||
if old_checksum is None:
|
||||
lines.append(f" • {kit_name}: added at {new_label}")
|
||||
else:
|
||||
lines.append(f" • {kit_name}: {old_label} → {new_label}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def open_dialog(on_applied=None):
|
||||
"""Opens the kit table. ``on_applied`` is called after the schema really changed."""
|
||||
window = app_state.window
|
||||
|
||||
if app_state.xml_root_element is None:
|
||||
messagebox.showinfo("Manage Kits", "Open a SAX file first.")
|
||||
return
|
||||
if app_state.xml_root_element.find("schema") is None:
|
||||
messagebox.showwarning("Manage Kits", "This file has no <schema> block to manage.")
|
||||
return
|
||||
|
||||
dialog = tk.Toplevel(window)
|
||||
dialog.title("Kits")
|
||||
dialog.transient(window)
|
||||
dialog.grab_set()
|
||||
dialog.geometry(f"820x520+{window.winfo_x() + 60}+{window.winfo_y() + 40}")
|
||||
dialog.minsize(640, 320)
|
||||
|
||||
frame = ttk.Frame(dialog, padding=10)
|
||||
frame.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
COLUMNS = ("name", "installed", "latest", "action")
|
||||
# 'tree headings' keeps the icon column, which is the only place a real checkbox image
|
||||
# can live; the data columns follow it.
|
||||
table = ttk.Treeview(frame, columns=COLUMNS, show="tree headings", selectmode="browse")
|
||||
table.heading("#0", text="")
|
||||
table.column("#0", width=32, minwidth=32, stretch=False, anchor="center")
|
||||
for column, heading, width, anchor in [
|
||||
("name", "Kit name", 200, "w"),
|
||||
("installed", "Installed", 105, "w"),
|
||||
("latest", "Latest", 105, "w"),
|
||||
("action", "Action", 300, "w"),
|
||||
]:
|
||||
table.heading(column, text=heading, anchor=anchor)
|
||||
table.column(column, width=width, anchor=anchor, stretch=(column == "action"))
|
||||
|
||||
scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=table.yview)
|
||||
table.configure(yscrollcommand=scrollbar.set)
|
||||
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
|
||||
table.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
table.tag_configure(SECTION_TAG, background="#dcdcdc")
|
||||
|
||||
# Held on the dialog: a PhotoImage is dropped as soon as nothing references it.
|
||||
boxes = {
|
||||
(True, True): make_checkbox_image(True, True),
|
||||
(False, True): make_checkbox_image(False, True),
|
||||
(True, False): make_checkbox_image(True, False),
|
||||
}
|
||||
|
||||
hint = ttk.Label(
|
||||
frame,
|
||||
text="A tick means the schema declares the kit. Grey ticks are needed by the app "
|
||||
"and cannot be removed; untick another to remove it. Click the Action cell to "
|
||||
"choose a build. Nothing reaches the file until you save it.",
|
||||
foreground="#666666",
|
||||
wraplength=780,
|
||||
)
|
||||
hint.pack(anchor="w", pady=(8, 0))
|
||||
|
||||
button_row = ttk.Frame(frame)
|
||||
button_row.pack(fill=tk.X, pady=(10, 0))
|
||||
|
||||
rows = [] # row dicts, table order
|
||||
row_by_item = {} # Treeview item id -> row dict
|
||||
editor = {"widget": None}
|
||||
|
||||
def close_editor(_event=None):
|
||||
if editor["widget"] is not None:
|
||||
editor["widget"].destroy()
|
||||
editor["widget"] = None
|
||||
|
||||
def action_text(row):
|
||||
"""What the Action cell says, which an untick overrides with REMOVE."""
|
||||
if row["declared"] and not row["checked"]:
|
||||
return REMOVE_ACTION
|
||||
return row["options"][row["choice"]][0]
|
||||
|
||||
def row_values(row):
|
||||
return (row["kit"], row["installed_text"], row["latest_text"], action_text(row))
|
||||
|
||||
def row_image(row):
|
||||
return boxes[(row["checked"], not row["required"])]
|
||||
|
||||
def rebuild_table():
|
||||
close_editor()
|
||||
for item in table.get_children():
|
||||
table.delete(item)
|
||||
row_by_item.clear()
|
||||
|
||||
rows[:] = collect_rows()
|
||||
for row in rows:
|
||||
if "section" in row:
|
||||
table.insert("", "end", values=(row["section"], "", "", ""), tags=(SECTION_TAG,))
|
||||
continue
|
||||
item = table.insert("", "end", image=row_image(row), values=row_values(row))
|
||||
row_by_item[item] = row
|
||||
|
||||
def refresh_row(item):
|
||||
row = row_by_item[item]
|
||||
table.item(item, image=row_image(row), values=row_values(row))
|
||||
|
||||
def set_choice(item, index):
|
||||
row = row_by_item[item]
|
||||
row["choice"] = index
|
||||
|
||||
# Naming a build is the user saying they want the kit at it, so the row ticks
|
||||
# itself — and for a declared row that also cancels a pending removal.
|
||||
if row["declared"] or row["options"][index][1] is not None:
|
||||
row["checked"] = True
|
||||
refresh_row(item)
|
||||
|
||||
def toggle_check(item):
|
||||
row = row_by_item[item]
|
||||
if row["required"]:
|
||||
# Needed by the app: the tick is fixed, and the box is drawn greyed to say so.
|
||||
return
|
||||
|
||||
row["checked"] = not row["checked"]
|
||||
if not row["declared"]:
|
||||
# Ticking a kit that is not in the schema means adding it, at its newest build
|
||||
# unless the user picks another; unticking puts the row back to doing nothing.
|
||||
row["choice"] = 1 if (row["checked"] and len(row["options"]) > 1) else 0
|
||||
if row["checked"] and len(row["options"]) < 2:
|
||||
row["checked"] = False
|
||||
messagebox.showwarning(
|
||||
"Manage Kits",
|
||||
f"Sedona home holds no manifest for '{row['kit']}', so there is no "
|
||||
"build to add it at.",
|
||||
parent=dialog
|
||||
)
|
||||
refresh_row(item)
|
||||
|
||||
def edit_action(item):
|
||||
row = row_by_item[item]
|
||||
cell = table.bbox(item, "action")
|
||||
if not cell:
|
||||
return
|
||||
|
||||
labels = [label for label, _ in row["options"]]
|
||||
current = action_text(row)
|
||||
variable = tk.StringVar(value=current if current in labels else labels[row["choice"]])
|
||||
combo = ttk.Combobox(table, textvariable=variable, values=labels, state="readonly")
|
||||
combo.place(x=cell[0], y=cell[1], width=cell[2], height=cell[3])
|
||||
editor["widget"] = combo
|
||||
|
||||
def commit(_event=None):
|
||||
if variable.get() in labels:
|
||||
set_choice(item, labels.index(variable.get()))
|
||||
close_editor()
|
||||
|
||||
combo.bind("<<ComboboxSelected>>", commit)
|
||||
combo.bind("<Escape>", lambda event: close_editor())
|
||||
combo.focus_set()
|
||||
# A single click on the cell should show the builds, not just a collapsed box.
|
||||
combo.after_idle(lambda: combo.event_generate("<Down>"))
|
||||
|
||||
def on_click(event):
|
||||
close_editor()
|
||||
if table.identify_region(event.x, event.y) not in ("tree", "cell"):
|
||||
return
|
||||
item = table.identify_row(event.y)
|
||||
if item not in row_by_item:
|
||||
return
|
||||
|
||||
column = table.identify_column(event.x)
|
||||
if column == "#0":
|
||||
toggle_check(item)
|
||||
elif column == "#4":
|
||||
edit_action(item)
|
||||
|
||||
table.bind("<Button-1>", on_click)
|
||||
table.bind("<MouseWheel>", close_editor, add="+")
|
||||
table.bind("<Configure>", close_editor, add="+")
|
||||
|
||||
def apply_changes(selections, removals, label, nothing_message, note=""):
|
||||
if not selections and not removals:
|
||||
messagebox.showinfo("Manage Kits", nothing_message, parent=dialog)
|
||||
return
|
||||
|
||||
changes, error = sax_file.apply_kit_selections(selections, removals, label=label)
|
||||
if error:
|
||||
messagebox.showerror("Kits unchanged", error, parent=dialog)
|
||||
rebuild_table()
|
||||
return
|
||||
|
||||
if not changes:
|
||||
messagebox.showinfo(
|
||||
"Manage Kits", "The schema already reads that way.", parent=dialog
|
||||
)
|
||||
return
|
||||
|
||||
rebuild_table()
|
||||
if on_applied is not None:
|
||||
on_applied()
|
||||
|
||||
app_state.set_status(
|
||||
f"schema kits changed ({len(changes)}) — save the file to keep it.",
|
||||
warn=True
|
||||
)
|
||||
messagebox.showinfo(
|
||||
"Kits updated",
|
||||
f"The schema block now reads:\n\n{describe_changes(changes)}\n\n"
|
||||
+ (note + "\n\n" if note else "")
|
||||
+ "Save the file to write this to disk. The device's scode must hold these "
|
||||
"builds or the application will not run.",
|
||||
parent=dialog
|
||||
)
|
||||
|
||||
def upgrade_all():
|
||||
"""Every kit the schema declares, moved to the newest build Sedona home holds."""
|
||||
selections = []
|
||||
for row in rows:
|
||||
if "section" in row or not row["declared"] or not row["latest"]:
|
||||
continue
|
||||
latest_checksum = row["latest"][1]
|
||||
if latest_checksum != row["checksum"]:
|
||||
selections.append((row["kit"], latest_checksum))
|
||||
|
||||
if selections:
|
||||
preview = "\n".join(
|
||||
f" • {kit}: {build_label(palette.version_for_checksum(kit, checksum), checksum)}"
|
||||
for kit, checksum in selections
|
||||
)
|
||||
proceed = messagebox.askokcancel(
|
||||
"Upgrade all kits",
|
||||
f"{len(selections)} kit{'' if len(selections) == 1 else 's'} in this app's "
|
||||
f"schema will move to their latest installed build:\n\n{preview}\n\nContinue?",
|
||||
icon=messagebox.WARNING,
|
||||
parent=dialog
|
||||
)
|
||||
if not proceed:
|
||||
return
|
||||
|
||||
apply_changes(
|
||||
selections, [],
|
||||
"upgrade all kits",
|
||||
"Every kit in the schema is already at its latest installed build."
|
||||
)
|
||||
|
||||
def update_marked():
|
||||
"""The ticks and the Action column, applied: repins, additions and removals."""
|
||||
selections = []
|
||||
removals = []
|
||||
for row in row_by_item.values():
|
||||
if row["declared"] and not row["checked"]:
|
||||
removals.append(row["kit"])
|
||||
continue
|
||||
if not row["checked"]:
|
||||
continue
|
||||
checksum = row["options"][row["choice"]][1]
|
||||
if checksum:
|
||||
selections.append((row["kit"], checksum))
|
||||
|
||||
note = ""
|
||||
if removals:
|
||||
note = (
|
||||
"Removed kits are gone from the schema only. Any build of them still "
|
||||
"installed in Sedona home can be added back here."
|
||||
)
|
||||
|
||||
apply_changes(
|
||||
selections, removals,
|
||||
"update kits",
|
||||
"Nothing is marked for a change. Pick a build in a kit's Action column, tick "
|
||||
"a kit to add it, or untick one to remove it."
|
||||
)
|
||||
|
||||
ttk.Button(
|
||||
button_row, text="Upgrade all kits to latest version", command=upgrade_all
|
||||
).pack(side=tk.LEFT)
|
||||
ttk.Button(button_row, text="Update kits", command=update_marked).pack(side=tk.LEFT, padx=6)
|
||||
ttk.Button(button_row, text="Close", command=dialog.destroy).pack(side=tk.RIGHT)
|
||||
|
||||
rebuild_table()
|
||||
Reference in New Issue
Block a user