-----------------------------------------------------------------------------
| 589 | |
| 590 | //----------------------------------------------------------------------------- |
| 591 | PyObject* ctkAbstractPythonManager::pythonObject(const QString& variableNameAndFunction) |
| 592 | { |
| 593 | QStringList variableNameAndFunctionList = variableNameAndFunction.split("."); |
| 594 | QString compareFunction = variableNameAndFunctionList.last(); |
| 595 | variableNameAndFunctionList.removeLast(); |
| 596 | QString pythonVariableName = variableNameAndFunctionList.last(); |
| 597 | variableNameAndFunctionList.removeLast(); |
| 598 | QString precedingModules = variableNameAndFunctionList.join("."); |
| 599 | |
| 600 | Q_ASSERT(PyThreadState_GET()->interp); |
| 601 | PyObject* object = ctkAbstractPythonManager::pythonModule(precedingModules); |
| 602 | if (!object) |
| 603 | { |
| 604 | return NULL; |
| 605 | } |
| 606 | if (!pythonVariableName.isEmpty()) |
| 607 | { |
| 608 | QStringList tmpNames = pythonVariableName.split('.'); |
| 609 | for (int i = 0; i < tmpNames.size() && object; ++i) |
| 610 | { |
| 611 | QByteArray tmpName = tmpNames.at(i).toLatin1(); |
| 612 | PyObject* prevObj = object; |
| 613 | if (PyDict_Check(object)) |
| 614 | { |
| 615 | object = PyDict_GetItemString(object, tmpName.data()); |
| 616 | Py_XINCREF(object); |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | object = PyObject_GetAttrString(object, tmpName.data()); |
| 621 | } |
| 622 | Py_DECREF(prevObj); |
| 623 | } |
| 624 | } |
| 625 | PyObject* finalPythonObject = NULL; |
| 626 | if (object) |
| 627 | { |
| 628 | PyObject* keys = PyObject_Dir(object); |
| 629 | if (keys) |
| 630 | { |
| 631 | PyObject* key; |
| 632 | PyObject* value; |
| 633 | int nKeys = PyList_Size(keys); |
| 634 | for (int i = 0; i < nKeys; ++i) |
| 635 | { |
| 636 | key = PyList_GetItem(keys, i); |
| 637 | value = PyObject_GetAttr(object, key); |
| 638 | if (!value) |
| 639 | { |
| 640 | continue; |
| 641 | } |
| 642 | QString keyStr = PyString_AsString(key); |
| 643 | if (keyStr.operator ==(compareFunction.toLatin1())) |
| 644 | { |
| 645 | finalPythonObject = value; |
| 646 | break; |
| 647 | } |
| 648 | Py_DECREF(value); |