| 274 | } |
| 275 | |
| 276 | ScriptCommand::Ptr PythonModule::createScriptCommand(const std::string& scriptBasePath, const std::string& relativeScriptPath) |
| 277 | { |
| 278 | try |
| 279 | { |
| 280 | auto fullPath = scriptBasePath + relativeScriptPath; |
| 281 | |
| 282 | // Create a new dictionary for the initialisation routine |
| 283 | py::dict locals; |
| 284 | |
| 285 | // Disable the flag for initialisation, just to be sure |
| 286 | locals["__executeCommand__"] = false; |
| 287 | |
| 288 | // Attempt to run the specified script |
| 289 | py::eval_file(fullPath, getGlobals(), locals); |
| 290 | |
| 291 | std::string cmdName; |
| 292 | std::string cmdDisplayName; |
| 293 | |
| 294 | if (locals.contains("__commandName__")) |
| 295 | { |
| 296 | cmdName = locals["__commandName__"].cast<std::string>(); |
| 297 | } |
| 298 | |
| 299 | if (locals.contains("__commandDisplayName__")) |
| 300 | { |
| 301 | cmdDisplayName = locals["__commandDisplayName__"].cast<std::string>(); |
| 302 | } |
| 303 | |
| 304 | if (!cmdName.empty()) |
| 305 | { |
| 306 | if (cmdDisplayName.empty()) |
| 307 | { |
| 308 | cmdDisplayName = cmdName; |
| 309 | } |
| 310 | |
| 311 | // Successfully retrieved the command |
| 312 | return std::make_shared<ScriptCommand>(cmdName, cmdDisplayName, relativeScriptPath); |
| 313 | } |
| 314 | |
| 315 | rError() << "Script file " << relativeScriptPath << " does not export a __commandName__ value" << std::endl; |
| 316 | return {}; |
| 317 | } |
| 318 | catch (const py::error_already_set& ex) |
| 319 | { |
| 320 | rError() << "Script file " << relativeScriptPath << " is not a valid command:" << std::endl; |
| 321 | rError() << ex.what() << std::endl; |
| 322 | return {}; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | PythonModule* PythonModule::_instance = nullptr; |
| 327 | |