ppf: Update log formatting. Add exception handling.
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 19 Aug 2011 11:42:17 +0000 (14:42 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 19 Aug 2011 11:42:22 +0000 (14:42 +0300)
Create DatabaseError class for raising Database exceptions.

ppf/db/access.py

index db7cc46..642a975 100644 (file)
@@ -17,7 +17,7 @@ ch = logging.StreamHandler()
 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)
@@ -26,6 +26,15 @@ 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,
@@ -155,10 +164,14 @@ class SQLiteDatabaseAccess(DatabaseAccess):
 
     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()
@@ -172,7 +185,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                     """, 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:
@@ -190,7 +204,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                     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:
@@ -204,7 +219,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                         """ %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:
@@ -214,7 +230,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                     """, 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:
@@ -230,7 +247,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                 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:
@@ -241,6 +259,9 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                 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):
@@ -251,7 +272,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
             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:
@@ -264,7 +286,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                         """, 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:
@@ -280,7 +303,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                 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):
@@ -309,7 +333,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                 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:
@@ -323,7 +348,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                     """ %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:
@@ -348,7 +374,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                             """, (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])
@@ -358,7 +385,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                     """, 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):
@@ -383,7 +411,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                 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:
@@ -397,7 +426,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                         """ %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])
@@ -407,7 +437,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                     """, 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):
@@ -431,7 +462,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                 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:
@@ -445,7 +477,8 @@ class SQLiteDatabaseAccess(DatabaseAccess):
                         """ %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):