# Path: myDevices/utils/logger.py
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def warn(message):
#     if checkFlood(message):
#         return
#     LOGGER.warn(message)
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# def setDebug():
#     LOGGER.setLevel(DEBUG)
#     return
# 
# Path: myDevices/utils/singleton.py
# class Singleton(_Singleton('SingletonMeta', (object,), {})): pass


from sqlite3 import connect
from myDevices.utils.logger import exception, info, warn, error, debug, setDebug
from threading import RLock
from time import sleep
from myDevices.utils.singleton import Singleton

connection = None
cursor = None
mutex = RLock()

class DbManager(Singleton):
    def CreateTable(tablename, columns, required_columns = None):
        try:
            with mutex:
                if not cursor or not connection:
                    return False
                if required_columns and len(required_columns):
                    cursor.execute('PRAGMA table_info(' +  tablename + ')')
                    actual_columns = [column[1] for column in cursor.fetchall()]
                    if actual_columns != required_columns:
                        try:
                            if actual_columns:
                                warn(tablename + ' table columns {} do not match required columns {}, dropping table'.format(actual_columns, required_columns) + " From: " + str(hex(id(connection))) + " " + str(hex(id(cursor)))  )
                            cursor.execute('DROP TABLE ' + tablename)
                            connection.commit()
                        except:
                            pass
                    del actual_columns
                cursor.execute('CREATE TABLE IF NOT EXISTS ' + tablename + ' ('+ str(columns) +')')
                connection.commit()
                # sleep(5)
        except Exception as ex:

# Path: myDevices/utils/logger.py
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def warn(message):
#     if checkFlood(message):
#         return
#     LOGGER.warn(message)
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# def setDebug():
#     LOGGER.setLevel(DEBUG)
#     return
# 
# Path: myDevices/utils/singleton.py
# class Singleton(_Singleton('SingletonMeta', (object,), {})): pass


# Path: myDevices/cloud/dbmanager.py
from sqlite3 import connect
from myDevices.utils.logger import exception, info, warn, error, debug, setDebug
from threading import RLock
from time import sleep
from myDevices.utils.singleton import Singleton


connection = None
cursor = None
mutex = RLock()

class DbManager(Singleton):
    def CreateTable(tablename, columns, required_columns = None):
        try:
            with mutex:
                if not cursor or not connection:
                    return False
                if required_columns and len(required_columns):
                    cursor.execute('PRAGMA table_info(' +  tablename + ')')
                    actual_columns = [column[1] for column in cursor.fetchall()]
                    if actual_columns != required_columns:
                        try:
                            if actual_columns:
                                warn(tablename + ' table columns {} do not match required columns {}, dropping table'.format(actual_columns, required_columns) + " From: " + str(hex(id(connection))) + " " + str(hex(id(cursor)))  )
                            cursor.execute('DROP TABLE ' + tablename)
                            connection.commit()
                        except:
                            pass
                    del actual_columns
                cursor.execute('CREATE TABLE IF NOT EXISTS ' + tablename + ' ('+ str(columns) +')')
                connection.commit()
                # sleep(5)
        except Exception as ex:

            error("Failed to CreateTable: " + tablename + " " + str(ex))