diff --git a/src/libdap-python.c b/src/libdap-python.c index 79bb87ebae28dc1c0580e32a75f8a79ce60434f6..6552a49df1f08063275272e20e7647118e9dcbf4 100644 --- a/src/libdap-python.c +++ b/src/libdap-python.c @@ -1,35 +1,17 @@ #include "libdap-python.h" static PyObject *dap_init(PyObject *self, PyObject *args){ - const char *data; - char *system_configs_dir; - char *dap_app_name; + //const char *data; + const char *system_configs_dir; + const char *dap_app_name; char *dap_app_name_logs; - if (!PyArg_ParseTuple(args, "s", &data)) + if (!PyArg_ParseTuple(args, "s|s", &system_configs_dir, &dap_app_name)) return NULL; - int lenSystemConfigDir=0; - int lenDapAppName=0; - int countSeparators=0; - int lenMassives = 0; - while (*(data+lenMassives) != '\0'){ - if (*(data+lenMassives)=='\n'){ - countSeparators += 1; - }else { - if (countSeparators == 0) - lenSystemConfigDir++; - if (countSeparators == 1) - lenDapAppName++; - } - lenMassives++; - } - system_configs_dir = calloc(lenSystemConfigDir, sizeof(char)); - dap_app_name = calloc(lenDapAppName, sizeof(char)); - dap_app_name_logs = calloc((lenDapAppName+9), sizeof(char)); - memcpy(system_configs_dir,data,lenSystemConfigDir); - memcpy(dap_app_name, data+lenSystemConfigDir+1, lenDapAppName); - memcpy(dap_app_name_logs, dap_app_name, lenDapAppName); + int len_dap_app_name = strlen(dap_app_name); + dap_app_name_logs = calloc((len_dap_app_name+9), sizeof(char)); + memcpy(dap_app_name_logs, dap_app_name, len_dap_app_name); const char* log = "_logs.txt"; - memcpy(dap_app_name_logs+lenDapAppName, log,9); + memcpy(dap_app_name_logs+len_dap_app_name, log,9); dap_config_init(system_configs_dir); if ((g_config = dap_config_open(dap_app_name) ) == NULL){ log_it(L_CRITICAL, "Can't init general configurations"); @@ -85,32 +67,10 @@ static dap_log_level_t convert_const_char_to_dap_log_level(const char* string){ } static PyObject* dap_log_it(PyObject* self, PyObject* args){ - const char *data; - if (!PyArg_ParseTuple(args, "s", &data)) + const char* dap_log_leve_char; + const char* string_output; + if (!PyArg_ParseTuple(args, "s|s", &dap_log_leve_char, &string_output)) return NULL; - char* dap_log_leve_char; - char* string_output; - int len_log_level_char=0; - int len_string_output=0; - int countSeparators=0; - int lenMassives = 0; - while (*(data+lenMassives) != '\0'){ - if (*(data+lenMassives)=='\n'){ - countSeparators += 1; - }else { - if (countSeparators == 0) - len_log_level_char++; - if (countSeparators == 1) - len_string_output++; - } - lenMassives++; - } - if (len_log_level_char == 0 || len_string_output == 0) - return PyLong_FromLong(-1); - dap_log_leve_char = calloc(len_log_level_char, sizeof(char)); - string_output = calloc(len_string_output, sizeof(char)); - memcpy(dap_log_leve_char, data, len_log_level_char); - memcpy(string_output, data+len_log_level_char+1, len_string_output); dap_log_level_t log_level = convert_const_char_to_dap_log_level(dap_log_leve_char); if (log_level == -1) return PyLong_FromLong(-1); @@ -119,6 +79,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 f43a70d79bc48e1357c94fc849c27a3ae851474c..1aac3c6ab27878ef1aec09acc26d47431d35ef9f 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 ddcd081a3e7cda40a21564afb92cd0d0cb2a4d9c..73a8d3485902b79b075d90a728d99b420217ee2c 100644 --- a/src/libdapConnector.py +++ b/src/libdapConnector.py @@ -11,7 +11,7 @@ class Dap: self.config_dir=res['dap']['config_dir'] self.log_level=res['dap']['log_level'] self.application_name=res['dap']['application_name'] - res_init = libdap_python_module.init(res['dap']['config_dir']+"\n"+res['dap']['application_name']+"\n") + res_init = libdap_python_module.init(res['dap']['config_dir'], res['dap']['application_name']) if res_init == -1 or res_init == -2: raise DapIniException("Initialization erorr") def __del__(self): @@ -23,8 +23,14 @@ class Dap: raise DapIniException("Failed to set the logging level, perhaps you did not correctly specify the name of the level") def logIt(self, data): parse_data = json.loads(data) - res_log_it = libdap_python_module.logIt(parse_data['level']+"\n"+parse_data['data']) + res_log_it = libdap_python_module.logIt(parse_data['level'], 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 7fbb113e94c03371acf3a12b555fa07b4d9a8987..45ed2293fdda8195daf4107f04a82f4a009ea348 100644 --- a/test/main_test.py +++ b/test/main_test.py @@ -3,7 +3,7 @@ import libdapConnector json_string = """{ "modules": "libdap", "dap": { - "config_dir": "/opt/dap/etc/", + "config_dir": "/opt/dap/etc", "log_level": "Debug", "application_name": "TestAPP" } @@ -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")