0
0
Fork 0
Codesys-MCP-SP21-plus/src/scripts/rename_object.py
Luke aad246576c Add 17 new MCP tools for v0.4.0: compiler diagnostics, project authoring, runtime monitoring, library management
- Phase 1: Structured compiler diagnostics — compile_project now returns parsed errors/warnings with object name and line number; new get_compile_messages tool reads last build messages without recompiling
- Phase 2: Project authoring — create_dut, create_gvl, create_folder, delete_object, rename_object, get_all_pou_code
- Phase 3: Online/runtime — connect_to_device, disconnect_from_device, get_application_state, read_variable, write_variable, download_to_device, start_stop_application (with ensure_online_connection helper)
- Phase 4: Library management — list_project_libraries, add_library
- Bump version to 0.4.0, update README with full tool reference

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 21:05:12 +10:00

51 lines
2.4 KiB
Python

import sys, scriptengine as script_engine, os, traceback
OBJECT_PATH = "{OBJECT_PATH}"
NEW_NAME = "{NEW_NAME}"
try:
print("DEBUG: rename_object script: ObjectPath='%s', NewName='%s', Project='%s'" % (OBJECT_PATH, NEW_NAME, PROJECT_FILE_PATH))
primary_project = ensure_project_open(PROJECT_FILE_PATH)
if not OBJECT_PATH: raise ValueError("Object path empty.")
if not NEW_NAME: raise ValueError("New name empty.")
# Find the target object
target_object = find_object_by_path_robust(primary_project, OBJECT_PATH, "target object")
if not target_object:
raise ValueError("Object not found at path: %s" % OBJECT_PATH)
old_name = getattr(target_object, 'get_name', lambda: OBJECT_PATH)()
target_type = type(target_object).__name__
print("DEBUG: Found target object: %s (Type: %s)" % (old_name, target_type))
# Rename the object
if hasattr(target_object, 'set_name'):
print("DEBUG: Calling set_name('%s') on object '%s'" % (NEW_NAME, old_name))
target_object.set_name(NEW_NAME)
print("DEBUG: Object renamed.")
elif hasattr(target_object, 'rename'):
print("DEBUG: Calling rename('%s') on object '%s'" % (NEW_NAME, old_name))
target_object.rename(NEW_NAME)
print("DEBUG: Object renamed.")
else:
raise TypeError("Object '%s' of type %s does not support set_name() or rename()." % (old_name, target_type))
try:
print("DEBUG: Saving Project...")
primary_project.save()
print("DEBUG: Project saved successfully after rename.")
except Exception as save_err:
print("ERROR: Failed to save Project after renaming object: %s" % save_err)
detailed_error = traceback.format_exc()
error_message = "Error saving Project after renaming '%s' to '%s': %s\n%s" % (old_name, NEW_NAME, save_err, detailed_error)
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
print("Object Renamed: '%s' -> '%s'" % (old_name, NEW_NAME))
print("Object Type: %s" % target_type)
print("Path: %s" % OBJECT_PATH)
print("SCRIPT_SUCCESS: Object renamed successfully.")
sys.exit(0)
except Exception as e:
detailed_error = traceback.format_exc()
error_message = "Error renaming object '%s' in project '%s': %s\n%s" % (OBJECT_PATH, PROJECT_FILE_PATH, e, detailed_error)
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)