Get Python Plugin Metadata

This commit is contained in:
Florian Märkl 2018-07-07 11:07:05 +02:00
parent 1f3315d020
commit b9c859bc0c
3 changed files with 29 additions and 7 deletions

View File

@ -3,7 +3,7 @@
#include "CutterPythonPlugin.h"
CutterPythonPlugin::CutterPythonPlugin(PyObject* pluginModule)
CutterPythonPlugin::CutterPythonPlugin(PyObject *pluginModule)
{
this->pluginModule = pluginModule;
@ -14,7 +14,28 @@ CutterPythonPlugin::CutterPythonPlugin(PyObject* pluginModule)
pInstance = PyObject_GetAttrString(pluginModule, "plugin");
if (!pInstance) {
qWarning() << "Cannot find plugin instance.";
return;
}
auto getStringAttr = [this](const char *attrName) -> QString {
if (!PyObject_HasAttrString(pInstance, attrName)) {
return QString();
}
PyObject *attr = PyObject_GetAttrString(pInstance, attrName);
if (!attr) {
PyErr_Print();
return QString();
}
if (!PyUnicode_Check(attr)) {
return QString();
}
return QString::fromUtf8(PyUnicode_AsUTF8(attr));
};
name = getStringAttr("name");
description = getStringAttr("description");
version = getStringAttr("version");
author = getStringAttr("author");
}
CutterPythonPlugin::~CutterPythonPlugin()

View File

@ -3,12 +3,14 @@ from cutter_plugin import CutterPlugin
from PySide2 import QtCore, QtWidgets
import shiboken2
class CutterSamplePlugin(CutterPlugin):
name = "SamplePlugin"
description = "A sample plugin written in python."
version = "1.0"
author = "xarkes"
def setupPlugin(self):
self.name = 'SamplePlugin'
self.description = 'A sample plugin written in python.'
self.version = '1.0'
self.author = 'xarkes'
self.app = QtCore.QCoreApplication.instance()
def setupInterface(self):
@ -28,4 +30,3 @@ class CutterSamplePlugin(CutterPlugin):
# Instantiate our plugin
plugin = CutterSamplePlugin()

View File

@ -1,11 +1,11 @@
from abc import ABC, abstractmethod
class CutterPlugin(ABC):
name = ''
description = ''
version = ''
author = ''
app = None
@abstractmethod
def setupPlugin(self):