memory methods improvement
- new method to save private data (mainly useful for plugins) - contacts/presence management refactored - new independant methods to manage subscription
This commit is contained in:
parent
547c3d59f9
commit
313d3ef93e
1 changed files with 74 additions and 16 deletions
|
|
@ -28,9 +28,11 @@ from xml.dom import minidom
|
|||
from logging import debug, info, error
|
||||
import pdb
|
||||
from twisted.internet import defer
|
||||
from twisted.words.protocols.jabber import jid
|
||||
|
||||
SAVEFILE_PARAM="/param"
|
||||
SAVEFILE_HISTORY="/history"
|
||||
SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins)
|
||||
|
||||
class Param():
|
||||
"""This class manage parameter with xml"""
|
||||
|
|
@ -68,6 +70,7 @@ class Param():
|
|||
self.host = host
|
||||
host.set_const('savefile_param', SAVEFILE_PARAM)
|
||||
host.set_const('savefile_history', SAVEFILE_HISTORY)
|
||||
host.set_const('savefile_private', SAVEFILE_PRIVATE)
|
||||
host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
|
||||
|
||||
def __get_unique_node(self, parent, tag, name):
|
||||
|
|
@ -183,10 +186,12 @@ class Memory:
|
|||
def __init__(self, host):
|
||||
info ("Memory manager init")
|
||||
self.host = host
|
||||
self.contact={}
|
||||
self.contacts={}
|
||||
self.presenceStatus={}
|
||||
self.subscriptions={}
|
||||
self.params=Param(host)
|
||||
self.history={}
|
||||
self.history={} #used to store chat history (key: short jid)
|
||||
self.private={} #used to store private value
|
||||
self.disco={} #XXX: maybe best in a separate class
|
||||
self.features={}
|
||||
self.load()
|
||||
|
|
@ -197,6 +202,8 @@ class Memory:
|
|||
self.host.get_const('savefile_param'))
|
||||
history_file = os.path.expanduser(self.host.get_const('local_dir')+
|
||||
self.host.get_const('savefile_history'))
|
||||
private_file = os.path.expanduser(self.host.get_const('local_dir')+
|
||||
self.host.get_const('savefile_private'))
|
||||
|
||||
#parameters
|
||||
if os.path.exists(param_file):
|
||||
|
|
@ -219,6 +226,14 @@ class Memory:
|
|||
except:
|
||||
error ("Can't load history !")
|
||||
|
||||
#private
|
||||
if os.path.exists(private_file):
|
||||
try:
|
||||
with open(private_file, 'r') as private_pickle:
|
||||
self.private=pickle.load(private_pickle)
|
||||
debug("private values loaded")
|
||||
except:
|
||||
error ("Can't load private values !")
|
||||
|
||||
def save(self):
|
||||
"""Save parameters and all memory things to file/db"""
|
||||
|
|
@ -227,12 +242,17 @@ class Memory:
|
|||
self.host.get_const('savefile_param'))
|
||||
history_file = os.path.expanduser(self.host.get_const('local_dir')+
|
||||
self.host.get_const('savefile_history'))
|
||||
private_file = os.path.expanduser(self.host.get_const('local_dir')+
|
||||
self.host.get_const('savefile_private'))
|
||||
|
||||
self.params.save(param_file)
|
||||
debug("params saved")
|
||||
with open(history_file, 'w') as history_pickle:
|
||||
pickle.dump(self.history, history_pickle)
|
||||
debug("history saved")
|
||||
with open(private_file, 'w') as private_pickle:
|
||||
pickle.dump(self.private, private_pickle)
|
||||
debug("private values saved")
|
||||
|
||||
def addToHistory(self, me_jid, from_jid, to_jid, type, message):
|
||||
me_short=me_jid.userhost()
|
||||
|
|
@ -268,28 +288,66 @@ class Memory:
|
|||
|
||||
return ret
|
||||
|
||||
def addContact(self, JID, attributes, groups):
|
||||
debug("Memory addContact: %s",JID)
|
||||
def setPrivate(self, key, value):
|
||||
"""Save a misc private value (mainly useful for plugins)"""
|
||||
self.private[key] = value
|
||||
|
||||
def getPrivate(self, key):
|
||||
"""return a private value
|
||||
@param key: name of wanted value
|
||||
@return: value or None if value don't exist"""
|
||||
if self.private.has_key(key):
|
||||
return self.private[key]
|
||||
return None
|
||||
|
||||
|
||||
def addContact(self, contact_jid, attributes, groups):
|
||||
debug("Memory addContact: %s",contact_jid.userhost())
|
||||
assert(isinstance(attributes,dict))
|
||||
assert(isinstance(groups,set))
|
||||
self.contact[JID]=[attributes, groups]
|
||||
|
||||
def addPresenceStatus(self, jid, type, show, status, priority):
|
||||
self.presenceStatus[jid]=[type, show, status, priority]
|
||||
self.contacts[contact_jid.userhost()]=[attributes, groups]
|
||||
|
||||
def delContact(self, contact_jid):
|
||||
debug("Memory delContact: %s",contact_jid.userhost())
|
||||
if self.contacts.has_key(contact_jid.userhost()):
|
||||
del self.contacts[contact_jid.userhost()]
|
||||
|
||||
def getContact(self, contact_jid):
|
||||
if self.contacts.has_key(contact_jid.userhost()):
|
||||
self.contacts[contact_jid.userhost()]
|
||||
else:
|
||||
return None
|
||||
|
||||
def getContacts(self):
|
||||
debug ("Memory getContact OK (%s)", self.contact)
|
||||
debug ("Memory getContact OK (%s)", self.contacts)
|
||||
ret=[]
|
||||
for contact in self.contact:
|
||||
ret.append([contact] + [self.contact[contact][0]] + [self.contact[contact][1]]) #very ugly I know !
|
||||
for contact in self.contacts:
|
||||
attr, groups = self.contacts[contact]
|
||||
ret.append([contact, attr, groups ])
|
||||
return ret
|
||||
|
||||
def addPresenceStatus(self, contact_jid, show, priority, statuses):
|
||||
if not self.presenceStatus.has_key(contact_jid.userhost()):
|
||||
self.presenceStatus[contact_jid.userhost()] = {}
|
||||
resource = jid.parse(contact_jid.full())[2] or ''
|
||||
self.presenceStatus[contact_jid.userhost()][resource] = (show, priority, statuses)
|
||||
|
||||
def addWaitingSub(self, type, contact_jid):
|
||||
"""Called when a subcription request is received"""
|
||||
self.subscriptions[contact_jid] = type
|
||||
|
||||
def delWaitingSub(self, contact_jid):
|
||||
"""Called when a subcription request is finished"""
|
||||
if self.subscriptions.has_key(contact_jid):
|
||||
del self.subscriptions[contact_jid]
|
||||
|
||||
def getWaitingSub(self):
|
||||
"""Called to get a list of currently waiting subscription requests"""
|
||||
return self.subscriptions
|
||||
|
||||
def getPresenceStatus(self):
|
||||
status=[]
|
||||
for contact, contactStatus in self.presenceStatus.items():
|
||||
status.append([contact]+contactStatus)
|
||||
debug ("Memory getPresenceStatus (%s)", status)
|
||||
return status
|
||||
debug ("Memory getPresenceStatus (%s)", self.presenceStatus)
|
||||
return self.presenceStatus
|
||||
|
||||
def getParamA(self, name, category, attr="value"):
|
||||
return self.params.getParamA(name, category, attr)
|
||||
|
|
|
|||
Loading…
Reference in a new issue