Ejemplo n.º 1
0
void script_set_main_window(MainWindow *mwin)
{
  pycon("Adding Sproxel objects to script...");

  PyObject *mod=PyImport_AddModule("sproxel");
  PyObject *o;
  bool gotErrors=false;

  o = Py_None; Py_INCREF(o);
  PyModule_AddObject(mod, "main_window", o);

  o=PyList_New(1);
  PyList_SetItem(o, 0, qstr_to_py(exe_dir.absoluteFilePath("plugins")));
  PyModule_AddObject(mod, "plugin_pathes", o);

  PyModule_AddObject(mod, "undo", undo_manager_to_py(mwin->undoManager()));

  //== TODO: add more components to script

  // init plugin pathes
  if (py_init_plugin_pathes)
  {
    PyObject *res=PyObject_CallFunctionObjArgs(py_init_plugin_pathes, NULL);
    if (!res) PyErr_Print();
    else Py_DECREF(res);
  }

  pycon("Sproxel objects: %s", gotErrors ? "FAILED to add to script" : "all added");
}
Ejemplo n.º 2
0
bool run_script(const QString &fn)
{
  QString filename=exe_dir.filePath(fn);
  #ifdef _WIN32
    FILE *file=_wfopen(filename.utf16(), L"r");
  #else
    FILE *file=fopen(filename.toUtf8(), "r");
  #endif
  if (file)
  {
    pycon("Starting script %S", filename.unicode());

    PyObject *mod=PyImport_AddModule("__main__");
    PyObject *modDict=PyModule_GetDict(mod);

    PyObject *o=PyRun_File(file, qPrintable(filename), Py_file_input, modDict, modDict);
    fclose(file);
    Py_XDECREF(o);

    if (PyErr_Occurred())
    {
      PyErr_Print();
      return false;
    }

    return true;
  }
  else
  {
    pycon("Failed to open script file %S", filename.unicode());
    return false;
  }
}
Ejemplo n.º 3
0
void init_script(const char *exe_path)
{
  exe_dir=QFileInfo(exe_path).absoluteDir();

  qDebug() << "prog name:" << exe_path;
  Py_SetProgramName((char*)exe_path);

  qDebug() << "init py...";
  Py_Initialize();
  qDebug() << "init py ok";

  init_python_console();
  init_sproxel_bindings();

  pycon("exe path: %s", exe_path);
  pycon("exe dir: %s", exe_dir.absolutePath().toLocal8Bit().data());

  QString code="import sys\nsys.path.insert(0, \"";
  code.append(exe_dir.absolutePath());
  code.append("\")\nprint 'sys.path:', sys.path\n");
  PyRun_SimpleString(code.toLocal8Bit().data());

  PyObject *mod=PyImport_ImportModule("sproxel_utils");
  if (!mod)
  {
    PyErr_Print();
    QMessageBox::critical(NULL, "Sproxel Error", "Failed to import sproxel_utils");
  }
  else
  {
    // check required methods
    bool gotErrors=false;

    py_save_project=PyObject_GetAttrString(mod, "save_project");
    if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }

    py_load_project=PyObject_GetAttrString(mod, "load_project");
    if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }

    py_init_plugin_pathes=PyObject_GetAttrString(mod, "init_plugin_pathes");
    if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }

    py_scan_plugins=PyObject_GetAttrString(mod, "scan_plugins");
    if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }

    py_register_plugins=PyObject_GetAttrString(mod, "register_plugins");
    if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }

    py_unregister_plugins=PyObject_GetAttrString(mod, "unregister_plugins");
    if (PyErr_Occurred()) { PyErr_Print(); gotErrors=true; }

    pycon("Scripted methods: %s", gotErrors ? "some missing" : "all OK");
    if (gotErrors) QMessageBox::critical(NULL, "Sproxel Error", "Some scripted methods are missing.");

    Py_DECREF(mod); mod=NULL;
  }
}
Ejemplo n.º 4
0
void init_python_console()
{
  //logFile=_wfopen((wchar_t*)exe_dir.filePath("log.txt").unicode(), L"wt");

  console=new ConsoleWidget("Sproxel Python console");
  console->show();
  pycon("Starting Sproxel " SPROXEL_VERSION);

  Py_InitModule("sproxelConsole", methods);

  PyRun_SimpleString(
    "import sys\n"
    "import sproxelConsole\n"
    "\n"
    "class SproxelConsoleIO(object):\n"
    "  def write(self, s):\n"
    "    sproxelConsole.write(s)\n"
    "\n"
    "sys.stdout=SproxelConsoleIO()\n"
    "sys.stderr=sys.stdout\n"
  );
}