cutter/src/utils/PythonAPI.cpp

110 lines
2.5 KiB
C++
Raw Normal View History

2018-02-11 17:59:23 +00:00
#include "PythonAPI.h"
#include "cutter.h"
2018-02-22 21:08:06 +00:00
#include "JupyterConnection.h"
#include "NestedIPyKernel.h"
2018-02-11 17:59:23 +00:00
#include <QFile>
2018-02-11 17:59:23 +00:00
PyObject *api_version(PyObject *self, PyObject *null)
{
Q_UNUSED(self)
Q_UNUSED(null)
return PyUnicode_FromString("Cutter version " CUTTER_VERSION);
}
PyObject *api_cmd(PyObject *self, PyObject *args)
{
Q_UNUSED(self);
char *command;
char *result = (char*) "";
if (PyArg_ParseTuple(args, "s:command", &command))
{
result = Core()->cmd(command).toUtf8().data();
}
return PyUnicode_FromString(result);
}
PyMethodDef CutterMethods[] = {
{"version", api_version, METH_NOARGS,
"Returns Cutter current version"},
{"cmd", api_cmd, METH_VARARGS,
"Execute a command inside Cutter"},
{NULL, NULL, 0, NULL}
};
PyModuleDef CutterModule = {
PyModuleDef_HEAD_INIT, "cutter", NULL, -1, CutterMethods,
NULL, NULL, NULL, NULL
};
PyObject *PyInit_api()
{
return PyModule_Create(&CutterModule);
}
// -----------------------------
PyObject *api_internal_launch_ipykernel(PyObject *self, PyObject *args, PyObject *kw)
{
2018-02-22 18:51:02 +00:00
Q_UNUSED(self);
Q_UNUSED(kw);
QStringList argv;
2018-02-22 18:51:02 +00:00
PyObject *argvListObject;
2018-02-22 18:51:02 +00:00
if (!PyArg_ParseTuple(args, "O", &argvListObject)
|| !PyList_Check(argvListObject))
{
2018-02-22 18:51:02 +00:00
qWarning() << "Invalid args passed to api_internal_launch_ipykernel().";
return nullptr;
}
2018-02-22 18:51:02 +00:00
for (int i = 0; i < PyList_Size(argvListObject); i++)
{
PyObject *o = PyList_GetItem(argvListObject, i);
QString s = QString::fromUtf8(PyUnicode_AsUTF8(o));
argv.append(s);
}
2018-02-22 21:08:06 +00:00
long id = Jupyter()->startNestedIPyKernel(argv);
2018-02-22 21:08:06 +00:00
return PyLong_FromLong(id);
}
2018-02-22 21:08:06 +00:00
PyObject *api_internal_kernel_interface_kill(PyObject *, PyObject *args)
{
long id;
2018-02-22 21:08:06 +00:00
if (!PyArg_ParseTuple(args, "l", &id))
{
2018-02-22 21:08:06 +00:00
qWarning() << "Invalid args passed to api_internal_kernel_interface_kill().";
return nullptr;
}
2018-02-22 21:08:06 +00:00
Jupyter()->getNestedIPyKernel(id)->kill();
2018-02-22 21:08:06 +00:00
Py_RETURN_NONE;
}
PyMethodDef CutterInternalMethods[] = {
{"launch_ipykernel", (PyCFunction)api_internal_launch_ipykernel, METH_VARARGS | METH_KEYWORDS,
"Launch an IPython Kernel in a subinterpreter"},
2018-02-22 21:08:06 +00:00
{"kernel_interface_kill", (PyCFunction)api_internal_kernel_interface_kill, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
PyModuleDef CutterInternalModule = {
PyModuleDef_HEAD_INIT, "cutter_internal", NULL, -1, CutterInternalMethods,
NULL, NULL, NULL, NULL
};
PyObject *PyInit_api_internal()
{
return PyModule_Create(&CutterInternalModule);
}