Ejemplo n.º 1
0
  void PythonEngine::loadScript(const QString &filename)
  {
    QFileInfo info(filename);
    initializePython(info.canonicalPath());
    PythonThread pt;

    PythonScript *script = new PythonScript(filename);
    m_identifier = script->identifier();

    if(script->module()) {
      // make sure there is an Engine class defined
      if (PyObject_HasAttrString(script->module().ptr(), "Engine")) {

        try {
          prepareToCatchError();
          // instatiate the new Engine
          m_instance = script->module().attr("Engine")();
        } catch (error_already_set const &) {
          catchError();
          return;
        }

        m_script = script;

      } else {
        delete script;
        PythonError::instance()->append(tr("PythonEngine: checking ") + filename + "...");
        PythonError::instance()->append(tr("  - script has no 'Engine' class defined"));
      }
    } else {
      delete script;
      PythonError::instance()->append(tr("PythonEngine: checking ") + filename + "...");
      PythonError::instance()->append(tr("  - no module"));
    }
  }
Ejemplo n.º 2
0
  void PythonExtension::loadScript(const QString &filename)
  {
    QFileInfo info(filename);
    initializePython(info.canonicalPath());
    PythonThread pt;

    PythonScript *script = new PythonScript(filename);
    m_identifier = script->identifier();

    if (script->module()) {
      // make sure there is an Extension class defined
      if (PyObject_HasAttrString(script->module().ptr(), "Extension")) {
        try {
          prepareToCatchError();
          m_instance = script->module().attr("Extension")();
        } catch (error_already_set const &) {
          catchError();
          return;
        }

        // connect signal(s)
        if (PyObject_HasAttrString(m_instance.ptr(), "__pyqtSignals__")) {
          QObject *obj = extract<QObject*>(m_instance);
          connect(obj, SIGNAL(message(const QString&)), this, SIGNAL(message(const QString&)));
        }

        m_script = script;

      } else {
        delete script;
Ejemplo n.º 3
0
void PythonModule::initialize() throw (VoreenException) {
    VoreenModule::initialize();

    //
    // Initialize Python interpreter
    //
    LINFO("Python version: " << Py_GetVersion());

    // Pass program name to the Python interpreter
    char str_pyvoreen[] = "PyVoreen";
    Py_SetProgramName(str_pyvoreen);

    // Initialize the Python interpreter. Required.
    Py_InitializeEx(false);
    if (!Py_IsInitialized())
        throw VoreenException("Failed to initialize Python interpreter");

    // required in order to use threads.
    PyEval_InitThreads();

    // init ResourceManager search path
    addPath("");
    addPath(VoreenApplication::app()->getScriptPath());
    addPath(VoreenApplication::app()->getModulePath("python/scripts"));

    // init Python's internal module search path
    addModulePath(VoreenApplication::app()->getScriptPath());
    addModulePath(VoreenApplication::app()->getModulePath("python/scripts"));

    //
    // Redirect script output from std::cout to voreen_print function (see above)
    //

    // import helper module
    if (!Py_InitModule("voreen_internal", internal_methods)) {
        LWARNING("Failed to init helper module 'voreen_internal'");
    }

    // load output redirector script and run it once
    std::string filename = "outputcatcher.py";
    LDEBUG("Loading Python init script '" << filename << "'");
    PythonScript* initScript = load(filename);
    if (initScript) {
        if (!initScript->run())
            LWARNING("Failed to run init script '" << filename << "': " << initScript->getLog());
        dispose(initScript);
    }
    else {
        LWARNING("Failed to load init script '" << filename << "'");
    }

    //
    // Create actual Voreen Python bindings
    //
    pyVoreen_ = new PyVoreen();
}
Ejemplo n.º 4
0
void PythonScriptBase::execSyncV(      FieldContainer    &oFrom,
                                        ConstFieldMaskArg  whichField,
                                        AspectOffsetStore &oOffsets,
                                        ConstFieldMaskArg  syncMode,
                                  const UInt32             uiSyncInfo)
{
    PythonScript *pThis = static_cast<PythonScript *>(this);

    pThis->execSync(static_cast<PythonScript *>(&oFrom),
                    whichField,
                    oOffsets,
                    syncMode,
                    uiSyncInfo);
}
Ejemplo n.º 5
0
  void PythonTool::loadScript(const QString &filename)
  {
    QFileInfo info(filename);
    initializePython(info.canonicalPath());
    
    PythonThread pt;

    PythonScript *script = new PythonScript(filename);
    m_identifier = script->identifier();

    if(script->module()) {
      // make sure there is a Tool class defined
      if (PyObject_HasAttrString(script->module().ptr(), "Tool")) {
        try {
          prepareToCatchError();
          // instantiate the new tool
          m_instance = script->module().attr("Tool")();
          // if we have a settings widget already, add the python content...
          if (m_settingsWidget) {
            if (PyObject_HasAttrString(m_instance.ptr(), "settingsWidget")) {
              QWidget *widget = extract<QWidget*>(m_instance.attr("settingsWidget")());
              if (widget)
                m_settingsWidget->layout()->addWidget(widget);
            }
          }
        } catch (error_already_set const &) {
          catchError();
          return;
        }

        m_script = script;

      } else {
        delete script;
        PythonError::instance()->append(tr("PythonTool: checking ") + filename + "...");
        PythonError::instance()->append(tr("  - script has no 'Tool' class defined"));
      }
    } else {
      delete script;
      PythonError::instance()->append(tr("PythonTool: checking ") + filename + "...");
      PythonError::instance()->append(tr("  - no module"));
    }
  }
Ejemplo n.º 6
0
void PyInviwo::initOutputRedirector(Python3Module* module) {
    std::string directorFileName = module->getPath() + "/scripts/outputredirector.py";

    if (!filesystem::fileExists(directorFileName)) {
        LogError("Could not open outputredirector.py");
        return;
    }

    std::ifstream file(directorFileName.c_str());
    std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
    file.close();

    PythonScript outputCatcher;
    outputCatcher.setSource(text);
    outputCatcher.setFilename(directorFileName);

    if (!outputCatcher.run(false)) {
        LogWarn("Python init script failed to run");
    }
}
Ejemplo n.º 7
0
PythonScript* PythonModule::load(const std::string& filename, bool compileDirectly) {

    // do not check isInitialized(), since we call this function from initialize()
    if (!Py_IsInitialized()) {
        LWARNING("load(): not initialized");
        return 0;
    }

    if (isLoaded(filename)) {
        increaseUsage(filename);
        return get(filename);
    }

    PythonScript* script = new PythonScript();
    if (script->load(completePath(filename), compileDirectly)) {
        reg(script, filename);
        return script;
    }
    delete script;

    return 0;
}