comparison src/memory/memory.py @ 443:7099ea9c1b12

core: getPrivate/setPrivate removed from memory, private values now use database storage and persistent dicts \o/
author Goffi <goffi@goffi.org>
date Sat, 03 Dec 2011 23:33:00 +0100
parents 31e8c48b5f5d
children 094050fe461e
comparison
equal deleted inserted replaced
442:ab9ddf6c7eaf 443:7099ea9c1b12
33 from sat.core.default_config import default_config 33 from sat.core.default_config import default_config
34 from sat.memory.sqlite import SqliteStorage 34 from sat.memory.sqlite import SqliteStorage
35 from sat.memory.persistent import PersistentDict 35 from sat.memory.persistent import PersistentDict
36 36
37 SAVEFILE_PARAM_XML="/param" #xml parameters template 37 SAVEFILE_PARAM_XML="/param" #xml parameters template
38 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
43 42
165 @param profile_key: profile name or key which can be 164 @param profile_key: profile name or key which can be
166 @ALL@ for all profiles 165 @ALL@ for all profiles
167 @DEFAULT@ for default profile 166 @DEFAULT@ for default profile
168 @return: requested profile name or None if it doesn't exist""" 167 @return: requested profile name or None if it doesn't exist"""
169 if profile_key=='@DEFAULT@': 168 if profile_key=='@DEFAULT@':
170 if not self.params: 169 default = self.host.memory.memory_data.get('Profile_default')
171 return "" 170 if not default:
172 default = self.host.memory.getPrivate('Profile_default')
173 if not default or not default in self.params:
174 info(_('No default profile, returning first one')) #TODO: manage real default profile 171 info(_('No default profile, returning first one')) #TODO: manage real default profile
175 default = self.storage.getProfilesList()[0] 172 default = self.host.memory.memory_data['Profile_default'] = self.storage.getProfilesList()[0]
176 self.host.memory.setPrivate('Profile_default', default)
177 return default #FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists 173 return default #FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists
178 if not self.storage.hasProfile(profile_key): 174 if not self.storage.hasProfile(profile_key):
179 info (_('Trying to access an unknown profile')) 175 info (_('Trying to access an unknown profile'))
180 return "" 176 return ""
181 return profile_key 177 return profile_key
498 self.host = host 494 self.host = host
499 self.contacts={} 495 self.contacts={}
500 self.presenceStatus={} 496 self.presenceStatus={}
501 self.lastResource={} #tmp, will be refactored with bdd integration 497 self.lastResource={} #tmp, will be refactored with bdd integration
502 self.subscriptions={} 498 self.subscriptions={}
503 self.private={} #used to store private value
504 self.server_features={} #used to store discovery's informations 499 self.server_features={} #used to store discovery's informations
505 self.server_identities={} 500 self.server_identities={}
506 self.config = self.parseMainConf() 501 self.config = self.parseMainConf()
507 host.set_const('savefile_private', SAVEFILE_PRIVATE)
508 host.set_const('savefile_database', SAVEFILE_DATABASE) 502 host.set_const('savefile_database', SAVEFILE_DATABASE)
509 database_file = os.path.expanduser(self.getConfig('','local_dir')+ 503 database_file = os.path.expanduser(self.getConfig('','local_dir')+
510 self.host.get_const('savefile_database')) 504 self.host.get_const('savefile_database'))
511 self.storage = SqliteStorage(database_file) 505 self.storage = SqliteStorage(database_file)
512 PersistentDict.storage = self.storage 506 PersistentDict.storage = self.storage
513 self.params=Params(host, self.storage) 507 self.params=Params(host, self.storage)
514 self.loadFiles() 508 self.loadFiles()
515 d = self.storage.initialized.addCallback(self.load) 509 d = self.storage.initialized.addCallback(lambda ignore:self.load())
510 self.memory_data = PersistentDict("memory")
511 d.addCallback(lambda ignore: self.memory_data.load())
516 d.chainDeferred(self.initialized) 512 d.chainDeferred(self.initialized)
517 513
518 def parseMainConf(self): 514 def parseMainConf(self):
519 """look for main .ini configuration file, and parse it""" 515 """look for main .ini configuration file, and parse it"""
520 _config = SafeConfigParser(defaults=default_config) 516 _config = SafeConfigParser(defaults=default_config)
542 538
543 def loadFiles(self): 539 def loadFiles(self):
544 """Load parameters and all memory things from file/db""" 540 """Load parameters and all memory things from file/db"""
545 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+ 541 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
546 self.host.get_const('savefile_param_xml')) 542 self.host.get_const('savefile_param_xml'))
547 private_file = os.path.expanduser(self.getConfig('','local_dir')+
548 self.host.get_const('savefile_private'))
549 543
550 #parameters template 544 #parameters template
551 if os.path.exists(param_file_xml): 545 if os.path.exists(param_file_xml):
552 try: 546 try:
553 self.params.load_xml(param_file_xml) 547 self.params.load_xml(param_file_xml)
557 self.params.load_default_params() 551 self.params.load_default_params()
558 else: 552 else:
559 info (_("No params template, using default template")) 553 info (_("No params template, using default template"))
560 self.params.load_default_params() 554 self.params.load_default_params()
561 555
562 #private 556
563 if os.path.exists(private_file): 557 def load(self):
564 try:
565 with open(private_file, 'r') as private_pickle:
566 self.private=pickle.load(private_pickle)
567 debug(_("private values loaded"))
568 except:
569 error (_("Can't load private values !"))
570
571 def load(self, ignore):
572 """Load parameters and all memory things from db""" 558 """Load parameters and all memory things from db"""
573 #parameters data 559 #parameters data
574 return self.params.loadGenParams() 560 return self.params.loadGenParams()
575 561
576 def loadIndividualParams(self, profile): 562 def loadIndividualParams(self, profile):
586 def save(self): 572 def save(self):
587 """Save parameters and all memory things to file/db""" 573 """Save parameters and all memory things to file/db"""
588 #TODO: need to encrypt files (at least passwords !) and set permissions 574 #TODO: need to encrypt files (at least passwords !) and set permissions
589 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+ 575 param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
590 self.host.get_const('savefile_param_xml')) 576 self.host.get_const('savefile_param_xml'))
591 private_file = os.path.expanduser(self.getConfig('','local_dir')+
592 self.host.get_const('savefile_private'))
593 577
594 self.params.save_xml(param_file_xml) 578 self.params.save_xml(param_file_xml)
595 debug(_("params saved")) 579 debug(_("params saved"))
596 with open(private_file, 'w') as private_pickle:
597 pickle.dump(self.private, private_pickle)
598 debug(_("private values saved"))
599 580
600 def getProfilesList(self): 581 def getProfilesList(self):
601 return self.storage.getProfilesList() 582 return self.storage.getProfilesList()
602 583
603 584
628 assert(profile!="@NONE@") 609 assert(profile!="@NONE@")
629 return self.storage.addToHistory(from_jid, to_jid, message, timestamp, profile) 610 return self.storage.addToHistory(from_jid, to_jid, message, timestamp, profile)
630 611
631 def getHistory(self, from_jid, to_jid, limit=0, between=True): 612 def getHistory(self, from_jid, to_jid, limit=0, between=True):
632 return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between) 613 return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between)
633
634 def setPrivate(self, key, value):
635 """Save a misc private value (mainly useful for plugins)"""
636 self.private[key] = value
637
638 def getPrivate(self, key):
639 """return a private value
640 @param key: name of wanted value
641 @return: value or None if value don't exist"""
642 if self.private.has_key(key):
643 return self.private[key]
644 return None
645 614
646 def addServerFeature(self, feature, profile): 615 def addServerFeature(self, feature, profile):
647 """Add a feature discovered from server 616 """Add a feature discovered from server
648 @param feature: string of the feature 617 @param feature: string of the feature
649 @param profile: which profile is using this server ?""" 618 @param profile: which profile is using this server ?"""