ch.setLevel(logging.ERROR)
# Create formatter.
-formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+formatter = logging.Formatter('%(filename)s:%(lineno)s - %(levelname)s: %(message)s')
# Add formatter to console handler.
ch.setFormatter(formatter)
logger.addHandler(ch)
+class DatabaseError(Exception):
+ """
+ Raised on SQL query errors.
+ """
+
+ def __init__(self, msg):
+ self.msg = msg
+
+
class DatabaseAccess:
"""
Low-level class for database access: insert, update, delete,
def connect(self, database):
if not os.path.isfile(database):
- return None
- self.conn = sqlite3.connect(database)
- self.cursor = self.conn.cursor()
- return self.conn
+ raise DatabaseError("No such file %s" %database)
+ try:
+
+ self.conn = sqlite3.connect(database)
+ self.cursor = self.conn.cursor()
+ except sqlite3.Error, e:
+ logger.error("Could not connect to database %s" %database)
+ raise DatabaseError(e.args[0])
def disconnect(self):
self.cursor.close()
""", row)
self.conn.commit()
except sqlite3.Error, e:
- print ("[swarms]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_swarms(self, show=False, swarm_id=None):
try:
print row
except sqlite3.Error, e:
- print("[swarms]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def delete_swarms(self, swarm_id=None):
try:
""" %swarm_id)
self.conn.commit()
except sqlite3.Error, e:
- print("[swarms]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def insert_btclients_row(self, row):
try:
""", row)
self.conn.commit()
except sqlite3.Error, e:
- print ("[btclients]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_btclients(self, show=False, id=None):
try:
for row in self.cursor:
print row
except sqlite3.Error, e:
- print ("[btclients]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_btclients_by_name(self, client_name, show=False):
try:
for row in self.cursor:
print row
except sqlite3.Error, e:
+ logger.error("An exception was raised: %s" %e.args[0])
+ log.error("[swarms] An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
print ("[btclients]An error ocurred: ", e.args[0])
def select_btclients_id_by_name(self, client_name):
for row in self.cursor:
return row[0]
except sqlite3.Error, e:
- print ("[btclients]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def insert_client_sessions_row(self, row):
try:
""", row)
self.conn.commit()
except sqlite3.Error, e:
- print ("[client_sessions]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_client_sessions_by_id(self, show=False, cs_id=None):
try:
for row in self.cursor:
print row
except sqlite3.Error, e:
- print ("[client_sessions]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_client_sessions_by_swarm(self, show=False, swarm_id=None,
client_id=None):
for row in self.cursor:
print row
except sqlite3.Error, e:
- print ("[client_sessions]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def delete_client_sessions_by_id(self, cs_id=None):
try:
""" %cs_id)
self.conn.commit()
except sqlite3.Error, e:
- print ("[client_sessions]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def delete_client_sessions_by_swarm(self, swarm_id=None, client_id=None):
try:
""", (swarm_id, client_id))
self.conn.commit()
except sqlite3.Error, e:
- print ("[client_sessions]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def insert_status_messages_row(self, row):
logger.debug("[status_messages] INSERT row: %s" %row[0])
""", row)
self.conn.commit()
except sqlite3.Error, e:
- logger.error("[status_messages]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_status_messages(self, show=False, cs_id=None,
restrictArray=None):
for row in self.cursor:
print row
except sqlite3.Error, e:
- print("[status_messages]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def delete_status_messages(self, cs_id=None):
try:
""" %cs_id)
self.conn.commit()
except sqlite3.Error, e:
- print("[status_messages]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def insert_verbose_messages_row(self, row):
logger.debug("[verbose_messages] INSERT row %s" %row[0])
""", row)
self.conn.commit()
except sqlite3.Error, e:
- print ("[verbose_messages]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def select_verbose_messages(self, show=False, cs_id=None,
restrictArray=None):
for row in self.cursor:
print row
except sqlite3.Error, e:
- print("[status_messages]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
def delete_verbose_messages(self, cs_id=None):
try:
""" %cs_id)
self.conn.commit()
except sqlite3.Error, e:
- print("[status_messages]An error ocurred: ", e.args[0])
+ logger.error("An exception was raised: %s" %e.args[0])
+ raise DatabaseError(e.args[0])
class MySQLDatabaseAccess(DatabaseAccess):