ctkAbstractPythonManager.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDir>
  16. #include <QDebug>
  17. // CTK includes
  18. #include "ctkAbstractPythonManager.h"
  19. #include "ctkScriptingPythonCoreConfigure.h"
  20. // PythonQT includes
  21. #include <PythonQt.h>
  22. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  23. #include <PythonQt_QtBindings.h>
  24. #endif
  25. // STD includes
  26. #include <csignal>
  27. #ifdef __GNUC__
  28. // Disable warnings related to signal() function
  29. // See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
  30. // Note: Ideally the incriminated functions and macros should be fixed upstream ...
  31. #pragma GCC diagnostic ignored "-Wold-style-cast"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. class ctkAbstractPythonManagerPrivate
  35. {
  36. Q_DECLARE_PUBLIC(ctkAbstractPythonManager);
  37. protected:
  38. ctkAbstractPythonManager* q_ptr;
  39. public:
  40. ctkAbstractPythonManagerPrivate(ctkAbstractPythonManager& object);
  41. virtual ~ctkAbstractPythonManagerPrivate();
  42. void (*InitFunction)();
  43. int PythonQtInitializationFlags;
  44. };
  45. //-----------------------------------------------------------------------------
  46. // ctkAbstractPythonManagerPrivate methods
  47. //-----------------------------------------------------------------------------
  48. ctkAbstractPythonManagerPrivate::ctkAbstractPythonManagerPrivate(ctkAbstractPythonManager &object) :
  49. q_ptr(&object)
  50. {
  51. this->InitFunction = 0;
  52. this->PythonQtInitializationFlags = PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut;
  53. }
  54. //-----------------------------------------------------------------------------
  55. ctkAbstractPythonManagerPrivate::~ctkAbstractPythonManagerPrivate()
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. // ctkAbstractPythonManager methods
  60. //-----------------------------------------------------------------------------
  61. ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent),
  62. d_ptr(new ctkAbstractPythonManagerPrivate(*this))
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. ctkAbstractPythonManager::~ctkAbstractPythonManager()
  67. {
  68. if (Py_IsInitialized())
  69. {
  70. Py_Finalize();
  71. }
  72. PythonQt::cleanup();
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkAbstractPythonManager::setInitializationFlags(int flags)
  76. {
  77. Q_D(ctkAbstractPythonManager);
  78. if (PythonQt::self())
  79. {
  80. return;
  81. }
  82. d->PythonQtInitializationFlags = flags;
  83. }
  84. //-----------------------------------------------------------------------------
  85. int ctkAbstractPythonManager::initializationFlags()const
  86. {
  87. Q_D(const ctkAbstractPythonManager);
  88. return d->PythonQtInitializationFlags;
  89. }
  90. //-----------------------------------------------------------------------------
  91. bool ctkAbstractPythonManager::initialize()
  92. {
  93. Q_D(ctkAbstractPythonManager);
  94. if (!PythonQt::self())
  95. {
  96. this->initPythonQt(d->PythonQtInitializationFlags);
  97. }
  98. return this->isPythonInitialized();
  99. }
  100. //-----------------------------------------------------------------------------
  101. PythonQtObjectPtr ctkAbstractPythonManager::mainContext()
  102. {
  103. bool initalized = this->initialize();
  104. if (initalized)
  105. {
  106. return PythonQt::self()->getMainModule();
  107. }
  108. return PythonQtObjectPtr();
  109. }
  110. //-----------------------------------------------------------------------------
  111. void ctkAbstractPythonManager::initPythonQt(int flags)
  112. {
  113. Q_D(ctkAbstractPythonManager);
  114. PythonQt::init(flags);
  115. // Python maps SIGINT (control-c) to its own handler. We will remap it
  116. // to the default so that control-c works.
  117. #ifdef SIGINT
  118. signal(SIGINT, SIG_DFL);
  119. #endif
  120. // Forward signal from PythonQt::self() to this instance of ctkAbstractPythonManager
  121. this->connect(PythonQt::self(), SIGNAL(systemExitExceptionRaised(int)),
  122. SIGNAL(systemExitExceptionRaised(int)));
  123. this->connect(PythonQt::self(), SIGNAL(pythonStdOut(QString)),
  124. SLOT(printStdout(QString)));
  125. this->connect(PythonQt::self(), SIGNAL(pythonStdErr(QString)),
  126. SLOT(printStderr(QString)));
  127. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  128. PythonQt_init_QtBindings();
  129. #endif
  130. QStringList initCode;
  131. // Update 'sys.path'
  132. initCode << "import sys";
  133. foreach (const QString& path, this->pythonPaths())
  134. {
  135. initCode << QString("sys.path.append('%1')").arg(QDir::fromNativeSeparators(path));
  136. }
  137. PythonQtObjectPtr _mainContext = PythonQt::self()->getMainModule();
  138. _mainContext.evalScript(initCode.join("\n"));
  139. this->preInitialization();
  140. if (d->InitFunction)
  141. {
  142. (*d->InitFunction)();
  143. }
  144. emit this->pythonPreInitialized();
  145. this->executeInitializationScripts();
  146. emit this->pythonInitialized();
  147. }
  148. //-----------------------------------------------------------------------------
  149. bool ctkAbstractPythonManager::isPythonInitialized()const
  150. {
  151. return PythonQt::self() != 0;
  152. }
  153. //-----------------------------------------------------------------------------
  154. bool ctkAbstractPythonManager::pythonErrorOccured()const
  155. {
  156. if (!PythonQt::self())
  157. {
  158. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  159. return false;
  160. }
  161. return PythonQt::self()->hadError();
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ctkAbstractPythonManager::resetErrorFlag()
  165. {
  166. if (!PythonQt::self())
  167. {
  168. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  169. return;
  170. }
  171. PythonQt::self()->clearError();
  172. }
  173. //-----------------------------------------------------------------------------
  174. QStringList ctkAbstractPythonManager::pythonPaths()
  175. {
  176. return QStringList();
  177. }
  178. //-----------------------------------------------------------------------------
  179. void ctkAbstractPythonManager::preInitialization()
  180. {
  181. }
  182. //-----------------------------------------------------------------------------
  183. void ctkAbstractPythonManager::executeInitializationScripts()
  184. {
  185. }
  186. //-----------------------------------------------------------------------------
  187. void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
  188. {
  189. if (!PythonQt::self())
  190. {
  191. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  192. return;
  193. }
  194. PythonQt::self()->addDecorators(decorator);
  195. }
  196. //-----------------------------------------------------------------------------
  197. void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
  198. {
  199. if (!PythonQt::self())
  200. {
  201. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  202. return;
  203. }
  204. PythonQt::self()->registerClass(metaobject);
  205. }
  206. //-----------------------------------------------------------------------------
  207. void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
  208. {
  209. if (!PythonQt::self())
  210. {
  211. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  212. return;
  213. }
  214. PythonQt::self()->registerCPPClass(name);
  215. }
  216. //-----------------------------------------------------------------------------
  217. bool ctkAbstractPythonManager::systemExitExceptionHandlerEnabled()const
  218. {
  219. if (!PythonQt::self())
  220. {
  221. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  222. return false;
  223. }
  224. return PythonQt::self()->systemExitExceptionHandlerEnabled();
  225. }
  226. //-----------------------------------------------------------------------------
  227. void ctkAbstractPythonManager::setSystemExitExceptionHandlerEnabled(bool value)
  228. {
  229. if (!PythonQt::self())
  230. {
  231. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  232. return;
  233. }
  234. PythonQt::self()->setSystemExitExceptionHandlerEnabled(value);
  235. }
  236. //-----------------------------------------------------------------------------
  237. QVariant ctkAbstractPythonManager::executeString(const QString& code,
  238. ctkAbstractPythonManager::ExecuteStringMode mode)
  239. {
  240. int start = -1;
  241. switch(mode)
  242. {
  243. case ctkAbstractPythonManager::FileInput: start = Py_file_input; break;
  244. case ctkAbstractPythonManager::SingleInput: start = Py_single_input; break;
  245. case ctkAbstractPythonManager::EvalInput:
  246. default: start = Py_eval_input; break;
  247. }
  248. QVariant ret;
  249. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  250. if (main)
  251. {
  252. ret = main.evalScript(code, start);
  253. }
  254. return ret;
  255. }
  256. //-----------------------------------------------------------------------------
  257. void ctkAbstractPythonManager::executeFile(const QString& filename)
  258. {
  259. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  260. if (main)
  261. {
  262. QString path = QFileInfo(filename).absolutePath();
  263. // See http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html
  264. QStringList code = QStringList()
  265. << "import sys"
  266. << QString("sys.path.insert(0, '%1')").arg(path)
  267. << "_updated_globals = globals()"
  268. << QString("_updated_globals['__file__'] = '%1'").arg(filename)
  269. << "_ctk_executeFile_exc_info = None"
  270. << "try:"
  271. << QString(" execfile('%1', _updated_globals)").arg(filename)
  272. << "except Exception, e:"
  273. << " _ctk_executeFile_exc_info = sys.exc_info()"
  274. << "finally:"
  275. << " del _updated_globals"
  276. << QString(" if sys.path[0] == '%1': sys.path.pop(0)").arg(path)
  277. << " if _ctk_executeFile_exc_info:"
  278. << " raise _ctk_executeFile_exc_info[1], None, _ctk_executeFile_exc_info[2]";
  279. this->executeString(code.join("\n"));
  280. //PythonQt::self()->handleError(); // Clear errorOccured flag
  281. }
  282. }
  283. //-----------------------------------------------------------------------------
  284. void ctkAbstractPythonManager::setInitializationFunction(void (*initFunction)())
  285. {
  286. Q_D(ctkAbstractPythonManager);
  287. d->InitFunction = initFunction;
  288. }
  289. //-----------------------------------------------------------------------------
  290. QStringList ctkAbstractPythonManager::dir_object(PyObject* object,
  291. bool appendParenthesis)
  292. {
  293. QStringList results;
  294. if (!object)
  295. {
  296. return results;
  297. }
  298. PyObject* keys = PyObject_Dir(object);
  299. if (keys)
  300. {
  301. PyObject* key;
  302. PyObject* value;
  303. int nKeys = PyList_Size(keys);
  304. for (int i = 0; i < nKeys; ++i)
  305. {
  306. key = PyList_GetItem(keys, i);
  307. value = PyObject_GetAttr(object, key);
  308. if (!value)
  309. {
  310. continue;
  311. }
  312. QString key_str(PyString_AsString(key));
  313. // Append "()" if the associated object is a function
  314. if (appendParenthesis && PyCallable_Check(value))
  315. {
  316. key_str.append("()");
  317. }
  318. results << key_str;
  319. Py_DECREF(value);
  320. }
  321. Py_DECREF(keys);
  322. }
  323. return results;
  324. }
  325. QStringList ctkAbstractPythonManager::splitByDotOutsideParenthesis(const QString& pythonVariableName)
  326. {
  327. QStringList tmpNames;
  328. int last_pos_dot = pythonVariableName.length();
  329. int numberOfParenthesisClosed = 0;
  330. bool betweenSingleQuotes = false;
  331. bool betweenDoubleQuotes = false;
  332. for (int i = pythonVariableName.length()-1; i >= 0; --i)
  333. {
  334. QChar c = pythonVariableName.at(i);
  335. if (c == '\'' && !betweenDoubleQuotes)
  336. {
  337. betweenSingleQuotes = !betweenSingleQuotes;
  338. }
  339. if (c == '"' && !betweenSingleQuotes)
  340. {
  341. betweenDoubleQuotes = !betweenDoubleQuotes;
  342. }
  343. // note that we must not count parenthesis if they are between quote...
  344. if (!betweenSingleQuotes && !betweenDoubleQuotes)
  345. {
  346. if (c == '(')
  347. {
  348. if (numberOfParenthesisClosed>0)
  349. {
  350. numberOfParenthesisClosed--;
  351. }
  352. }
  353. if (c == ')')
  354. {
  355. numberOfParenthesisClosed++;
  356. }
  357. }
  358. // if we are outside parenthesis and we find a dot, then split
  359. if ((c == '.' && numberOfParenthesisClosed<=0)
  360. || i == 0)
  361. {
  362. if (i == 0) {i--;} // last case where we have to split the begging this time
  363. QString textToSplit = pythonVariableName.mid(i+1,last_pos_dot-(i+1));
  364. if (!textToSplit.isEmpty())
  365. {
  366. tmpNames.push_front(textToSplit);
  367. }
  368. last_pos_dot =i;
  369. }
  370. }
  371. return tmpNames;
  372. }
  373. //----------------------------------------------------------------------------
  374. QStringList ctkAbstractPythonManager::pythonAttributes(const QString& pythonVariableName,
  375. const QString& module,
  376. bool appendParenthesis) const
  377. {
  378. Q_ASSERT(PyThreadState_GET()->interp);
  379. PyObject* dict = PyImport_GetModuleDict();
  380. // Split module by '.' and retrieve the object associated if the last module
  381. QString precedingModule = module;
  382. PyObject* object = ctkAbstractPythonManager::pythonModule(precedingModule);
  383. PyObject* prevObject = 0;
  384. QStringList moduleList = module.split(".", QString::SkipEmptyParts);
  385. foreach(const QString& module, moduleList)
  386. {
  387. object = PyDict_GetItemString(dict, module.toLatin1().data());
  388. if (prevObject) { Py_DECREF(prevObject); }
  389. if (!object)
  390. {
  391. break;
  392. }
  393. Py_INCREF(object);
  394. dict = PyModule_GetDict(object);
  395. prevObject = object;
  396. }
  397. if (!object)
  398. {
  399. return QStringList();
  400. }
  401. // PyObject* object = PyDict_GetItemString(dict, module.toLatin1().data());
  402. // if (!object)
  403. // {
  404. // return QStringList();
  405. // }
  406. // Py_INCREF(object);
  407. PyObject* main_object = object; // save the modue object (usually __main__ or __main__.__builtins__)
  408. QString instantiated_class_name = "_ctkAbstractPythonManager_autocomplete_tmp";
  409. QStringList results; // the list of attributes to return
  410. QString line_code="";
  411. if (!pythonVariableName.isEmpty())
  412. {
  413. // Split the pythonVariableName at every dot
  414. // /!\ // CAREFUL to don't take dot which are between parenthesis
  415. // To avoid the problem: split by dots in a smarter way!
  416. QStringList tmpNames = splitByDotOutsideParenthesis(pythonVariableName);
  417. for (int i = 0; i < tmpNames.size() && object; ++i)
  418. {
  419. // fill the line step by step
  420. // For example: pythonVariableName = d.foo_class().instantiate_bar().
  421. // line_code will be filled first by 'd.' and then, line_code = 'd.foo_class().', etc
  422. line_code.append(tmpNames[i]);
  423. line_code.append(".");
  424. QByteArray tmpName = tmpNames.at(i).toLatin1();
  425. if (tmpName.contains('(') && tmpName.contains(')'))
  426. {
  427. tmpNames[i] = tmpNames[i].left(tmpName.indexOf('('));
  428. tmpName = tmpNames.at(i).toLatin1();
  429. // Attempt to instantiate the associated python class
  430. PyObject* classToInstantiate;
  431. if (PyDict_Check(dict))
  432. classToInstantiate = PyDict_GetItemString(dict, tmpName.data());
  433. else
  434. classToInstantiate = PyObject_GetAttrString(object, tmpName.data());
  435. if (classToInstantiate)
  436. {
  437. QString code = " = ";
  438. code.prepend(instantiated_class_name);
  439. line_code.remove(line_code.size()-1,1); // remove the last char which is a dot
  440. code.append(line_code);
  441. // create a temporary attribute which will instantiate the class
  442. // For example: code = '_ctkAbstractPythonManager_autocomplete_tmp = d.foo_class()'
  443. PyRun_SimpleString(code.toLatin1().data());
  444. line_code.append('.'); // add the point again in case we need to continue to fill line_code
  445. object = PyObject_GetAttrString(main_object,instantiated_class_name.toLatin1().data());
  446. dict = object;
  447. results = ctkAbstractPythonManager::dir_object(object,appendParenthesis);
  448. }
  449. }
  450. else
  451. {
  452. PyObject* prevObj = object;
  453. if (PyDict_Check(object))
  454. {
  455. object = PyDict_GetItemString(object, tmpName.data());
  456. Py_XINCREF(object);
  457. }
  458. else
  459. {
  460. object = PyObject_GetAttrString(object, tmpName.data());
  461. dict = object;
  462. }
  463. Py_DECREF(prevObj);
  464. if (object)
  465. {
  466. results = ctkAbstractPythonManager::dir_object(object,appendParenthesis);
  467. }
  468. }
  469. }
  470. PyErr_Clear();
  471. }
  472. // By default if pythonVariable is empty, return the attributes of the module
  473. else
  474. {
  475. results = ctkAbstractPythonManager::dir_object(object,appendParenthesis);
  476. }
  477. if (object)
  478. {
  479. Py_DECREF(object);
  480. }
  481. // remove the temporary attribute (created to instantiate a class) from the module object
  482. if (PyObject_HasAttrString(main_object,instantiated_class_name.toLatin1().data()))
  483. {
  484. PyObject_DelAttrString(main_object,instantiated_class_name.toLatin1().data());
  485. }
  486. return results;
  487. }
  488. //-----------------------------------------------------------------------------
  489. PyObject* ctkAbstractPythonManager::pythonObject(const QString& variableNameAndFunction)
  490. {
  491. QStringList variableNameAndFunctionList = variableNameAndFunction.split(".");
  492. QString compareFunction = variableNameAndFunctionList.last();
  493. variableNameAndFunctionList.removeLast();
  494. QString pythonVariableName = variableNameAndFunctionList.last();
  495. variableNameAndFunctionList.removeLast();
  496. QString precedingModules = variableNameAndFunctionList.join(".");
  497. Q_ASSERT(PyThreadState_GET()->interp);
  498. PyObject* object = ctkAbstractPythonManager::pythonModule(precedingModules);
  499. if (!object)
  500. {
  501. return NULL;
  502. }
  503. if (!pythonVariableName.isEmpty())
  504. {
  505. QStringList tmpNames = pythonVariableName.split('.');
  506. for (int i = 0; i < tmpNames.size() && object; ++i)
  507. {
  508. QByteArray tmpName = tmpNames.at(i).toLatin1();
  509. PyObject* prevObj = object;
  510. if (PyDict_Check(object))
  511. {
  512. object = PyDict_GetItemString(object, tmpName.data());
  513. Py_XINCREF(object);
  514. }
  515. else
  516. {
  517. object = PyObject_GetAttrString(object, tmpName.data());
  518. }
  519. Py_DECREF(prevObj);
  520. }
  521. }
  522. PyObject* finalPythonObject = NULL;
  523. if (object)
  524. {
  525. PyObject* keys = PyObject_Dir(object);
  526. if (keys)
  527. {
  528. PyObject* key;
  529. PyObject* value;
  530. int nKeys = PyList_Size(keys);
  531. for (int i = 0; i < nKeys; ++i)
  532. {
  533. key = PyList_GetItem(keys, i);
  534. value = PyObject_GetAttr(object, key);
  535. if (!value)
  536. {
  537. continue;
  538. }
  539. QString keyStr = PyString_AsString(key);
  540. if (keyStr.operator ==(compareFunction.toLatin1()))
  541. {
  542. finalPythonObject = value;
  543. break;
  544. }
  545. Py_DECREF(value);
  546. }
  547. Py_DECREF(keys);
  548. }
  549. Py_DECREF(object);
  550. }
  551. return finalPythonObject;
  552. }
  553. //-----------------------------------------------------------------------------
  554. PyObject* ctkAbstractPythonManager::pythonModule(const QString& module)
  555. {
  556. PyObject* dict = PyImport_GetModuleDict();
  557. PyObject* object = 0;
  558. PyObject* prevObject = 0;
  559. QStringList moduleList = module.split(".", QString::KeepEmptyParts);
  560. if (!dict)
  561. {
  562. return object;
  563. }
  564. foreach(const QString& module, moduleList)
  565. {
  566. object = PyDict_GetItemString(dict, module.toLatin1().data());
  567. if (prevObject)
  568. {
  569. Py_DECREF(prevObject);
  570. }
  571. if (!object)
  572. {
  573. break;
  574. }
  575. Py_INCREF(object); // This is required, otherwise python destroys object.
  576. if (PyObject_HasAttrString(object, "__dict__"))
  577. {
  578. dict = PyObject_GetAttrString(object, "__dict__");
  579. }\
  580. prevObject = object;
  581. }
  582. return object;
  583. }
  584. //-----------------------------------------------------------------------------
  585. void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
  586. {
  587. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  588. if (main && obj)
  589. {
  590. main.addObject(name, obj);
  591. }
  592. }
  593. //-----------------------------------------------------------------------------
  594. void ctkAbstractPythonManager::addWrapperFactory(PythonQtForeignWrapperFactory* factory)
  595. {
  596. if (!PythonQt::self())
  597. {
  598. qWarning() << Q_FUNC_INFO << " failed: PythonQt is not initialized";
  599. return;
  600. }
  601. PythonQt::self()->addWrapperFactory(factory);
  602. }
  603. //-----------------------------------------------------------------------------
  604. QVariant ctkAbstractPythonManager::getVariable(const QString& name)
  605. {
  606. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  607. if (main)
  608. {
  609. return PythonQt::self()->getVariable(main, name);
  610. }
  611. return QVariant();
  612. }
  613. //-----------------------------------------------------------------------------
  614. void ctkAbstractPythonManager::printStdout(const QString& text)
  615. {
  616. std::cout << qPrintable(text);
  617. }
  618. //-----------------------------------------------------------------------------
  619. void ctkAbstractPythonManager::printStderr(const QString& text)
  620. {
  621. std::cerr << qPrintable(text);
  622. }