diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000000000000000000000000000000000..8ad3151fc89f4fa2fc0d33ad751955559a6a67bd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,31 @@ +sudo: required +language: python +python: + - "3.7-dev" +compiler: gcc +dist: xenial +notifications: + email: false + +before_install: + - git submodule init + - git submodule update + +install: + - sudo apt-get install gcc make + +script: + - sudo service network-manager start + - mkdir build + - cd build + - cmake -DBUILD_DAP_PYTHON_TESTS=ON ../ + - make + - python3.7 main_test.py + +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - network-manager + diff --git a/README.md b/README.md index 828a2da6bbe3e27687df60cd51d4b4d5f9c035e9..22d51fd196b3379f51c9bb498d152b813f32adfc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # libdap-python Python binding for libdap + +[](https://travis-ci.com/cellframe/libdap-python) diff --git a/src/libdap-python.c b/src/libdap-python.c index 6552a49df1f08063275272e20e7647118e9dcbf4..58e4bb51dc0599b44201e21f25a5563437c7153a 100644 --- a/src/libdap-python.c +++ b/src/libdap-python.c @@ -25,18 +25,21 @@ static PyObject *dap_init(PyObject *self, PyObject *args){ return PyLong_FromLong(0); } -static PyObject *dap_deinit(PyObject *self){ +static PyObject *dap_deinit(){ + log_it(L_DEBUG, "Running function dap_deinit"); + dap_config_close(g_config); + log_it(L_DEBUG, "Config file closed."); dap_config_deinit(); - dap_common_deinit(); + log_it(L_DEBUG, "Function dap_config_deinit done."); + log_it(L_DEBUG, "Function dap_deinit done."); return PyLong_FromLong(0); } static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ - const char *data; - if (!PyArg_ParseTuple(args, "s", &data)) + short int new_log_level; + if (!PyArg_ParseTuple(args, "h", &new_log_level)) return NULL; - dap_log_level_t new_log_level = convert_const_char_to_dap_log_level(data); - if (new_log_level == -1) { + if (new_log_level < 0 || new_log_level > 5 ) { return PyLong_FromLong(-1); } else { dap_log_level_set(new_log_level); @@ -44,39 +47,17 @@ static PyObject *dap_set_log_level(PyObject *self, PyObject *args){ } } -static dap_log_level_t convert_const_char_to_dap_log_level(const char* string){ - if (strcmp(string,"DEBUG") == 0){ - return L_DEBUG; - } - if (strcmp(string, "INFO") == 0){ - return L_INFO; - } - if (strcmp(string, "NOTICE") == 0){ - return L_NOTICE; - } - if (strcmp(string, "WARNING") == 0){ - return L_WARNING; - } - if (strcmp(string, "ERROR") == 0){ - return L_ERROR; - } - if (strcmp(string, "CRITICAL") == 0){ - return L_CRITICAL; - } - return -1; -} - static PyObject* dap_log_it(PyObject* self, PyObject* args){ - const char* dap_log_leve_char; + short int log_level; const char* string_output; - if (!PyArg_ParseTuple(args, "s|s", &dap_log_leve_char, &string_output)) + if (!PyArg_ParseTuple(args, "h|s", &log_level, &string_output)) return NULL; - dap_log_level_t log_level = convert_const_char_to_dap_log_level(dap_log_leve_char); - if (log_level == -1) + if (log_level < 0 || log_level > 5 ) { return PyLong_FromLong(-1); - log_it(log_level, string_output); - - return PyLong_FromLong(0); + } else { + log_it(log_level, string_output); + return PyLong_FromLong(0); + } } static PyObject* py_m_dap_config_get_item(PyObject *self, PyObject *args){ diff --git a/src/libdap-python.h b/src/libdap-python.h index 1aac3c6ab27878ef1aec09acc26d47431d35ef9f..360f564b1b9ef942abd6838e83b00ce311b2f535 100644 --- a/src/libdap-python.h +++ b/src/libdap-python.h @@ -1,5 +1,5 @@ #define PY_SSIZE_T_CLEAN -#include <python3.7/Python.h> +#include <Python.h> #include "dap_config.h" #include "dap_common.h" @@ -11,14 +11,12 @@ extern "C" { static PyObject *dap_init(PyObject *self, PyObject *args); -static PyObject *dap_deinit(PyObject *self); +static PyObject *dap_deinit(); static PyObject *dap_set_log_level(PyObject *self, PyObject *args); 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); @@ -46,4 +44,4 @@ PyMODINIT_FUNC PyInit_libdap_python_module(void); #ifdef __cplusplus } -#endif \ No newline at end of file +#endif diff --git a/src/libdapConnector.py b/src/libdapConnector.py index 73a8d3485902b79b075d90a728d99b420217ee2c..b0165a72056c03606edd2b1540f051f42cc6eb01 100644 --- a/src/libdapConnector.py +++ b/src/libdapConnector.py @@ -1,36 +1,44 @@ import json +from enum import Enum import libdap_python_module class DapIniException(Exception): pass -class Dap: - def __init__(self, data): - res = json.loads(data) - self.modules=res['modules'] - 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'], res['dap']['application_name']) - if res_init == -1 or res_init == -2: - raise DapIniException("Initialization erorr") - def __del__(self): - libdap_python_module.deinit() - def setLogLevel(self, data): - self.log_level=data - res_setLogLevel = libdap_python_module.setLogLevel(data) - if res_setLogLevel == -1: - 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'], 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) +class LogLevel(Enum): + L_CRITICAL=5 + L_ERROR=4 + L_WARNING=3 + L_NOTICE=2 + L_INFO=1 + L_DEBUG=0 + +def init(data): + res = json.loads(data) + modules=res['modules'] + config_dir=res['dap']['config_dir'] + log_level=res['dap']['log_level'] + application_name=res['dap']['application_name'] + res_init = libdap_python_module.init(config_dir, application_name) + if res_init == -1 or res_init == -2: + raise DapIniException("Initialization erorr") + if log_level != LogLevel.L_NOTICE.name: + setLogLevel(LogLevel[log_level]) +def deInit(): + libdap_python_module.deinit() +def setLogLevel(logLevel): + res_setLogLevel = libdap_python_module.setLogLevel(logLevel.value) + if res_setLogLevel == -1: + raise DapIniException("Failed to set the logging level, perhaps you did not correctly specify the name of the level") +def logIt(logLevel, data): + res_log_it = libdap_python_module.logIt(logLevel.value, 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(section_path, item_name): + res = libdap_python_module.configGetItem(section_path, item_name) + return res +def configGetItemDefault(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 45ed2293fdda8195daf4107f04a82f4a009ea348..477febf7f18dcfd70ec6ebaa61c3e29319fca71c 100644 --- a/test/main_test.py +++ b/test/main_test.py @@ -1,25 +1,42 @@ -import libdapConnector +from libdapConnector import * +import os +import sys + +def create_config_file(app_name): + f = open(app_name+".cfg", "w") + f.write("[server]\nlisten_address=0.0.0.0\n") + f.close() + +logIt(LogLevel.L_INFO,"Start main test") +app_name = "TestAPP" +logIt(LogLevel.L_INFO, "Create config file") +create_config_file(app_name) + json_string = """{ - "modules": "libdap", + "modules": "", "dap": { - "config_dir": "/opt/dap/etc", - "log_level": "Debug", - "application_name": "TestAPP" + "config_dir": \""""+os.getcwd()+"""\", + "log_level": \""""+LogLevel.L_DEBUG.name+"""\", + "application_name": \""""+app_name+"""\" } }""" -print("Standard Configuration \n"+json_string) -daptest = libdapConnector.Dap(json_string) -print("Initialization of the DAP") -daptest.setLogLevel("DEBUG") -print("Level logging ""DEBUG"" done") -daptest.logIt("""{ - "level": "DEBUG", - "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") +init(json_string) +logIt(LogLevel.L_INFO, "Initialization of the DAP done") +setLogLevel(LogLevel.L_DEBUG) +logIt(LogLevel.L_INFO,"Level logging ""DEBUG"" done") +logIt(LogLevel.L_DEBUG, "Test. Outputting a string using the log_it function in the libdap library") +logIt(LogLevel.L_INFO,"Outputting a string using the log_it function done") +res1 = configGetItem("server", "listen_address") +logIt(LogLevel.L_INFO, "Output [server] 'listen_address' = "+res1+"\n") +res2 = configGetItemDefault("server1", "listen_address", "8.8.8.8") +logIt(LogLevel.L_INFO, "Output default value '8.8.8.8' [server1] 'listen_address' = "+res2+"\n") +logIt(LogLevel.L_INFO, "TEST. Get default config done") +deInit() +logIt(LogLevel.L_INFO, "Deinitialization done") + +os.remove(app_name+".cfg") +logIt(LogLevel.L_INFO, "Dellete config file") +logIt(LogLevel.L_INFO, "Main test done"); + +sys.exit(0)