comparison src/tools/memory.py @ 425:e4e9187e3b5b

backend, bridge: asynchronous history quick_frontend: use of asynchronous history
author Goffi <goffi@goffi.org>
date Tue, 08 Nov 2011 01:08:11 +0100
parents 6c20c76abdcc
children a4a9efadabfc
comparison
equal deleted inserted replaced
424:72c13313b6d6 425:e4e9187e3b5b
32 from sat.tools.xml_tools import paramsXml2xmlUI 32 from sat.tools.xml_tools import paramsXml2xmlUI
33 from sat.core.default_config import default_config 33 from sat.core.default_config import default_config
34 from sat.tools.sqlite import SqliteStorage 34 from sat.tools.sqlite import SqliteStorage
35 35
36 SAVEFILE_PARAM_XML="/param" #xml parameters template 36 SAVEFILE_PARAM_XML="/param" #xml parameters template
37 SAVEFILE_HISTORY="/history"
38 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins) 37 SAVEFILE_PRIVATE="/private" #file used to store misc values (mainly for plugins)
39 SAVEFILE_DATABASE="/sat.db" 38 SAVEFILE_DATABASE="/sat.db"
40 39
41 class ProfileNotInCacheError(Exception): 40 class ProfileNotInCacheError(Exception):
42 pass 41 pass
499 self.host = host 498 self.host = host
500 self.contacts={} 499 self.contacts={}
501 self.presenceStatus={} 500 self.presenceStatus={}
502 self.lastResource={} #tmp, will be refactored with bdd integration 501 self.lastResource={} #tmp, will be refactored with bdd integration
503 self.subscriptions={} 502 self.subscriptions={}
504 self.history={} #used to store chat history (key: short jid)
505 self.private={} #used to store private value 503 self.private={} #used to store private value
506 self.server_features={} #used to store discovery's informations 504 self.server_features={} #used to store discovery's informations
507 self.server_identities={} 505 self.server_identities={}
508 self.config = self.parseMainConf() 506 self.config = self.parseMainConf()
509 host.set_const('savefile_history', SAVEFILE_HISTORY)
510 host.set_const('savefile_private', SAVEFILE_PRIVATE) 507 host.set_const('savefile_private', SAVEFILE_PRIVATE)
511 host.set_const('savefile_database', SAVEFILE_DATABASE) 508 host.set_const('savefile_database', SAVEFILE_DATABASE)
512 database_file = os.path.expanduser(self.getConfig('','local_dir')+ 509 database_file = os.path.expanduser(self.getConfig('','local_dir')+
513 self.host.get_const('savefile_database')) 510 self.host.get_const('savefile_database'))
514 self.storage = SqliteStorage(database_file) 511 self.storage = SqliteStorage(database_file)
545 542
546 def loadFiles(self): 543 def loadFiles(self):
547 """Load parameters and all memory things from file/db""" 544 """Load parameters and all memory things from file/db"""
548 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+ 545 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
549 self.host.get_const('savefile_param_xml')) 546 self.host.get_const('savefile_param_xml'))
550 history_file = os.path.expanduser(self.getConfig('','local_dir')+
551 self.host.get_const('savefile_history'))
552 private_file = os.path.expanduser(self.getConfig('','local_dir')+ 547 private_file = os.path.expanduser(self.getConfig('','local_dir')+
553 self.host.get_const('savefile_private')) 548 self.host.get_const('savefile_private'))
554 549
555 #parameters template 550 #parameters template
556 if os.path.exists(param_file_xml): 551 if os.path.exists(param_file_xml):
562 self.params.load_default_params() 557 self.params.load_default_params()
563 else: 558 else:
564 info (_("No params template, using default template")) 559 info (_("No params template, using default template"))
565 self.params.load_default_params() 560 self.params.load_default_params()
566 561
567
568 #history
569 if os.path.exists(history_file):
570 try:
571 with open(history_file, 'r') as history_pickle:
572 self.history=pickle.load(history_pickle)
573 debug(_("history loaded"))
574 except:
575 error (_("Can't load history !"))
576
577 #private 562 #private
578 if os.path.exists(private_file): 563 if os.path.exists(private_file):
579 try: 564 try:
580 with open(private_file, 'r') as private_pickle: 565 with open(private_file, 'r') as private_pickle:
581 self.private=pickle.load(private_pickle) 566 self.private=pickle.load(private_pickle)
602 def save(self): 587 def save(self):
603 """Save parameters and all memory things to file/db""" 588 """Save parameters and all memory things to file/db"""
604 #TODO: need to encrypt files (at least passwords !) and set permissions 589 #TODO: need to encrypt files (at least passwords !) and set permissions
605 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+ 590 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
606 self.host.get_const('savefile_param_xml')) 591 self.host.get_const('savefile_param_xml'))
607 history_file = os.path.expanduser(self.getConfig('','local_dir')+
608 self.host.get_const('savefile_history'))
609 private_file = os.path.expanduser(self.getConfig('','local_dir')+ 592 private_file = os.path.expanduser(self.getConfig('','local_dir')+
610 self.host.get_const('savefile_private')) 593 self.host.get_const('savefile_private'))
611 594
612 self.params.save_xml(param_file_xml) 595 self.params.save_xml(param_file_xml)
613 debug(_("params saved")) 596 debug(_("params saved"))
614 with open(history_file, 'w') as history_pickle:
615 pickle.dump(self.history, history_pickle)
616 debug(_("history saved"))
617 with open(private_file, 'w') as private_pickle: 597 with open(private_file, 'w') as private_pickle:
618 pickle.dump(self.private, private_pickle) 598 pickle.dump(self.private, private_pickle)
619 debug(_("private values saved")) 599 debug(_("private values saved"))
620 600
621 def getProfilesList(self): 601 def getProfilesList(self):
643 def deleteProfile(self, name): 623 def deleteProfile(self, name):
644 """Delete an existing profile 624 """Delete an existing profile
645 @param name: Name of the profile""" 625 @param name: Name of the profile"""
646 return self.params.deleteProfile(name) 626 return self.params.deleteProfile(name)
647 627
648 def addToHistory(self, me_jid, from_jid, to_jid, type, message): 628 def addToHistory(self, from_jid, to_jid, message, timestamp=None, profile="@NONE@"):
649 me_short=me_jid.userhost() 629 assert(profile!="@NONE@")
650 from_short=from_jid.userhost() 630 return self.storage.addToHistory(from_jid, to_jid, message, timestamp, profile)
651 to_short=to_jid.userhost() 631
652 632 def getHistory(self, from_jid, to_jid, limit=0, between=True):
653 if from_jid==me_jid: 633 return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between)
654 key=to_short
655 else:
656 key=from_short
657
658 if not self.history.has_key(me_short):
659 self.history[me_short]={}
660 if not self.history[me_short].has_key(key):
661 self.history[me_short][key]={}
662
663 self.history[me_short][key][int(time.time())] = (from_jid.full(), message)
664
665 def getHistory(self, from_jid, to_jid, size):
666 ret={}
667 if not self.history.has_key(from_jid):
668 error(_("source JID not found !"))
669 #TODO: throw an error here
670 return {}
671 if not self.history[from_jid].has_key(to_jid):
672 error(_("dest JID not found !"))
673 #TODO: throw an error here
674 return {}
675 stamps=self.history[from_jid][to_jid].keys()
676 stamps.sort()
677 for stamp in stamps[-size:]:
678 ret[stamp]=self.history[from_jid][to_jid][stamp]
679
680 return ret
681 634
682 def setPrivate(self, key, value): 635 def setPrivate(self, key, value):
683 """Save a misc private value (mainly useful for plugins)""" 636 """Save a misc private value (mainly useful for plugins)"""
684 self.private[key] = value 637 self.private[key] = value
685 638