MCPcopy Create free account
hub / github.com/commontk/CTK / pythonAttributes

Method pythonAttributes

Libs/Scripting/Python/Core/ctkAbstractPythonManager.cpp:452–588  ·  view source on GitHub ↗

----------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

450
451//----------------------------------------------------------------------------
452QStringList ctkAbstractPythonManager::pythonAttributes(const QString& pythonVariableName,
453 const QString& module,
454 bool appendParenthesis) const
455{
456 Q_ASSERT(PyThreadState_GET()->interp);
457 PyObject* dict = PyImport_GetModuleDict();
458
459 // Split module by '.' and retrieve the object associated if the last module
460 QString precedingModule = module;
461 PyObject* object = ctkAbstractPythonManager::pythonModule(precedingModule);
462 PyObject* prevObject = 0;
463 #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
464 QStringList moduleList = module.split(".", Qt::SkipEmptyParts);
465 #else
466 QStringList moduleList = module.split(".", QString::SkipEmptyParts);
467 #endif
468
469 foreach(const QString& module, moduleList)
470 {
471 object = PyDict_GetItemString(dict, module.toLatin1().data());
472 if (prevObject) { Py_DECREF(prevObject); }
473 if (!object)
474 {
475 break;
476 }
477 Py_INCREF(object);
478 dict = PyModule_GetDict(object);
479 prevObject = object;
480 }
481 if (!object)
482 {
483 return QStringList();
484 }
485
486// PyObject* object = PyDict_GetItemString(dict, module.toLatin1().data());
487// if (!object)
488// {
489// return QStringList();
490// }
491// Py_INCREF(object);
492
493 PyObject* main_object = object; // save the module object (usually __main__ or __main__.__builtins__)
494 QString instantiated_class_name = "_ctkAbstractPythonManager_autocomplete_tmp";
495 QStringList results; // the list of attributes to return
496 QString line_code="";
497
498 if (!pythonVariableName.isEmpty())
499 {
500 // Split the pythonVariableName at every dot
501 // /!\ // CAREFUL to don't take dot which are between parenthesis
502 // To avoid the problem: split by dots in a smarter way!
503 QStringList tmpNames = splitByDotOutsideParenthesis(pythonVariableName);
504
505 for (int i = 0; i < tmpNames.size() && object; ++i)
506 {
507 // fill the line step by step
508 // For example: pythonVariableName = d.foo_class().instantiate_bar().
509 // line_code will be filled first by 'd.' and then, line_code = 'd.foo_class().', etc

Callers 5

isInUserDefinedClassMethod · 0.80
isUserDefinedFunctionMethod · 0.80
isBuiltInFunctionMethod · 0.80
updateCompletionModelMethod · 0.80
testPythonAttributesMethod · 0.80

Calls 8

QStringListClass · 0.85
isEmptyMethod · 0.45
sizeMethod · 0.45
containsMethod · 0.45
indexOfMethod · 0.45
dataMethod · 0.45
removeMethod · 0.45
clearMethod · 0.45

Tested by 1

testPythonAttributesMethod · 0.64