From a7e729e18a0e263cc2d3a96fed1cfb1f0615af16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Ba=C5=9Faran?= Date: Mon, 7 Mar 2016 17:13:03 +0200 Subject: [PATCH] ahenk db service --- etc/ahenk/ahenk.conf | 1 + opt/ahenk/base/config/ConfigManager.py | 3 +- opt/ahenk/base/database/AhenkDao.py | 11 --- opt/ahenk/base/database/AhenkDbService.py | 83 +++++++++++++++++++++++ opt/ahenk/base/model/Policy.py | 9 +++ opt/ahenk/base/model/Task.py | 24 +++++-- opt/ahenk/base/plugin/PluginManager.py | 6 +- 7 files changed, 114 insertions(+), 23 deletions(-) delete mode 100644 opt/ahenk/base/database/AhenkDao.py create mode 100644 opt/ahenk/base/database/AhenkDbService.py create mode 100644 opt/ahenk/base/model/Policy.py diff --git a/etc/ahenk/ahenk.conf b/etc/ahenk/ahenk.conf index 2167ba6..bb3a272 100644 --- a/etc/ahenk/ahenk.conf +++ b/etc/ahenk/ahenk.conf @@ -1,5 +1,6 @@ [BASE] logconfigurationfilepath = /etc/ahenk/log.conf +dbPath=/etc/ahenk/ahenk.db [PLUGIN] pluginfolderpath = /home/volkan/devzone/ diff --git a/opt/ahenk/base/config/ConfigManager.py b/opt/ahenk/base/config/ConfigManager.py index 8e54712..ca79415 100644 --- a/opt/ahenk/base/config/ConfigManager.py +++ b/opt/ahenk/base/config/ConfigManager.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN -import configparser,os +import os,configparser from os import listdir from os.path import isfile, join from configparser import SafeConfigParser @@ -15,7 +15,6 @@ class ConfigManager(object): is configuration files folder path """ def __init__(self, configurationFilePath=None, configurationFolderPath=None): - super(ConfigManager, self).__init__() self.configurationFilePath = configurationFilePath self.configurationFolderPath = configurationFolderPath diff --git a/opt/ahenk/base/database/AhenkDao.py b/opt/ahenk/base/database/AhenkDao.py deleted file mode 100644 index 14430d4..0000000 --- a/opt/ahenk/base/database/AhenkDao.py +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- -# Author: İsmail BAŞARAN - - -class AhenkDao(object): - """ - Sqlite manager for ahenk - """ - def __init__(self): - super(AhenkDao, self).__init__() diff --git a/opt/ahenk/base/database/AhenkDbService.py b/opt/ahenk/base/database/AhenkDbService.py new file mode 100644 index 0000000..2e13ad6 --- /dev/null +++ b/opt/ahenk/base/database/AhenkDbService.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN +from base.Scope import Scope +import sqlite3 + +class AhenkDbService(object): + """ + Sqlite manager for ahenk + """ + def __init__(self): + scope = Scope.getInstance() + self.logger = scope.getLogger() + self.configurationManager = scope.getConfigurationManager() + self.db_path=self.configurationManager.get("BASE","dbPath") + self.connection=None + self.cursor = None + + def connect(): + try: + self.connection=sqlite3.connect(self.db_path, check_same_thread=False) + self.cursor = self.connection.cursor() + except Exception as e: + self.logger.error('Database connection error ' + str(e)) + + def check_and_create_table(self,table_name,cols): + if self.cursor: + cols = ', '.join([str(x) for x in cols]) + self.cursor.execute("create table if not exists "+table_name+" ("+cols+")") + else + self.warn("Could not create table cursor is None! Table Name : " + str(table_name)) + + def update(self, table_name, cols, args, criteria=None): + try: + if self.connection: + if criteria == None: + cols = ', '.join([str(x) for x in cols]) + params = ', '.join(["?" for x in args]) + sql = "INSERT INTO "+table_name+" ("+cols+") VALUES ("+params+")" + self.cursor.execute(sql, tuple(args)) + else: + update_list = "" + params = ', '.join(["?" for x in args]) + for index in range(len(cols)): + update_list = update_list + " " + cols[index] +" = ?," + + update_list = update_list.strip(',') + sql = "UPDATE "+table_name+" SET " + update_list + " " + criteria + self.cursor.execute(sql, tuple(args)) + self.connection.commit() + else: + self.warn("Could not update table cursor is None! Table Name : " + str(table_name)) + except Exception, e: + self.logger.error("Updating table error ! Table Name : " + str(table_name) + " " + str(e)) + + def delete(self): + sql = "DELETE FROM " + table_name + str(criteria) + self.cursor.execute(sql) + self.connection.commit() + + def findByProperty(self): + # Not implemented yet + pass + def select(self,table_name, cols="*", criteria="", orderby=""): + if self.cursor: + try: + if not cols == "*": + cols = ', '.join([str(x) for x in cols]) + sql = "SELECT "+cols+" FROM " + table_name + " " + str(criteria) + " " + orderby + self.cursor.execute(sql) + rows = self.cursor.fetchall() + return rows + except Exception as e: + raise + else: + self.warn("Could not select table cursor is None! Table Name : " + str(table_name)) + + def close(self): + try: + self.cursor.close() + self.connection.close() + except Exception as e: + self.logger.error("Closing database connection error " + str(e)) diff --git a/opt/ahenk/base/model/Policy.py b/opt/ahenk/base/model/Policy.py new file mode 100644 index 0000000..05841de --- /dev/null +++ b/opt/ahenk/base/model/Policy.py @@ -0,0 +1,9 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN +import json + +class Policy(object): + """docstring for Policy""" + def __init__(self,message): + self.message = message diff --git a/opt/ahenk/base/model/Task.py b/opt/ahenk/base/model/Task.py index 29603f7..3c1846e 100644 --- a/opt/ahenk/base/model/Task.py +++ b/opt/ahenk/base/model/Task.py @@ -1,16 +1,26 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN +import json class Task(object): """docstring for Task""" def __init__(self,message): - super(Task, self).__init__() + self.payload = json.loads(message) + self.request = self.payload[u'request'] - def getPluginId(self): - # Not implemented yet - pass + @property + def getPluginName(self): + self.request[u'pluginName'] - def getcommandId(self): - # Not implemented yet - pass + @property + def getCommandId(self): + self.request[u'commandId'] + + @property + def params(self): + self.request[u'parameterMap'] + + @property + def pluginVersion(self): + self.request[u'pluginVersion'] diff --git a/opt/ahenk/base/plugin/PluginManager.py b/opt/ahenk/base/plugin/PluginManager.py index 4a0fd1e..4ab6d1e 100644 --- a/opt/ahenk/base/plugin/PluginManager.py +++ b/opt/ahenk/base/plugin/PluginManager.py @@ -11,11 +11,11 @@ class PluginManager(object): #implement logger def __init__(self): super(PluginManager, self).__init__() - scope = Scope.getInstance() - self.configManager = scope.getConfigurationManager() + self.scope = Scope.getInstance() + self.configManager = self.scope.getConfigurationManager() self.plugins = [] self.pluginQueueDict = dict() - self.logger = scope.getLogger() + self.logger = self.scope.getLogger() def loadPlugins(self): print("loading")