ResPlugin::ResPlugin(const QString &pluginName) { //load module module = PyImport_ImportModule(pluginName.toUtf8().constData()); if (module == NULL) { show_pyerr(); exit(-1); } //get name PyObject *_name = PyObject_GetAttrString(module, "res_name"); if (_name) { name = PyString_AsQString(_name); Py_DecRef(_name); } else { PyErr_Clear(); name = pluginName.mid(4); } //get search() and load_item() searchFunc = PyObject_GetAttrString(module, "search"); loadItemFunc = PyObject_GetAttrString(module, "load_item"); if (searchFunc == NULL || loadItemFunc == NULL) { show_pyerr(); exit(-1); } //get tags PyObject *tags = PyObject_GetAttrString(module, "tags"); if (tags == NULL) { show_pyerr(); exit(-1); } tagsList = PyList_AsQStringList(tags); Py_DecRef(tags); //get countries PyObject *countries = PyObject_GetAttrString(module, "countries"); if (countries == NULL) { show_pyerr(); exit(-1); } countriesList = PyList_AsQStringList(countries); Py_DecRef(countries); }
QString fetchPythonException() { static PyObject *tracebackFunc = NULL; PyObject *ptype, *pvalue, *ptraceback; QStringList tracebacks; // Get error data PyErr_Fetch(&ptype, &pvalue, &ptraceback); PyErr_NormalizeException(&ptype, &pvalue, &ptraceback); // See if we can get a full traceback if (tracebackFunc == NULL) { PyObject *pyth_module = PyImport_ImportModule("traceback"); if (pyth_module) tracebackFunc = PyObject_GetAttrString(pyth_module, "format_exception"); } if (tracebackFunc && PyCallable_Check(tracebackFunc)) { PyObject *retVal = PyObject_CallFunctionObjArgs(tracebackFunc, ptype, pvalue, ptraceback, NULL); tracebacks = PyList_AsQStringList(retVal); Py_DecRef(retVal); } Py_DecRef(ptype); Py_DecRef(pvalue); Py_DecRef(ptraceback); return tracebacks.join(""); }
PyObject* DetailView::loadDetail(PyObject *dict) { static QString nameFmt = "<span style=\" font-size:16pt; font-weight:600;\">%1</span> (rating: %2)"; if (!PyDict_Check(dict)) { PyErr_SetString(PyExc_TypeError, "The argument is not a dict."); return NULL; } PyObject *item; QString name; //name if (NULL != (item = PyDict_GetItemString(dict, "name"))) { name = PyString_AsQString(item); setWindowTitle(name + tr(" - Detail page")); } //rating if (NULL != (item = PyDict_GetItemString(dict, "rating"))) ui->nameLabel->setText(nameFmt.arg(name, QString::number(PyFloat_AsDouble(item)))); else ui->nameLabel->setText(nameFmt.arg(name, tr("Unknown"))); //length if (NULL != (item = PyDict_GetItemString(dict, "length"))) ui->lengthLabel->setText(PyString_AsQString(item)); else ui->lengthLabel->setText(tr("Unknown")); //summary if (NULL != (item = PyDict_GetItemString(dict, "summary"))) ui->summaryLabel->setText(PyString_AsQString(item)); else ui->summaryLabel->setText(tr("Unknown")); //others struct Item {const char *item_name; QLabel *label;}; struct Item items[] = { {"directors", ui->directorLabel}, {"script_writers", ui->scriptwriterLabel}, {"players", ui->playerLabel}, {"types", ui->typeLabel}, {"nations", ui->nationLabel}, {"languages", ui->langLabel}, {"dates", ui->dateLabel}, {"alt_names", ui->alternameLabel}, {NULL, NULL} }; for (struct Item *i = items; i->item_name; i++) { item = PyDict_GetItemString(dict, i->item_name); if (item) { QStringList list = PyList_AsQStringList(item); i->label->setText(list.join(" / ").simplified()); } else i->label->setText(tr("Unknown")); } // Source ui->sourceListWidget->clear(); urls.clear(); item = PyDict_GetItemString(dict, "source"); if (item) { int n = PyList_Size(item); for (int i = 0; i < n; i += 2) { QString name = PyString_AsQString(PyList_GetItem(item, i)); const char *url = PyString_AsString(PyList_GetItem(item, i+1)); ui->sourceListWidget->addItem(name); urls.append(url); } } // Image item = PyDict_GetItemString(dict, "image"); if (item) { QNetworkRequest request(QUrl(PyString_AsQString(item))); request.setRawHeader("User-Agent", "moonplayer"); reply = access_manager->get(request); connect(reply, SIGNAL(finished()), this, SLOT(onImageLoaded())); } Py_IncRef(Py_None); return Py_None; }