core: param loading/purging on profile connection/disconnection

- fixed default value in .*getParam.*
This commit is contained in:
Goffi 2011-11-01 22:59:15 +01:00
commit fa6d27efb4
4 changed files with 42 additions and 18 deletions

View file

@ -268,6 +268,15 @@ class SAT(service.Service):
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

View file

@ -93,6 +93,7 @@ class SatXMPPClient(client.XMPPClient):
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):

View file

@ -79,19 +79,27 @@ class Param():
"""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 @@ class Param():
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 @@ class Param():
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 @@ class Param():
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 @@ class Param():
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 @@ class Param():
@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 @@ class Param():
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 @@ class Param():
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 @@ class Param():
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 @@ class Param():
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 @@ class Memory:
"""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"""

View file

@ -114,7 +114,7 @@ class SqliteStorage():
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 @@ class SqliteStorage():
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]