Kit manager menu oncesi
This commit is contained in:
+202
-46
@@ -76,9 +76,11 @@ def reset_to_startup_state():
|
||||
app_state.canvas_link_map.clear()
|
||||
app_state.final_components_registry.clear()
|
||||
app_state.active_schema_kits.clear()
|
||||
app_state.pending_schema_kits.clear()
|
||||
|
||||
undo.reset()
|
||||
app_state.mark_clean()
|
||||
app_state.clear_status()
|
||||
|
||||
if app_state.tree is not None:
|
||||
for item in app_state.tree.get_children():
|
||||
@@ -110,6 +112,8 @@ def open_file():
|
||||
try:
|
||||
app_state.current_file_path = file_path
|
||||
app_state.active_components_list.clear()
|
||||
app_state.pending_schema_kits.clear()
|
||||
app_state.clear_status()
|
||||
app_state.selected_canvas_tag = None
|
||||
app_state.current_sheet_parent = None
|
||||
undo.reset()
|
||||
@@ -204,6 +208,9 @@ def execute_direct_overwrite_save():
|
||||
try:
|
||||
sync_live_metadata_to_dom()
|
||||
|
||||
if not reconcile_schema_kits_before_save():
|
||||
return
|
||||
|
||||
with open(app_state.current_file_path, "wb") as f:
|
||||
f.write(b'<?xml version="1.0" encoding="UTF-8"?>\n')
|
||||
ET.ElementTree(app_state.xml_root_element).write(f, encoding="utf-8", xml_declaration=False)
|
||||
@@ -211,6 +218,7 @@ def execute_direct_overwrite_save():
|
||||
app_state.mark_clean()
|
||||
undo.mark_saved()
|
||||
refresh_treeview_from_dom(app_state.xml_root_element)
|
||||
app_state.set_status(f"Saved {os.path.basename(app_state.current_file_path)}.")
|
||||
|
||||
messagebox.showinfo("Success", f"Direct changes saved onto:\n{os.path.basename(app_state.current_file_path)}")
|
||||
except Exception as e:
|
||||
@@ -250,6 +258,9 @@ def save_file_as():
|
||||
else:
|
||||
target_path = app_state.current_file_path
|
||||
|
||||
if not reconcile_schema_kits_before_save():
|
||||
return
|
||||
|
||||
with open(target_path, "wb") as f:
|
||||
f.write(b'<?xml version="1.0" encoding="UTF-8"?>\n')
|
||||
ET.ElementTree(app_state.xml_root_element).write(f, encoding="utf-8", xml_declaration=False)
|
||||
@@ -261,6 +272,7 @@ def save_file_as():
|
||||
|
||||
folder_name = os.path.basename(os.path.dirname(target_path))
|
||||
file_name = os.path.basename(target_path)
|
||||
app_state.set_status(f"Saved {file_name}.")
|
||||
|
||||
messagebox.showinfo("Success", f"File Saved\nFolder: {folder_name}\nFile: {file_name}")
|
||||
|
||||
@@ -316,44 +328,193 @@ def current_sheet_container():
|
||||
return app_element if app_element is not None else app_state.xml_root_element
|
||||
|
||||
|
||||
def ensure_kit_declared(kit_name):
|
||||
"""Adds a <kit> entry to <schema> when placing a component from an undeclared kit.
|
||||
def note_kit_used(kit_name):
|
||||
"""Records that a placed component needs a kit the file's <schema> does not declare.
|
||||
|
||||
The new entry carries the checksum of the manifest the palette resolved, so the file
|
||||
records exactly which kit build the component was authored against. Entries already in
|
||||
the file are left as they are — a missing checksum there is a deliberate tolerance on
|
||||
load, not something to backfill.
|
||||
Nothing is written to the DOM here. The <schema> block is reconciled when the file is
|
||||
saved, which is the one moment that can report the complete kit list the file will
|
||||
carry rather than interrupting every placement with a partial one.
|
||||
|
||||
Returns the element that was added, or None when the kit was already declared. A kit
|
||||
absent from the schema also has to exist in the device's scode, so the caller warns.
|
||||
Returns True when this call is what put the kit on the pending list.
|
||||
"""
|
||||
if app_state.xml_root_element is None:
|
||||
return None
|
||||
return False
|
||||
if kit_name in app_state.active_schema_kits:
|
||||
return False
|
||||
if kit_name in app_state.pending_schema_kits:
|
||||
return False
|
||||
|
||||
app_state.pending_schema_kits[kit_name] = palette.resolve_checksum(kit_name)
|
||||
return True
|
||||
|
||||
|
||||
def used_kit_names():
|
||||
"""Every kit the app's components need, taken from the type of each <comp>."""
|
||||
names = set()
|
||||
if app_state.xml_root_element is None:
|
||||
return names
|
||||
|
||||
for comp in app_state.xml_root_element.iter("comp"):
|
||||
type_str = comp.get("type") or ""
|
||||
if "::" in type_str:
|
||||
names.add(type_str.split("::", 1)[0])
|
||||
return names
|
||||
|
||||
|
||||
def declare_used_kits():
|
||||
"""Adds a <kit> entry for every used kit the <schema> block does not list.
|
||||
|
||||
The DOM is the authority on what is used, not the pending list, so a placement that
|
||||
was later undone cannot leave a kit declared for nothing. Pending entries only supply
|
||||
the checksum of the manifest the palette resolved when the component was placed.
|
||||
|
||||
Entries already in the file are left as they are — a missing checksum there is a
|
||||
deliberate tolerance on load, not something to backfill.
|
||||
|
||||
Returns (added elements in the order written, whitespace to put back if the caller
|
||||
rolls the insertion out).
|
||||
"""
|
||||
if app_state.xml_root_element is None:
|
||||
return [], []
|
||||
|
||||
schema_elem = app_state.xml_root_element.find("schema")
|
||||
if schema_elem is None:
|
||||
return [], []
|
||||
|
||||
declared = {kit.get("name") for kit in schema_elem.findall("kit")}
|
||||
|
||||
added = []
|
||||
tail_restores = []
|
||||
for name in sorted(used_kit_names() - declared):
|
||||
kit_element = ET.Element("kit")
|
||||
kit_element.set("name", name)
|
||||
|
||||
checksum = app_state.pending_schema_kits.get(name) or palette.resolve_checksum(name)
|
||||
if checksum:
|
||||
kit_element.set("checksum", checksum)
|
||||
|
||||
# Added after the last existing kit. The block is not alphabetical — sys comes
|
||||
# first by convention — so appending is safer than guessing a sort position.
|
||||
insert_at = len(list(schema_elem))
|
||||
for index, existing in enumerate(list(schema_elem)):
|
||||
if existing.tag == "kit":
|
||||
insert_at = index + 1
|
||||
schema_elem.insert(insert_at, kit_element)
|
||||
restore = indent_kit_entry(schema_elem, kit_element)
|
||||
if restore is not None:
|
||||
tail_restores.append(restore)
|
||||
|
||||
app_state.active_schema_kits[name] = checksum
|
||||
added.append(kit_element)
|
||||
|
||||
return added, tail_restores
|
||||
|
||||
|
||||
def indent_kit_entry(schema_elem, kit_element):
|
||||
"""Gives a freshly inserted <kit> the surrounding block's line break and indent.
|
||||
|
||||
ElementTree keeps whitespace in each element's ``tail``, so an inserted element lands
|
||||
flush against its neighbour unless the tails are handed on: the new entry takes over
|
||||
the tail that used to close the block, and the entry before it takes the tail that
|
||||
separates two kits.
|
||||
|
||||
Handing a tail on means writing to a neighbour, so the pair needed to put that
|
||||
neighbour back is returned for a caller that may have to undo the insertion. It is
|
||||
(element, tail) for a block that already held kits, and None for one that did not.
|
||||
"""
|
||||
kits = [child for child in schema_elem if child.tag == "kit" and child is not kit_element]
|
||||
|
||||
if not kits:
|
||||
# A blank block keeps whatever whitespace it had as the new entry's closer, so an
|
||||
# all-on-one-line <schema></schema> still gains a line break rather than losing one.
|
||||
existing_text = schema_elem.text or ""
|
||||
kit_element.tail = existing_text if existing_text.strip() == "" and existing_text else "\n"
|
||||
schema_elem.text = "\n "
|
||||
return None
|
||||
|
||||
previous = kits[-1]
|
||||
restore = (previous, previous.tail)
|
||||
kit_element.tail = previous.tail
|
||||
previous.tail = kits[-2].tail if len(kits) >= 2 else "\n "
|
||||
return restore
|
||||
|
||||
|
||||
def format_kit_row(marker, kit_name, checksum):
|
||||
"""One line of the save-time kit report: name, version and checksum.
|
||||
|
||||
Version comes from the .kit filenames in Sedona home rather than from the file, which
|
||||
records only a checksum; '(unknown)' means this machine holds no .kit for that build.
|
||||
"""
|
||||
version = palette.version_for_checksum(kit_name, checksum) or "(unknown)"
|
||||
return f" {marker} {kit_name} version {version} checksum {checksum or '(none)'}"
|
||||
|
||||
|
||||
def describe_schema_kits(added_elements):
|
||||
"""The save-time report: the kits just added, then every kit the file will declare."""
|
||||
schema_elem = app_state.xml_root_element.find("schema")
|
||||
added_names = [kit.get("name") for kit in added_elements]
|
||||
|
||||
kit_word = "kit is" if len(added_names) == 1 else "kits are"
|
||||
lines = [
|
||||
f"The following {kit_word} used by a component but not declared in this app's "
|
||||
"schema, and has been added to it:",
|
||||
"",
|
||||
]
|
||||
lines.extend(
|
||||
format_kit_row("+", name, app_state.active_schema_kits.get(name))
|
||||
for name in added_names
|
||||
)
|
||||
lines += [
|
||||
"",
|
||||
"The device's scode must also contain "
|
||||
+ ", ".join(added_names)
|
||||
+ " or the application will not run.",
|
||||
"",
|
||||
"Schema kits after this save:",
|
||||
"",
|
||||
]
|
||||
|
||||
for kit in schema_elem.findall("kit"):
|
||||
if kit.get("name") == kit_name:
|
||||
return None
|
||||
name = kit.get("name")
|
||||
if not name:
|
||||
continue
|
||||
marker = "+" if name in added_names else "•"
|
||||
lines.append(format_kit_row(marker, name, kit.get("checksum")))
|
||||
|
||||
kit_element = ET.Element("kit")
|
||||
kit_element.set("name", kit_name)
|
||||
return "\n".join(lines)
|
||||
|
||||
checksum = palette.resolve_checksum(kit_name)
|
||||
if checksum:
|
||||
kit_element.set("checksum", checksum)
|
||||
|
||||
# Added after the last existing kit. The block is not alphabetical — sys comes first
|
||||
# by convention — so appending is safer than guessing a sort position.
|
||||
insert_at = len(list(schema_elem))
|
||||
for index, existing in enumerate(list(schema_elem)):
|
||||
if existing.tag == "kit":
|
||||
insert_at = index + 1
|
||||
schema_elem.insert(insert_at, kit_element)
|
||||
def reconcile_schema_kits_before_save():
|
||||
"""Brings <schema> in step with the components, warning before anything is written.
|
||||
|
||||
return kit_element
|
||||
Returns False when the user backs out, in which case the added entries are taken back
|
||||
out and the save is abandoned.
|
||||
"""
|
||||
added, tail_restores = declare_used_kits()
|
||||
if not added:
|
||||
return True
|
||||
|
||||
proceed = messagebox.askokcancel(
|
||||
"Kits added to schema",
|
||||
describe_schema_kits(added) + "\n\nSave with these kits?",
|
||||
icon=messagebox.WARNING
|
||||
)
|
||||
|
||||
if not proceed:
|
||||
schema_elem = app_state.xml_root_element.find("schema")
|
||||
for kit_element in added:
|
||||
if schema_elem is not None and kit_element in list(schema_elem):
|
||||
schema_elem.remove(kit_element)
|
||||
app_state.active_schema_kits.pop(kit_element.get("name"), None)
|
||||
# Oldest first, so the entry that ends the block gets the last word on its tail.
|
||||
for element, tail in reversed(tail_restores):
|
||||
element.tail = tail
|
||||
return False
|
||||
|
||||
# Declared in the file now, so nothing about them is pending any more.
|
||||
for kit_element in added:
|
||||
app_state.pending_schema_kits.pop(kit_element.get("name"), None)
|
||||
return True
|
||||
|
||||
|
||||
def add_component(kit_name, type_name, grid_x, grid_y):
|
||||
@@ -363,21 +524,9 @@ def add_component(kit_name, type_name, grid_x, grid_y):
|
||||
messagebox.showwarning("Add Component", "Open a SAX file first.")
|
||||
return None
|
||||
|
||||
kit_added = ensure_kit_declared(kit_name)
|
||||
if kit_added is not None:
|
||||
proceed = messagebox.askokcancel(
|
||||
"Kit not in schema",
|
||||
f"'{kit_name}' is not declared in this app's schema.\n\n"
|
||||
f"It will be added, but the device's scode must also contain {kit_name} "
|
||||
"or the application will not run.\n\nContinue?"
|
||||
)
|
||||
if not proceed:
|
||||
schema_elem = app_state.xml_root_element.find("schema")
|
||||
if schema_elem is not None:
|
||||
schema_elem.remove(kit_added)
|
||||
return None
|
||||
|
||||
# New kit means new types to resolve before the box can be painted.
|
||||
if note_kit_used(kit_name):
|
||||
# An undeclared kit brings types the registry has never resolved, and the box
|
||||
# cannot be painted until it has them.
|
||||
try:
|
||||
schema.load_schema_kits_and_manifests()
|
||||
except schema.MissingManifestError as missing:
|
||||
@@ -386,11 +535,9 @@ def add_component(kit_name, type_name, grid_x, grid_y):
|
||||
f"'{kit_name}' was not added.\n\n"
|
||||
+ schema.describe_missing_manifests(missing)
|
||||
)
|
||||
# The kit entry goes back out and the registry is rebuilt from the kits that
|
||||
# were resolvable a moment ago, so only this placement is abandoned.
|
||||
schema_elem = app_state.xml_root_element.find("schema")
|
||||
if schema_elem is not None:
|
||||
schema_elem.remove(kit_added)
|
||||
# The kit comes back off the pending list and the registry is rebuilt from
|
||||
# what was resolvable a moment ago, so only this placement is abandoned.
|
||||
app_state.pending_schema_kits.pop(kit_name, None)
|
||||
try:
|
||||
schema.load_schema_kits_and_manifests()
|
||||
except schema.MissingManifestError:
|
||||
@@ -414,8 +561,6 @@ def add_component(kit_name, type_name, grid_x, grid_y):
|
||||
|
||||
undo.push(undo.AddComponentCommand(
|
||||
parent, index, element,
|
||||
schema_parent=app_state.xml_root_element.find("schema"),
|
||||
kit_element=kit_added,
|
||||
label=f"add {comp_name}"
|
||||
))
|
||||
|
||||
@@ -423,6 +568,17 @@ def add_component(kit_name, type_name, grid_x, grid_y):
|
||||
refresh_treeview_from_dom(app_state.xml_root_element)
|
||||
wiresheet.render_current_sheet()
|
||||
|
||||
# The schema is only reconciled on save, so the placement says so here rather than
|
||||
# letting the save dialog be the first the user hears of it.
|
||||
if kit_name in app_state.pending_schema_kits:
|
||||
app_state.set_status(
|
||||
f"{comp_name} ({kit_name}::{type_name}) added — '{kit_name}' is not in this "
|
||||
"app's schema and will be declared when you save.",
|
||||
warn=True
|
||||
)
|
||||
else:
|
||||
app_state.set_status(f"{comp_name} ({kit_name}::{type_name}) added.")
|
||||
|
||||
return element
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user