Index: pykota/trunk/pykota/storages/pgstorage.py
===================================================================
--- pykota/trunk/pykota/storages/pgstorage.py (revision 1115)
+++ pykota/trunk/pykota/storages/pgstorage.py (revision 1130)
@@ -21,4 +21,7 @@
 #
 # $Log$
+# Revision 1.13  2003/10/02 20:23:18  jalet
+# Storage caching mechanism added.
+#
 # Revision 1.12  2003/08/17 14:20:25  jalet
 # Bug fix by Oleg Biteryakov
@@ -64,6 +67,5 @@
 #
 
-from pykota.storage import PyKotaStorageError
-from pykota.storage import StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
+from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
 
 try :
@@ -74,10 +76,8 @@
     raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PygreSQL module installed correctly." % sys.version.split()[0]
 
-class Storage :
+class Storage(BaseStorage) :
     def __init__(self, pykotatool, host, dbname, user, passwd) :
         """Opens the PostgreSQL database connection."""
-        self.tool = pykotatool
-        self.debug = pykotatool.config.getDebug()
-        self.closed = 1
+        BaseStorage.__init__(self, pykotatool)
         try :
             (host, port) = host.split(":")
@@ -92,11 +92,6 @@
         else :    
             self.closed = 0
-            if self.debug :
-                self.tool.logger.log_message("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user), "debug")
-            
-    def __del__(self) :        
-        """Ensures that the database connection is closed."""
-        self.close()
-        
+            self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
+            
     def close(self) :    
         """Closes the database connection."""
@@ -104,24 +99,20 @@
             self.database.close()
             self.closed = 1
-            if self.debug :
-                self.tool.logger.log_message("Database closed.", "debug")
+            self.tool.logdebug("Database closed.")
         
     def beginTransaction(self) :    
         """Starts a transaction."""
         self.database.query("BEGIN;")
-        if self.debug :
-            self.tool.logger.log_message("Transaction begins...", "debug")
+        self.tool.logdebug("Transaction begins...")
         
     def commitTransaction(self) :    
         """Commits a transaction."""
         self.database.query("COMMIT;")
-        if self.debug :
-            self.tool.logger.log_message("Transaction committed.", "debug")
+        self.tool.logdebug("Transaction committed.")
         
     def rollbackTransaction(self) :     
         """Rollbacks a transaction."""
         self.database.query("ROLLBACK;")
-        if self.debug :
-            self.tool.logger.log_message("Transaction aborted.", "debug")
+        self.tool.logdebug("Transaction aborted.")
         
     def doSearch(self, query) :
@@ -131,6 +122,5 @@
             query += ';'
         try :
-            if self.debug :
-                self.tool.logger.log_message("QUERY : %s" % query, "debug")
+            self.tool.logdebug("QUERY : %s" % query)
             result = self.database.query(query)
         except pg.error, msg :    
@@ -146,6 +136,5 @@
             query += ';'
         try :
-            if self.debug :
-                self.tool.logger.log_message("QUERY : %s" % query, "debug")
+            self.tool.logdebug("QUERY : %s" % query)
             result = self.database.query(query)
         except pg.error, msg :    
@@ -164,5 +153,5 @@
         return pg._quote(field, typ)
         
-    def getUser(self, username) :    
+    def getUserFromBackend(self, username) :    
         """Extracts user information given its name."""
         user = StorageUser(self, username)
@@ -178,5 +167,5 @@
         return user
        
-    def getGroup(self, groupname) :    
+    def getGroupFromBackend(self, groupname) :    
         """Extracts group information given its name."""
         group = StorageGroup(self, groupname)
@@ -194,5 +183,5 @@
         return group
        
-    def getPrinter(self, printername) :        
+    def getPrinterFromBackend(self, printername) :        
         """Extracts printer information given its name."""
         printer = StoragePrinter(self, printername)
@@ -206,31 +195,6 @@
             printer.Exists = 1
         return printer    
-            
-    def getUserGroups(self, user) :        
-        """Returns the user's groups list."""
-        groups = []
-        result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident))
-        if result :
-            for record in result :
-                groups.append(self.getGroup(record.get("groupname")))
-        return groups        
-        
-    def getGroupMembers(self, group) :        
-        """Returns the group's members list."""
-        groupmembers = []
-        result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident))
-        if result :
-            for record in result :
-                user = StorageUser(self, record.get("username"))
-                user.ident = record.get("userid")
-                user.LimitBy = record.get("limitby")
-                user.AccountBalance = record.get("balance")
-                user.LifeTimePaid = record.get("lifetimepaid")
-                user.Email = record.get("email")
-                user.Exists = 1
-                groupmembers.append(user)
-        return groupmembers        
-        
-    def getUserPQuota(self, user, printer) :        
+        
+    def getUserPQuotaFromBackend(self, user, printer) :        
         """Extracts a user print quota."""
         userpquota = StorageUserPQuota(self, user, printer)
@@ -248,5 +212,5 @@
         return userpquota
         
-    def getGroupPQuota(self, group, printer) :        
+    def getGroupPQuotaFromBackend(self, group, printer) :        
         """Extracts a group print quota."""
         grouppquota = StorageGroupPQuota(self, group, printer)
@@ -267,5 +231,5 @@
         return grouppquota
         
-    def getPrinterLastJob(self, printer) :        
+    def getPrinterLastJobFromBackend(self, printer) :        
         """Extracts a printer's last job information."""
         lastjob = StorageLastJob(self, printer)
@@ -282,4 +246,29 @@
             lastjob.Exists = 1
         return lastjob
+            
+    def getUserGroups(self, user) :        
+        """Returns the user's groups list."""
+        groups = []
+        result = self.doSearch("SELECT groupname FROM groupsmembers JOIN groups ON groupsmembers.groupid=groups.id WHERE userid=%s" % self.doQuote(user.ident))
+        if result :
+            for record in result :
+                groups.append(self.getGroup(record.get("groupname")))
+        return groups        
+        
+    def getGroupMembers(self, group) :        
+        """Returns the group's members list."""
+        groupmembers = []
+        result = self.doSearch("SELECT * FROM groupsmembers JOIN users ON groupsmembers.userid=users.id WHERE groupid=%s" % self.doQuote(group.ident))
+        if result :
+            for record in result :
+                user = StorageUser(self, record.get("username"))
+                user.ident = record.get("userid")
+                user.LimitBy = record.get("limitby")
+                user.AccountBalance = record.get("balance")
+                user.LifeTimePaid = record.get("lifetimepaid")
+                user.Email = record.get("email")
+                user.Exists = 1
+                groupmembers.append(user)
+        return groupmembers        
         
     def getMatchingPrinters(self, printerpattern) :
Index: pykota/trunk/pykota/storages/ldapstorage.py
===================================================================
--- pykota/trunk/pykota/storages/ldapstorage.py (revision 1119)
+++ pykota/trunk/pykota/storages/ldapstorage.py (revision 1130)
@@ -21,4 +21,7 @@
 #
 # $Log$
+# Revision 1.26  2003/10/02 20:23:18  jalet
+# Storage caching mechanism added.
+#
 # Revision 1.25  2003/08/20 15:56:24  jalet
 # Better user and group deletion
@@ -116,6 +119,5 @@
 import md5
 
-from pykota.storage import PyKotaStorageError
-from pykota.storage import StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
+from pykota.storage import PyKotaStorageError,BaseStorage,StorageObject,StorageUser,StorageGroup,StoragePrinter,StorageLastJob,StorageUserPQuota,StorageGroupPQuota
 
 try :
@@ -127,11 +129,9 @@
     raise PyKotaStorageError, "This python version (%s) doesn't seem to have the python-ldap module installed correctly." % sys.version.split()[0]
     
-class Storage :
+class Storage(BaseStorage) :
     def __init__(self, pykotatool, host, dbname, user, passwd) :
         """Opens the LDAP connection."""
         # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !"
-        self.closed = 1
-        self.tool = pykotatool
-        self.debug = pykotatool.config.getDebug()
+        BaseStorage.__init__(self, pykotatool)
         self.info = pykotatool.config.getLDAPInfo()
         try :
@@ -145,11 +145,6 @@
         else :    
             self.closed = 0
-            if self.debug :
-                self.tool.logger.log_message("Database opened (host=%s, dbname=%s, user=%s)" % (host, dbname, user), "debug")
-            
-    def __del__(self) :        
-        """Ensures that the database connection is closed."""
-        self.close()
-        
+            self.tool.logdebug("Database opened (host=%s, dbname=%s, user=%s)" % (host, dbname, user))
+            
     def close(self) :    
         """Closes the database connection."""
@@ -157,6 +152,5 @@
             del self.database
             self.closed = 1
-            if self.debug :
-                self.tool.logger.log_message("Database closed.", "debug")
+            self.tool.logdebug("Database closed.")
         
     def genUUID(self) :    
@@ -169,16 +163,13 @@
     def beginTransaction(self) :    
         """Starts a transaction."""
-        if self.debug :
-            self.tool.logger.log_message("Transaction begins... WARNING : No transactions in LDAP !", "debug")
+        self.tool.logdebug("Transaction begins... WARNING : No transactions in LDAP !")
         
     def commitTransaction(self) :    
         """Commits a transaction."""
-        if self.debug :
-            self.tool.logger.log_message("Transaction committed. WARNING : No transactions in LDAP !", "debug")
+        self.tool.logdebug("Transaction committed. WARNING : No transactions in LDAP !")
         
     def rollbackTransaction(self) :     
         """Rollbacks a transaction."""
-        if self.debug :
-            self.tool.logger.log_message("Transaction aborted. WARNING : No transaction in LDAP !", "debug")
+        self.tool.logdebug("Transaction aborted. WARNING : No transaction in LDAP !")
         
     def doSearch(self, key, fields=None, base="", scope=ldap.SCOPE_SUBTREE) :
@@ -186,12 +177,10 @@
         try :
             base = base or self.basedn
-            if self.debug :
-                self.tool.logger.log_message("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields), "debug")
+            self.tool.logdebug("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields))
             result = self.database.search_s(base or self.basedn, scope, key, fields)
         except ldap.LDAPError :    
             raise PyKotaStorageError, _("Search for %s(%s) from %s(scope=%s) returned no answer.") % (key, fields, base, scope)
         else :     
-            if self.debug :
-                self.tool.logger.log_message("QUERY : Result : %s" % result, "debug")
+            self.tool.logdebug("QUERY : Result : %s" % result)
             return result
             
@@ -199,6 +188,5 @@
         """Adds an entry in the LDAP directory."""
         try :
-            if self.debug :
-                self.tool.logger.log_message("QUERY : ADD(%s, %s)" % (dn, str(fields)), "debug")
+            self.tool.logdebug("QUERY : ADD(%s, %s)" % (dn, str(fields)))
             self.database.add_s(dn, modlist.addModlist(fields))
         except ldap.LDAPError :
@@ -210,6 +198,5 @@
         """Deletes an entry from the LDAP directory."""
         try :
-            if self.debug :
-                self.tool.logger.log_message("QUERY : Delete(%s)" % dn, "debug")
+            self.tool.logdebug("QUERY : Delete(%s)" % dn)
             self.database.delete_s(dn)
         except ldap.LDAPError :
@@ -220,6 +207,5 @@
         try :
             oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE)
-            if self.debug :
-                self.tool.logger.log_message("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry[0][1], fields), "debug")
+            self.tool.logdebug("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry[0][1], fields))
             self.database.modify_s(dn, modlist.modifyModlist(oldentry[0][1], fields, ignore_oldexistent=ignoreold))
         except ldap.LDAPError :
@@ -228,5 +214,5 @@
             return dn
             
-    def getUser(self, username) :    
+    def getUserFromBackend(self, username) :    
         """Extracts user information given its name."""
         user = StorageUser(self, username)
@@ -262,5 +248,5 @@
         return user
        
-    def getGroup(self, groupname) :    
+    def getGroupFromBackend(self, groupname) :    
         """Extracts group information given its name."""
         group = StorageGroup(self, groupname)
@@ -282,5 +268,5 @@
         return group
        
-    def getPrinter(self, printername) :        
+    def getPrinterFromBackend(self, printername) :        
         """Extracts printer information given its name."""
         printer = StoragePrinter(self, printername)
@@ -294,24 +280,6 @@
             printer.Exists = 1
         return printer    
-            
-    def getUserGroups(self, user) :        
-        """Returns the user's groups list."""
-        groups = []
-        result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], user.Name), [self.info["grouprdn"]], base=self.info["groupbase"])
-        if result :
-            for (groupid, fields) in result :
-                groups.append(self.getGroup(fields.get(self.info["grouprdn"])[0]))
-        return groups        
-        
-    def getGroupMembers(self, group) :        
-        """Returns the group's members list."""
-        groupmembers = []
-        result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (group.Name, self.info["grouprdn"], group.Name), [self.info["groupmembers"]], base=self.info["groupbase"])
-        if result :
-            for username in result[0][1].get(self.info["groupmembers"], []) :
-                groupmembers.append(self.getUser(username))
-        return groupmembers        
-        
-    def getUserPQuota(self, user, printer) :        
+        
+    def getUserPQuotaFromBackend(self, user, printer) :        
         """Extracts a user print quota."""
         userpquota = StorageUserPQuota(self, user, printer)
@@ -344,5 +312,5 @@
         return userpquota
         
-    def getGroupPQuota(self, group, printer) :        
+    def getGroupPQuotaFromBackend(self, group, printer) :        
         """Extracts a group print quota."""
         grouppquota = StorageGroupPQuota(self, group, printer)
@@ -383,5 +351,5 @@
         return grouppquota
         
-    def getPrinterLastJob(self, printer) :        
+    def getPrinterLastJobFromBackend(self, printer) :        
         """Extracts a printer's last job information."""
         lastjob = StorageLastJob(self, printer)
@@ -409,4 +377,22 @@
                 lastjob.Exists = 1
         return lastjob
+        
+    def getUserGroups(self, user) :        
+        """Returns the user's groups list."""
+        groups = []
+        result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % (self.info["groupmembers"], user.Name), [self.info["grouprdn"]], base=self.info["groupbase"])
+        if result :
+            for (groupid, fields) in result :
+                groups.append(self.getGroup(fields.get(self.info["grouprdn"])[0]))
+        return groups        
+        
+    def getGroupMembers(self, group) :        
+        """Returns the group's members list."""
+        groupmembers = []
+        result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (group.Name, self.info["grouprdn"], group.Name), [self.info["groupmembers"]], base=self.info["groupbase"])
+        if result :
+            for username in result[0][1].get(self.info["groupmembers"], []) :
+                groupmembers.append(self.getUser(username))
+        return groupmembers        
         
     def getMatchingPrinters(self, printerpattern) :
