1 
2 #ifdef CUTTER_ENABLE_PYTHON
3 
4 #include "PythonAPI.h"
5 #include "core/Cutter.h"
6 
7 #include "CutterConfig.h"
8 
9 #include <QFile>
10 
api_version(PyObject * self,PyObject * null)11 PyObject *api_version(PyObject *self, PyObject *null)
12 {
13     Q_UNUSED(self)
14     Q_UNUSED(null)
15     return PyUnicode_FromString(CUTTER_VERSION_FULL);
16 }
17 
api_cmd(PyObject * self,PyObject * args)18 PyObject *api_cmd(PyObject *self, PyObject *args)
19 {
20     Q_UNUSED(self);
21     char *command;
22     char *result = (char *) "";
23     QString cmdRes;
24     QByteArray cmdBytes;
25     if (PyArg_ParseTuple(args, "s:command", &command)) {
26         cmdRes = Core()->cmd(command);
27         cmdBytes = cmdRes.toLocal8Bit();
28         result = cmdBytes.data();
29     }
30     return PyUnicode_FromString(result);
31 }
32 
api_refresh(PyObject * self,PyObject * args)33 PyObject *api_refresh(PyObject *self, PyObject *args)
34 {
35     Q_UNUSED(self);
36     Q_UNUSED(args);
37     Core()->triggerRefreshAll();
38     return Py_None;
39 }
40 
api_message(PyObject * self,PyObject * args,PyObject * kwargs)41 PyObject *api_message(PyObject *self, PyObject *args, PyObject *kwargs)
42 {
43     Q_UNUSED(self);
44     char *message;
45     int debug = 0;
46     static const char *kwlist[] = { "", "debug", NULL };
47     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i",
48                                      const_cast<char**>(kwlist),
49                                      &message, &debug)) {
50         return NULL;
51     }
52     Core()->message(QString(message), debug);
53     Py_INCREF(Py_None);
54     return Py_None;
55 }
56 
57 PyMethodDef CutterMethods[] = {
58     {
59         "version", api_version, METH_NOARGS,
60         "Returns Cutter current version"
61     },
62     {
63         "cmd", api_cmd, METH_VARARGS,
64         "Execute a command inside Cutter"
65     },
66     {
67         "refresh", api_refresh, METH_NOARGS,
68         "Refresh Cutter widgets"
69     },
70     {
71         "message", (PyCFunction)(void *)/* don't remove this double cast! */api_message, METH_VARARGS | METH_KEYWORDS,
72         "Print message"
73     },
74     {NULL, NULL, 0, NULL}
75 };
76 
77 PyModuleDef CutterModule = {
78     PyModuleDef_HEAD_INIT, "_cutter", NULL, -1, CutterMethods,
79     NULL, NULL, NULL, NULL
80 };
81 
PyInit_api()82 PyObject *PyInit_api()
83 {
84     return PyModule_Create(&CutterModule);
85 }
86 
87 #endif  // CUTTER_ENABLE_PYTHON
88