-----------------------------------------------------------------------------
| 443 | |
| 444 | //----------------------------------------------------------------------------- |
| 445 | void pqPythonShell::runScript() |
| 446 | { |
| 447 | pqServer* server = pqApplicationCore::instance()->getActiveServer(); |
| 448 | pqFileDialog dialog(server, this, tr("Run Script"), QString(), |
| 449 | tr("Python Files") + QString(" (*.py);;") + tr("All Files") + QString(" (*)"), false, false); |
| 450 | dialog.setObjectName("PythonShellRunScriptDialog"); |
| 451 | dialog.setFileMode(pqFileDialog::ExistingFile); |
| 452 | if (dialog.exec() == QDialog::Accepted) |
| 453 | { |
| 454 | const auto pxm = server->proxyManager(); |
| 455 | const vtkTypeUInt32 location = dialog.getSelectedLocation(); |
| 456 | for (const QString& filename : dialog.getSelectedFiles()) |
| 457 | { |
| 458 | const std::string contents = pxm->LoadString(filename.toStdString().c_str(), location); |
| 459 | if (!contents.empty()) |
| 460 | { |
| 461 | QString code; |
| 462 | // First inject code to let the script know its own path |
| 463 | code.append(QString("__file__ = r'%1'\n").arg(filename)); |
| 464 | // Then append the file content |
| 465 | code.append(contents.c_str()); |
| 466 | code.append("\n"); |
| 467 | code.append("del __file__\n"); |
| 468 | this->executeScript(code.toUtf8().data()); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | qCritical() << tr("Error: script ") << filename << tr(" was empty or could not be opened."); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
nothing calls this directly
no test coverage detected