changeset 416:32dc8b18c2ae

core: param loading/purging on profile connection/disconnection - fixed default value in .*getParam.*
author Goffi <goffi@goffi.org>
date Tue, 01 Nov 2011 22:59:15 +0100
parents 3348331e0f09
children f1bf8b6143b7
files src/core/sat_main.py src/core/xmpp.py src/tools/memory.py src/tools/sqlite.py
diffstat 4 files changed, 42 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Tue Nov 01 22:55:58 2011 +0100
+++ b/src/core/sat_main.py	Tue Nov 01 22:59:15 2011 +0100
@@ -268,6 +268,15 @@
         info(_("Disconnecting..."))
         self.profiles[profile].stopService()
 
+    def purgeClient(self, profile):
+        """Remove reference to a profile client and purge cache
+        the garbage collector can then free the memory"""
+        try:
+            del self.profiles[profile]
+        except KeyError:
+            error(_("Trying to remove reference to a client not referenced"))
+        self.memory.purgeProfile(profile)
+
     def startService(self):
         info("Salut à toi ô mon frère !")
         #TODO: manage autoconnect
--- a/src/core/xmpp.py	Tue Nov 01 22:55:58 2011 +0100
+++ b/src/core/xmpp.py	Tue Nov 01 22:59:15 2011 +0100
@@ -93,6 +93,7 @@
         except AttributeError:
             debug (_("No keep_alife"))
         self.host_app.bridge.disconnected(self.profile) #we send the signal to the clients
+        self.host_app.purgeClient(self.profile) #and we remove references to this client
 
 
 class SatMessageProtocol(xmppim.MessageProtocol):
--- a/src/tools/memory.py	Tue Nov 01 22:55:58 2011 +0100
+++ b/src/tools/memory.py	Tue Nov 01 22:59:15 2011 +0100
@@ -79,19 +79,27 @@
         """Load parameters template from file"""
         self.dom = minidom.parse(file)
     
-    def loadGenData(self):
+    def loadGenParams(self):
         """Load general parameters data from storage
         @return: deferred triggered once params are loaded"""
         return self.storage.loadGenParams(self.params_gen)
         
-    def loadIndData(self, profile):
+    def loadIndParams(self, profile):
         """Load individual parameters
         set self.params cache
         @param profile: profile to load (*must exist*)
         @return: deferred triggered once params are loaded"""
         self.params[profile] = {}
         return self.storage.loadIndParams(self.params, profile)
-    
+   
+    def purgeProfile(self, profile):
+        """Remove cache data of a profile
+        @param profile: %(doc_profile)s"""
+        try:
+            del self.params[profile]
+        except KeyError:
+            error(_("Trying to purge cache of a profile not in memory: [%s]") % profile)
+
     def save_xml(self, file):
         """Save parameters template to xml file"""
         with open(file, 'wb') as xml_file:
@@ -233,7 +241,7 @@
 
         if node[0] == 'general':
             value = self.__getParam(None, category, name, 'general')
-            return value or node[1].getAttribute(attr)
+            return value if value!=None else node[1].getAttribute(attr)
         
         assert(node[0] == 'individual')
 
@@ -247,7 +255,8 @@
             return ""
 
         if attr == "value": 
-            return self.__getParam(profile, category, name) or node[1].getAttribute(attr)
+            value = self.__getParam(profile, category, name)
+            return value if value!=None else node[1].getAttribute(attr)
         else:
             return node[1].getAttribute(attr)
 
@@ -266,7 +275,7 @@
 
         if node[0] == 'general':
             value = self.__getParam(None, category, name, 'general')
-            callback(value or node[1].getAttribute(attr))
+            callback(value if value!=None else node[1].getAttribute(attr))
             return
         
         assert(node[0] == 'individual')
@@ -282,11 +291,12 @@
             return
         default = node[1].getAttribute(attr)
         try:
-            callback(self.__getParam(profile, category, name) or default)
+            value = self.__getParam(profile, category, name)
+            callback(value if value!=None else default)
         except ProfileNotInCacheError:
             #We have to ask data to the storage manager
             d = self.storage.getIndParam(category, name, profile)
-            d.addCallback(callback)
+            d.addCallback(lambda value: callback(value if value!=None else default))
             d.addErrback(lambda x:errback())
 
     def __getParam(self, profile, category, name, type='individual'):
@@ -312,6 +322,7 @@
         @param profile: profile name (not key !)
         @return: minidom.Document of the profile xml (cf warning above)
         """
+        #TODO: asynchronous equivalent
         prof_xml = minidom.parseString('<params/>')
         cache = {}
 
@@ -341,7 +352,7 @@
                                 dest_cat.appendChild(dest_params[name])
 
                             profile_value = self.__getParam(profile, category, name, type_node.nodeName)
-                            if profile_value:  #there is a value for this profile, we must change the default
+                            if profile_value!=None:  #there is a value for this profile, we must change the default
                                 dest_params[name].setAttribute('value', profile_value)
                         if new_node:
                             prof_xml.documentElement.appendChild(dest_cat)
@@ -350,6 +361,7 @@
 
     def getParamsUI(self, profile_key):
         """Return a SàT XMLUI for parameters, with given profile"""
+        #TODO: asynchronous equivalent
         profile = self.getProfileName(profile_key)
         if not profile:
             error(_("Asking params for inexistant profile"))
@@ -360,6 +372,7 @@
     def getParams(self, profile_key):
         """Construct xml for asked profile
         Take params xml as skeleton"""
+        #TODO: asynchronous equivalent
         profile = self.getProfileName(profile_key)
         if not profile:
             error(_("Asking params for inexistant profile"))
@@ -373,6 +386,7 @@
     def getParamsForCategory(self, category, profile_key):
         """Return node's xml for selected category"""
         #TODO: manage category of general type (without existant profile)
+        #TODO: asynchronous equivalent
         profile = self.getProfileName(profile_key)
         if not profile:
             error(_("Asking params for inexistant profile"))
@@ -551,17 +565,17 @@
         """Load parameters and all memory things from db
         @param init_defers: list of deferred to wait before parameters are loaded"""
         #parameters data
-        init_defers.append(self.params.loadGenData())
+        init_defers.append(self.params.loadGenParams())
 
-    def loadIndividualParams(self, profile_key):
+    def loadIndividualParams(self, profile):
         """Load individual parameters for a profile
-        @param profile_key: %(doc_profile_key)s"""
-        profile = self.getProfileName(profile_key)
-        if not profile:
-            error (_('Trying to load parameters for a non-existant profile'))
-            raise Exception("Profile doesn't exist")
+        @param profile: %(doc_profile)s"""
         return self.params.loadIndParams(profile)
 
+    def purgeProfile(self, profile):
+        """Delete cache of data of profile
+        @param profile: %(doc_profile)s"""
+        self.params.purgeProfile(profile)
 
     def save(self):
         """Save parameters and all memory things to file/db"""
--- a/src/tools/sqlite.py	Tue Nov 01 22:55:58 2011 +0100
+++ b/src/tools/sqlite.py	Tue Nov 01 22:59:15 2011 +0100
@@ -114,7 +114,7 @@
                 category,name,value = param
                 params_ind[profile][(category, name)] = value
         debug(_("loading individual parameters from database")) 
-        d = self.dbpool.runQuery("SELECT category,name,value FROM param_gen WHERE profile_id=?", self.profiles[profile])
+        d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile],))
         d.addCallback(fillParams)
         return d
 
@@ -155,4 +155,4 @@
     def __getFirstResult(self, result):
         """Return the first result of a database query
         Useful when we are looking for one specific value"""
-        return "" if not result else result[0][0]
+        return None if not result else result[0][0]