diff --git a/RESTService/pyminsky.cc b/RESTService/pyminsky.cc index 4ab1bf088..e0ef790ff 100644 --- a/RESTService/pyminsky.cc +++ b/RESTService/pyminsky.cc @@ -30,28 +30,73 @@ namespace pyminsky minsky::Item& findObject(const std::string& typeName) { - using namespace minsky; - auto& canvas = minsky.canvas; + using namespace minsky; + auto& canvas = minsky.canvas; - canvas.item.reset(); + canvas.item.reset(); // Reset the item to ensure no leftover references - if (typeName == "Group" && !canvas.model->groups.empty()) - canvas.item = canvas.model->groups.front(); - else - minsky.model->recursiveDo(&GroupItems::items, [&](const Items&, Items::const_iterator i) + bool found = false; + + if (typeName == "Group" && !canvas.model->groups.empty()) + { + canvas.item = canvas.model->groups.front(); + found = true; + } + else { - if ((*i)->classType() == typeName) + found = minsky.model->recursiveDo(&GroupItems::items, [&](const Items&, Items::const_iterator i) { - canvas.item = *i; - return true; // Stop recursion - } - return false; - }); + if ((*i)->classType() == typeName) + { + canvas.item = *i; + return true; // Stop recursion + } + return false; + }); + } + + // Check if an object was found + if (!found || !canvas.item) + { + // std::cerr << "findObject: Object of type '" << typeName << "' not found or invalid!" << std::endl; + throw std::runtime_error("Object not found"); + } - return *canvas.item; + return *canvas.item; // Return the dereferenced item } + // Find a variable by its name + minsky::Item& findVariable(const std::string& name) + { + using namespace minsky; + auto& canvas = minsky.canvas; + + canvas.item.reset(); // Reset item to ensure clean start + + bool found = minsky.model->recursiveDo( + &GroupItems::items, + [&](const Items&, Items::const_iterator i) { + if (auto v = dynamic_cast(i->get())) + { + if (v->name() == name) + { + canvas.item = *i; + return true; // Stop recursion + } + } + return false; + }); + + if (!found || !canvas.item) + { + // std::cerr << "findVariable: Variable '" << name << "' not found or invalid!" << std::endl; + throw std::runtime_error("Variable not found"); + } + + return *canvas.item; // Return the dereferenced item + } CLASSDESC_ADD_FUNCTION(findObject); + CLASSDESC_ADD_FUNCTION(findVariable); } CLASSDESC_PYTHON_MODULE(pyminsky); diff --git a/test/00/deleteKey.sh b/test/00/deleteKey.sh index bf2771f98..b47367fd8 100755 --- a/test/00/deleteKey.sh +++ b/test/00/deleteKey.sh @@ -30,7 +30,7 @@ trap "fail" 1 2 3 15 cat >input.py <input.py <