From ab303446f81cf4f65d87a1faea2fee9fce99162b Mon Sep 17 00:00:00 2001 From: Alexey Stratulat <alexey.stratulat@demlabs.net> Date: Wed, 26 Jun 2019 21:30:59 +0700 Subject: [PATCH] Added functions configGetItem and configGetItemDefault. They accept a section, a key, and the second function accepts a default value. These functions take parameters as strings and return string. Also added a test for these functions. --- src/libdap-python.c | 23 +++++++++++++++++++++++ src/libdap-python.h | 6 ++++++ src/libdapConnector.py | 6 ++++++ test/main_test.py | 5 +++++ 4 files changed, 40 insertions(+) diff --git a/src/libdap-python.c b/src/libdap-python.c index 79bb87eb..ffe602cd 100644 --- a/src/libdap-python.c +++ b/src/libdap-python.c @@ -119,6 +119,29 @@ static PyObject* dap_log_it(PyObject* self, PyObject* args){ return PyLong_FromLong(0); } +static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ + const char *section_path; + const char *item_name; + if (!PyArg_ParseTuple(args, "s|s", §ion_path, &item_name)) + return NULL; + const char *res = dap_config_get_item_str(g_config, section_path, item_name); + if (res == NULL){ + PyErr_SetString(PyExc_ValueError, "The value could not be obtained. Or there is no section. Or missing key in the section."); + return NULL; + } + return Py_BuildValue("s", res); +} + +static PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args){ + const char *section_path; + const char *item_name; + const char *def; + if (!PyArg_ParseTuple(args, "s|s|s", §ion_path, &item_name, &def)) + return NULL; + const char *res = dap_config_get_item_str_default(g_config, section_path, item_name, def); + return Py_BuildValue("s", res); +} + PyMODINIT_FUNC PyInit_libdap_python_module(void){ return PyModule_Create(&dapmodule); } diff --git a/src/libdap-python.h b/src/libdap-python.h index f43a70d7..1aac3c6a 100644 --- a/src/libdap-python.h +++ b/src/libdap-python.h @@ -19,11 +19,17 @@ static PyObject* dap_log_it(PyObject* self, PyObject* args); static dap_log_level_t convert_const_char_to_dap_log_level(const char* string); +static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args); + +static PyObject* py_m_dap_config_get_item_default(PyObject *self, PyObject *args); + static PyMethodDef DapMethods[] = { {"init", dap_init, METH_VARARGS, "Initialization of the DAP (Deus Applicaions Prototypes) library"}, {"deinit", dap_deinit, METH_NOARGS, "Deinitialization of the DAP (Deus Applicaions Prototypes) library"}, {"setLogLevel", dap_set_log_level, METH_VARARGS, "Setting the logging level"}, {"logIt", dap_log_it, METH_VARARGS, "The wrapper of the log_it function for the libdap library"}, + {"configGetItem", py_m_dap_config_get_item, METH_VARARGS, ""}, + {"configGetItemDefault", py_m_dap_config_get_item_default, METH_VARARGS, ""}, {NULL, NULL, 0, NULL} }; diff --git a/src/libdapConnector.py b/src/libdapConnector.py index ddcd081a..a548b9c8 100644 --- a/src/libdapConnector.py +++ b/src/libdapConnector.py @@ -26,5 +26,11 @@ class Dap: res_log_it = libdap_python_module.logIt(parse_data['level']+"\n"+parse_data['data']) if res_log_it == -1: raise DapIniException("Could not execute log_it function. Perhaps you did not correctly specify the name of the logging level or did not leave the information that needs to be displayed") + def configGetItem(self, section_path, item_name): + res = libdap_python_module.configGetItem(section_path, item_name) + return res + def configGetItemDefault(self, section_path, item_name, default): + return libdap_python_module.configGetItemDefault(section_path, item_name, default) + diff --git a/test/main_test.py b/test/main_test.py index 7fbb113e..a76e15a2 100644 --- a/test/main_test.py +++ b/test/main_test.py @@ -18,3 +18,8 @@ daptest.logIt("""{ "data": "Test. Outputting a string using the log_it function in the libdap library" }""") print("Outputting a string using the log_it function done") +res1 = daptest.configGetItem("server", "listen_address") +print("Output [server] 'listen_address' = "+res1+"\n") +res2 = daptest.configGetItemDefault("server1", "listen_address", "8.8.8.8") +print("Output default value '8.8.8.8' \n [server1] 'listen_address' = "+res2+"\n") +print("TEST. Get default config done") -- GitLab