Files
OrcaSlicer/tools/mcp/tests/fixtures/ops_undo.py
T
SoftFever d64e10f4dd Regroup the slicer's MCP commands and let each run from its arguments
Outside the Design tab an agent had 175 commands, 50 of them in the tool
list. There are now 118, named by what they act on (`object_`, `plate_`,
`preset_`, `filament_`, `plugin_` and so on), and 115 are listed. Each takes
its target and values in the call, acts on the selection when it names no
target and says what it acted on; an old name is refused. Printers and
system filaments install and uninstall, preferences read and write, plugins
enable, reload, uninstall and refresh, and objects reorder, with no dialog
to drive.

The Speed Dial keeps its 129 tiles under the same ids. Eight defects in the
rows these commands share with the menus are fixed. For a person: an object
row that cannot run says why instead of "Select an object first.", rows
that failed no longer claim success, Duplicate Plate can be undone, and
Refresh Plugins picked while a refresh runs now runs after it instead of
beside it.
2026-11-18 16:50:32 +08:00

42 lines
1.9 KiB
Python

"""The undo snapshots the app took since the last call, read from its log, for the ops_*.json replays.
No tool lists the undo history, and Plater::priv::take_snapshot() logs every snapshot it takes
("Undo / Redo snapshot taken: <name>"), so this reads those lines and keeps how many it has seen in
<datadir>/mcp/ops_undo_mark.txt. The first call of a run sets the mark; each later one returns the names
of the snapshots taken since the previous call, in order. A replay calls it with
{"tool": "script_run", "args": {"filename": "${replay_dir}/fixtures/ops_undo.py",
"code": "import ops_undo\nresult = ops_undo.taken()"}}
log_lines(text) returns the last log lines that contain a text, for what only the log says (a picker a
rule cancelled, for one).
"""
import os
import orca
KEY = "Undo / Redo snapshot taken: "
def taken():
log = orca.host.tools.call("app_log", {"lines": 1})["file"]
with open(log, encoding="utf-8", errors="replace") as f:
names = [line.split(KEY, 1)[1].split(", Undo / Redo stack memory")[0] for line in f if KEY in line]
mark = os.path.join(os.path.dirname(os.path.dirname(log)), "mcp", "ops_undo_mark.txt")
try:
with open(mark) as f:
seen = int(f.read())
except (OSError, ValueError):
seen = 0
if seen > len(names): # another log file: the app was restarted
seen = 0
with open(mark, "w") as f:
f.write(str(len(names)))
return {"taken": names[seen:], "count": len(names) - seen}
def log_lines(text, last=3):
"""The last `last` lines of the app's log that contain `text`, without their timestamps."""
log = orca.host.tools.call("app_log", {"lines": 1})["file"]
with open(log, encoding="utf-8", errors="replace") as f:
lines = [line.rstrip("\n") for line in f if text in line]
return [line.split("]:", 1)[-1] for line in lines[-last:]]