Add support for removing entries and listing child keys in PyPluginStorage

This commit is contained in:
Michael Zanetti 2022-03-23 00:19:39 +01:00
parent b77b9a7bb6
commit 1befe6166f
2 changed files with 30 additions and 3 deletions

View File

@ -63,7 +63,7 @@ static PyObject * PyPluginStorage_setValue(PyPluginStorage* self, PyObject* args
char *keyStr = nullptr;
PyObject *value = nullptr;
if (!PyArg_ParseTuple(args, "sO", &keyStr, &value)) {
PyErr_SetString(PyExc_TypeError, "Invalid arguments in value call. Expected: value(key, value)");
PyErr_SetString(PyExc_TypeError, "Invalid arguments in setValue call. Expected: setValue(key, value)");
return nullptr;
}
@ -74,7 +74,7 @@ static PyObject * PyPluginStorage_setValue(PyPluginStorage* self, PyObject* args
static PyObject * PyPluginStorage_beginGroup(PyPluginStorage* self, PyObject* args) {
char *groupStr = nullptr;
if (!PyArg_ParseTuple(args, "s", &groupStr)) {
PyErr_SetString(PyExc_TypeError, "Invalid arguments in value call. Expected: beginGroup(group)");
PyErr_SetString(PyExc_TypeError, "Invalid arguments in beginGroup call. Expected: beginGroup(group)");
return nullptr;
}
@ -88,11 +88,29 @@ static PyObject * PyPluginStorage_endGroup(PyPluginStorage* self, PyObject* args
Py_RETURN_NONE;
};
static PyObject * PyPluginStorage_remove(PyPluginStorage *self, PyObject* args) {
char *keyStr = nullptr;
if (!PyArg_ParseTuple(args, "s", &keyStr)) {
PyErr_SetString(PyExc_TypeError, "Invalid arguments in remove call. Expected: remove(key)");
return nullptr;
}
self->pluginStorage->remove(keyStr);
Py_RETURN_NONE;
};
static PyObject * PyPluginStorage_childKeys(PyPluginStorage* self, PyObject* args) {
Q_UNUSED(args)
QStringList keys = self->pluginStorage->childKeys();
return QVariantToPyObject(keys);
};
static PyMethodDef PyPluginStorage_methods[] = {
{ "value", (PyCFunction)PyPluginStorage_value, METH_VARARGS, "Get a value from the plugin storage" },
{ "value", (PyCFunction)PyPluginStorage_value, METH_VARARGS, "Get a value from the plugin storage" },
{ "setValue", (PyCFunction)PyPluginStorage_setValue, METH_VARARGS, "Set a value to the plugin storage"},
{ "beginGroup", (PyCFunction)PyPluginStorage_beginGroup, METH_VARARGS, "Begin a group in the plugin storage."},
{ "endGroup", (PyCFunction)PyPluginStorage_endGroup, METH_VARARGS, "End a group in the plugin storage."},
{ "remove", (PyCFunction)PyPluginStorage_remove, METH_VARARGS, "Remove an entry/group from the plugin storage."},
{ "childKeys", (PyCFunction)PyPluginStorage_childKeys, METH_VARARGS, "List all keys (including subgroups) of the current group."},
{nullptr, nullptr, 0, nullptr} // sentinel
};

View File

@ -44,6 +44,15 @@ PyObject *QVariantToPyObject(const QVariant &value)
Py_DECREF(timeTuple);
break;
}
case QVariant::StringList: {
QStringList stringList = value.toStringList();
pyValue = PyTuple_New(stringList.length());
for (int i = 0; i < stringList.count(); i++) {
PyObject *entry = PyUnicode_FromString(stringList.at(i).toUtf8());
PyTuple_SetItem(pyValue, i, entry);
}
break;
}
default:
Q_ASSERT_X(false, "pyutils.h", QString("Unhandled data type in converting QVariant to PyObject: %1").arg(value.type()).toUtf8());
qCWarning(dcPythonIntegrations()) << "Unhandled data type" << value.type() << "in conversion from Param to PyParam!";