diff --git a/include/wrapping_dap_chain_net_node_cli.h b/include/wrapping_dap_chain_net_node_cli.h new file mode 100644 index 0000000000000000000000000000000000000000..f07eeb78e2241d0a7cb87d5801b8c851604a8e07 --- /dev/null +++ b/include/wrapping_dap_chain_net_node_cli.h @@ -0,0 +1,81 @@ +#ifndef _WRAPPING_DAP_CHAIN_NET_NODE_CLI_ +#define _WRAPPING_DAP_CHAIN_NET_NODE_CLI_ + +#include <Python.h> +#include "dap_config.h" +#include "dap_chain_node_cli.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +typedef struct PyDapChainNodeCli{ + PyObject_HEAD + cmdfunc_t *func; +}PyDapChainNodeCliObject; +static PyObject *binded_object_cmdfunc = NULL; + +int dap_chain_node_cli_init_py(dap_config_t *g_config); +void dap_chain_node_cli_delete_py(void); + +PyObject *DapChainNodeCliObject_new(PyTypeObject *type_object, PyObject *args, PyObject *kwds); + +PyObject *dap_chain_node_cli_cmd_item_create_py(PyObject *self, PyObject *args); +PyObject *dap_chain_node_cli_set_reply_text_py(PyObject *self, PyObject *args); + +static PyMethodDef DapChainNodeCliMethods[] = { + {"cmdItemCreate", dap_chain_node_cli_cmd_item_create_py, METH_VARARGS, ""}, + {"setReplyText", dap_chain_node_cli_set_reply_text_py, METH_VARARGS, ""}, + {NULL, NULL, 0, NULL} +}; + +static PyTypeObject DapChainNodeCliObject_DapChainNodeCliObjectType = { + PyVarObject_HEAD_INIT(NULL, 0) + "CellFrame.Chain.Node.cli", /* tp_name */ + sizeof(PyDapChainNodeCliObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Chain net node cli object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DapChainNodeCliMethods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + DapChainNodeCliObject_new, /* tp_new */ +}; + +char **PyListToString(PyObject *list); +PyObject *stringToPyList(char **list); + +#ifdef __cplusplus +} +#endif + +#endif //_WRAPPING_DAP_CHAIN_NET_NODE_CLI_ diff --git a/src/wrapping_dap_chain_net_node_cli.c b/src/wrapping_dap_chain_net_node_cli.c new file mode 100644 index 0000000000000000000000000000000000000000..709fcb5aff54508f6d7f15fb83975c99c79b63a8 --- /dev/null +++ b/src/wrapping_dap_chain_net_node_cli.c @@ -0,0 +1,79 @@ +#include "wrapping_dap_chain_net_node_cli.h" + +int dap_chain_node_cli_init_py(dap_config_t *g_config){ + dap_chain_node_cli_init(g_config); +} +void dap_chain_node_cli_delete_py(void){ + dap_chain_node_cli_delete(); +} + +static int wrapping_cmdfunc(int argc, char **argv, char **str_reply){ + PyObject *arglist; + PyObject *result; + PyObject *obj_argv = stringToPyList(argv); + PyObject *obj_str_reply = stringToPyList(str_reply); + arglist = Py_BuildValue("i|O|O", argc, obj_argv, obj_str_reply); + result = PyObject_CallObject(binded_object_cmdfunc, arglist); + Py_DECREF(arglist); + Py_DECREF(obj_argv); + Py_DECREF(obj_str_reply); + int r = -1; + if (PyLong_Check(result)){ + r = (int)PyLong_AsLong(result); + } + Py_DECREF(result); + return r; +} + +PyObject *DapChainNodeCliObject_new(PyTypeObject *type_object, PyObject *args, PyObject *kwds){ + PyDapChainNodeCliObject *obj = (PyDapChainNodeCliObject*)PyType_GenericNew(type_object, args, kwds); + obj->func = wrapping_cmdfunc; + return (PyObject *)obj; +} + +PyObject *dap_chain_node_cli_cmd_item_create_py(PyObject *self, PyObject *args){ + const char *name, *doc, *doc_ex; + PyObject *obj_cmdfunc; + if (!PyArg_ParseTuple(args, "s|O:set_callback|s|s", &name, &obj_cmdfunc, &doc, &doc_ex)){ + return NULL; + } else { + if (!PyCallable_Check(obj_cmdfunc)){ + PyErr_SetString(PyExc_TypeError, "parameter must be callable"); + return NULL; + } + } + Py_XINCREF(obj_cmdfunc); + Py_XDECREF(binded_object_cmdfunc); + binded_object_cmdfunc = obj_cmdfunc; + dap_chain_node_cli_cmd_item_create(name, ((PyDapChainNodeCliObject*)obj_cmdfunc)->func, doc, doc_ex); + return PyLong_FromLong(0); +} + +PyObject *dap_chain_node_cli_set_reply_text_py(PyObject *self, PyObject *args){ + PyObject *obj_str_reply_list; + const char *str_list; + if (!PyArg_ParseTuple(args, "O|O", &obj_str_reply_list)) + return NULL; + char **str_reply_list = PyListToString(obj_str_reply_list); + dap_chain_node_cli_set_reply_text(str_reply_list, str_list); + return PyLong_FromLong(0); +} + +char **PyListToString(PyObject *list){ + Py_ssize_t size = PyList_Size(list); + char **res = calloc(sizeof(char**), (size_t)size); + for (Py_ssize_t i=0; i < size;i++ ){ + char *data = PyBytes_AsString(PyList_GetItem(list, i)); + res[(size_t)i] = data; + } + return res; +} +PyObject *stringToPyList(char **list){ + size_t size = sizeof(list) / sizeof(list[0]); + PyObject *obj = PyList_New((Py_ssize_t)size); + for (size_t i=0; i < size; i++){ + PyObject *data = PyBytes_FromString(list[i]); + PyList_Append(obj, data); + } + return obj; +}