From dc17c3c73e9897f2db2c8eeaeb893f2ec33ddc22 Mon Sep 17 00:00:00 2001 From: "alexey.stratulat" <alexey.stratulat@demlabs.net> Date: Fri, 31 May 2024 04:04:43 +0000 Subject: [PATCH] Feature 11714 --- CellFrame/python-cellframe_common.c | 41 +++++++++++++++++++ include/python-cellframe_common.h | 1 + .../chain/include/libdap-chain-python.h | 2 + .../chain/src/libdap-chain-python.c | 10 +++++ .../net/include/libdap_chain_net_python.h | 1 + .../net/src/libdap_chain_net_python.c | 11 +++++ modules/dap-sdk/core/src/libdap-python.c | 39 +----------------- 7 files changed, 68 insertions(+), 37 deletions(-) diff --git a/CellFrame/python-cellframe_common.c b/CellFrame/python-cellframe_common.c index 9f0a278f..7b43b139 100644 --- a/CellFrame/python-cellframe_common.c +++ b/CellFrame/python-cellframe_common.c @@ -57,3 +57,44 @@ void python_error_in_log_it(const char *a_tag){ PyErr_Restore(type, value, trackback); } + +PyObject *python_get_config_item(dap_config_t* a_config, const char *a_section, const char *a_key, PyObject *obj_default) { + dap_config_item_type_t l_type_item = dap_config_get_item_type( + a_config, a_section, a_key); + switch (l_type_item) { + case DAP_CONFIG_ITEM_UNKNOWN: { + if (obj_default != NULL) { + return obj_default; + } + PyErr_SetString(PyExc_ValueError, "Value can't be obtained. Either no such section or a key is missing in section"); + return NULL; + } + case DAP_CONFIG_ITEM_ARRAY: { + uint16_t l_values_count = 0; + char **l_values = dap_config_get_array_str(a_config, a_section, a_key, &l_values_count); + PyObject *obj_list = PyList_New(l_values_count); + for (uint16_t i = 0; i < l_values_count; i++) { + const char *l_value = l_values[i]; + PyObject *obj_unicode = PyUnicode_FromString(l_value); + PyList_SetItem(obj_list, i, obj_unicode); + } + return obj_list; + } + case DAP_CONFIG_ITEM_BOOL: { + if (dap_config_get_item_bool(a_config, a_section, a_key)) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } + case DAP_CONFIG_ITEM_DECIMAL: { + int res = dap_config_get_item_uint32(a_config, a_section, a_key); + return Py_BuildValue("i", res); + } + case DAP_CONFIG_ITEM_STRING: { + const char *res = dap_config_get_item_str(a_config, a_section, a_key); + return Py_BuildValue("s", res); + } + default:; + } + Py_RETURN_NONE; +} diff --git a/include/python-cellframe_common.h b/include/python-cellframe_common.h index 59527f56..3959944d 100644 --- a/include/python-cellframe_common.h +++ b/include/python-cellframe_common.h @@ -8,5 +8,6 @@ void python_error_in_log_it(const char *a_tag); void _PyErr_logIt(const dap_log_level_t a_level, const char *a_tag, const char *a_msg); +PyObject *python_get_config_item(dap_config_t* a_config, const char *a_section, const char *a_key, PyObject *obj_default); #define Py_BuildNone Py_BuildValue("") \ No newline at end of file diff --git a/modules/cellframe-sdk/chain/include/libdap-chain-python.h b/modules/cellframe-sdk/chain/include/libdap-chain-python.h index 9d0bc365..62989076 100644 --- a/modules/cellframe-sdk/chain/include/libdap-chain-python.h +++ b/modules/cellframe-sdk/chain/include/libdap-chain-python.h @@ -57,6 +57,8 @@ PyObject *dap_chain_python_get_cs_name(PyObject *self, PyObject *args); PyObject *dap_chain_python_get_net(PyObject *self, PyObject *args); +PyObject *dap_chain_python_get_config_item(PyObject *self, PyObject *args); + PyObject *PyDapChain_str(PyObject *self); extern PyTypeObject DapChainObjectType; diff --git a/modules/cellframe-sdk/chain/src/libdap-chain-python.c b/modules/cellframe-sdk/chain/src/libdap-chain-python.c index 4224766e..e52ebc8e 100644 --- a/modules/cellframe-sdk/chain/src/libdap-chain-python.c +++ b/modules/cellframe-sdk/chain/src/libdap-chain-python.c @@ -33,6 +33,7 @@ static PyMethodDef DapChainMethods[] = { {"getTransactions", (PyCFunction)dap_chain_python_get_txs, METH_VARARGS, ""}, {"getCSName", (PyCFunction)dap_chain_python_get_cs_name, METH_NOARGS, ""}, {"getNet", (PyCFunction) dap_chain_python_get_net, METH_NOARGS, ""}, + {"configGetItem", (PyCFunction)dap_chain_python_get_config_item, METH_VARARGS, ""}, {} }; @@ -525,3 +526,12 @@ PyObject *dap_chain_python_get_net(PyObject *self, PyObject *args){ obj_net->chain_net = dap_chain_net_by_id(((PyDapChainObject*)self)->chain_t->net_id); return (PyObject*)obj_net; } + +PyObject *dap_chain_python_get_config_item(PyObject *self, PyObject *args) { + const char *section_path; + const char *item_name; + PyObject *obj_def = NULL; + if (!PyArg_ParseTuple(args, "ss|O", §ion_path, &item_name, &obj_def)) + return NULL; + return python_get_config_item(((PyDapChainObject*)self)->chain_t->config, section_path, item_name, obj_def); +} diff --git a/modules/cellframe-sdk/net/include/libdap_chain_net_python.h b/modules/cellframe-sdk/net/include/libdap_chain_net_python.h index a5c6d336..adb9c01a 100644 --- a/modules/cellframe-sdk/net/include/libdap_chain_net_python.h +++ b/modules/cellframe-sdk/net/include/libdap_chain_net_python.h @@ -74,6 +74,7 @@ PyObject *dap_chain_net_get_chain_by_name_py(PyObject *self, PyObject *args); PyObject *dap_chain_net_get_cur_addr_py(PyObject *self, PyObject *args); PyObject *dap_chain_net_get_cur_cell_py(PyObject *self, PyObject *args); PyObject *dap_chain_net_get_cur_addr_int_py(PyObject *self, PyObject *args); +PyObject *dap_chain_net_get_config_by_item(PyObject *self, PyObject *args); PyObject *dap_chain_net_get_gdb_group_mempool_py(PyObject *self, PyObject *args); PyObject *dap_chain_net_get_gdb_group_mempool_by_chain_type_py(PyObject *self, PyObject *args); diff --git a/modules/cellframe-sdk/net/src/libdap_chain_net_python.c b/modules/cellframe-sdk/net/src/libdap_chain_net_python.c index e2c18db6..e62649af 100644 --- a/modules/cellframe-sdk/net/src/libdap_chain_net_python.c +++ b/modules/cellframe-sdk/net/src/libdap_chain_net_python.c @@ -26,6 +26,7 @@ static PyMethodDef DapChainNetMethods[] = { {"getName", dap_chain_net_get_name_py, METH_NOARGS, ""}, {"getTxByHash", dap_chain_net_get_tx_by_hash_py, METH_VARARGS, ""}, {"verifyCodeToStr", (PyCFunction)dap_chain_net_convert_verify_code_to_str, METH_VARARGS | METH_STATIC, ""}, + {"configGetItem", (PyCFunction)dap_chain_net_get_config_by_item, METH_VARARGS, ""}, {} }; @@ -211,6 +212,16 @@ PyObject *dap_chain_net_get_cur_addr_int_py(PyObject *self, PyObject *args){ return PyLong_FromUnsignedLongLong(res); } +PyObject *dap_chain_net_get_config_by_item(PyObject *self, PyObject *args){ + const char *section_path; + const char *item_name; + PyObject *obj_def = NULL; + if (!PyArg_ParseTuple(args, "ss|O", §ion_path, &item_name, &obj_def)) + return NULL; + return python_get_config_item(((PyDapChainNetObject *)self)->chain_net->pub.config, + section_path, item_name, obj_def); +} + PyObject *dap_chain_net_get_gdb_group_mempool_py(PyObject *self, PyObject *args){ PyObject *obj_chain; if (!PyArg_ParseTuple(args, "O", &obj_chain)) diff --git a/modules/dap-sdk/core/src/libdap-python.c b/modules/dap-sdk/core/src/libdap-python.c index eff8edef..6a2fde52 100644 --- a/modules/dap-sdk/core/src/libdap-python.c +++ b/modules/dap-sdk/core/src/libdap-python.c @@ -1,4 +1,5 @@ #include "libdap-python.h" +#include "python-cellframe_common.h" #define LOG_TAG "libdap-python" @@ -205,43 +206,7 @@ PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ PyObject *obj_def = NULL; if (!PyArg_ParseTuple(args, "ss|O", §ion_path, &item_name, &obj_def)) return NULL; - dap_config_item_type_t l_type_item = dap_config_get_item_type(g_config, section_path, item_name); - switch (l_type_item) { - case DAP_CONFIG_ITEM_UNKNOWN: { - if (obj_def != NULL) { - return obj_def; - } - PyErr_SetString(PyExc_ValueError, "Value can't be obtained. Either no such section or a key is missing in section"); - return NULL; - } - case DAP_CONFIG_ITEM_ARRAY: { - uint16_t l_values_count = 0; - char **l_values = dap_config_get_array_str(g_config, section_path, item_name, &l_values_count); - PyObject *obj_list = PyList_New(l_values_count); - for (uint16_t i = 0; i < l_values_count; i++) { - const char *l_value = l_values[i]; - PyObject *obj_unicode = PyUnicode_FromString(l_value); - PyList_SetItem(obj_list, i, obj_unicode); - } - return obj_list; - } - case DAP_CONFIG_ITEM_BOOL: { - if (dap_config_get_item_bool(g_config, section_path, item_name)) - Py_RETURN_TRUE; - else - Py_RETURN_FALSE; - } - case DAP_CONFIG_ITEM_DECIMAL: { - int res = dap_config_get_item_uint32(g_config, section_path, item_name); - return Py_BuildValue("i", res); - } - case DAP_CONFIG_ITEM_STRING: { - const char *res = dap_config_get_item_str(g_config, section_path, item_name); - return Py_BuildValue("s", res); - } - default:; - } - Py_RETURN_NONE; + return python_get_config_item(g_config, section_path, item_name, obj_def); } PyObject *dapListToPyList(dap_list_t *list) { -- GitLab