Skip to content
Snippets Groups Projects
Unverified Commit e8d7f4e8 authored by Dmitriy A. Gerasimov's avatar Dmitriy A. Gerasimov Committed by GitHub
Browse files

Merge pull request #3 from cellframe/features-2308

Features 2308 - Additional work on the task.
parents 7c42ed23 014bbbcd
No related branches found
No related tags found
1 merge request!26Support 3689
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
# libdap-python
Python binding for libdap
[![Build Status](https://travis-ci.com/cellframe/libdap-python.svg?branch=master)](https://travis-ci.com/cellframe/libdap-python)
......@@ -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){
......
#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
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)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment