comparison src/memory/memory.py @ 677:9a50aa7feefb

core (memory): cleaned the part for load/save to xml (not used)
author souliane <souliane@mailoo.org>
date Fri, 18 Oct 2013 19:23:03 +0200
parents ffb716804580
children 59c9a7ff903d
comparison
equal deleted inserted replaced
676:a2bef7315ac5 677:9a50aa7feefb
30 from sat.core.default_config import default_config 30 from sat.core.default_config import default_config
31 from sat.memory.sqlite import SqliteStorage 31 from sat.memory.sqlite import SqliteStorage
32 from sat.memory.persistent import PersistentDict 32 from sat.memory.persistent import PersistentDict
33 from sat.core import exceptions 33 from sat.core import exceptions
34 34
35 SAVEFILE_PARAM_XML = "/param" # xml parameters template
36 SAVEFILE_DATABASE = "/sat.db" 35 SAVEFILE_DATABASE = "/sat.db"
37 NO_SECURITY_LIMIT = -1 36 NO_SECURITY_LIMIT = -1
38 37
39 class Params(object): 38 class Params(object):
40 """This class manage parameters with xml""" 39 """This class manage parameters with xml"""
135 self.host = host 134 self.host = host
136 self.storage = storage 135 self.storage = storage
137 self.default_profile = None 136 self.default_profile = None
138 self.params = {} 137 self.params = {}
139 self.params_gen = {} 138 self.params_gen = {}
140 host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML)
141 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB) 139 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
142 140
143 def createProfile(self, profile): 141 def createProfile(self, profile):
144 """Create a new profile 142 """Create a new profile
145 @param profile: profile of the profile""" 143 @param profile: profile of the profile"""
601 def __init__(self, host): 599 def __init__(self, host):
602 info(_("Memory manager init")) 600 info(_("Memory manager init"))
603 self.initialized = defer.Deferred() 601 self.initialized = defer.Deferred()
604 self.host = host 602 self.host = host
605 self.entitiesCache = {} # XXX: keep presence/last resource/other data in cache 603 self.entitiesCache = {} # XXX: keep presence/last resource/other data in cache
606 # /!\ an entity is not necessarily in roster 604 # /!\ an entity is not necessarily in roster
607 self.subscriptions = {} 605 self.subscriptions = {}
608 self.server_features = {} # used to store discovery's informations 606 self.server_features = {} # used to store discovery's informations
609 self.server_identities = {} 607 self.server_identities = {}
610 self.config = self.parseMainConf() 608 self.config = self.parseMainConf()
611 host.set_const('savefile_database', SAVEFILE_DATABASE) 609 host.set_const('savefile_database', SAVEFILE_DATABASE)
612 database_file = os.path.expanduser(self.getConfig('', 'local_dir') + 610 database_file = os.path.expanduser(self.getConfig('', 'local_dir') +
613 self.host.get_const('savefile_database')) 611 self.host.get_const('savefile_database'))
614 self.storage = SqliteStorage(database_file) 612 self.storage = SqliteStorage(database_file)
615 PersistentDict.storage = self.storage 613 PersistentDict.storage = self.storage
616 self.params = Params(host, self.storage) 614 self.params = Params(host, self.storage)
617 self.loadFiles() 615 info(_("Loading default params template"))
616 self.params.load_default_params()
618 d = self.storage.initialized.addCallback(lambda ignore: self.load()) 617 d = self.storage.initialized.addCallback(lambda ignore: self.load())
619 self.memory_data = PersistentDict("memory") 618 self.memory_data = PersistentDict("memory")
620 d.addCallback(lambda ignore: self.memory_data.load()) 619 d.addCallback(lambda ignore: self.memory_data.load())
621 d.chainDeferred(self.initialized) 620 d.chainDeferred(self.initialized)
622 621
642 except (NoOptionError, NoSectionError): 641 except (NoOptionError, NoSectionError):
643 _value = '' 642 _value = ''
644 643
645 return os.path.expanduser(_value) if name.endswith('_path') or name.endswith('_dir') else _value 644 return os.path.expanduser(_value) if name.endswith('_path') or name.endswith('_dir') else _value
646 645
647 def loadFiles(self): 646 def load_xml(self, filename):
648 """Load parameters and all memory things from file/db""" 647 """Load parameters template from xml file"""
649 param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') + 648 if filename is None:
650 self.host.get_const('savefile_param_xml')) 649 return False
651 650 filename = os.path.expanduser(filename)
652 #parameters template 651 if os.path.exists(filename):
653 if os.path.exists(param_file_xml):
654 try: 652 try:
655 self.params.load_xml(param_file_xml) 653 self.params.load_xml(filename)
656 debug(_("params template loaded")) 654 debug(_("Parameters loaded from file: %s") % filename)
655 return True
657 except Exception as e: 656 except Exception as e:
658 error(_("Can't load params template: %s") % (e, )) 657 error(_("Can't load parameters from file: %s") % e)
659 self.params.load_default_params() 658 return False
660 else:
661 info(_("No params template, using default template"))
662 self.params.load_default_params()
663 659
664 def load(self): 660 def load(self):
665 """Load parameters and all memory things from db""" 661 """Load parameters and all memory things from db"""
666 #parameters data 662 #parameters data
667 return self.params.loadGenParams() 663 return self.params.loadGenParams()
685 try: 681 try:
686 del self.entitiesCache[profile] 682 del self.entitiesCache[profile]
687 except KeyError: 683 except KeyError:
688 error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile) 684 error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile)
689 685
690 def save(self): 686 def save_xml(self, filename=None):
691 """Save parameters and all memory things to file/db""" 687 """Save parameters template to xml file"""
688 if filename is None:
689 return False
692 #TODO: need to encrypt files (at least passwords !) and set permissions 690 #TODO: need to encrypt files (at least passwords !) and set permissions
693 param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') + 691 filename = os.path.expanduser(filename)
694 self.host.get_const('savefile_param_xml')) 692 try:
695 693 self.params.save_xml(filename)
696 # TODO: check if this method is still needed 694 debug(_("Parameters saved to file: %s") % filename)
697 #self.params.save_xml(param_file_xml) 695 return True
698 debug(_("params saved")) 696 except Exception as e:
697 error(_("Can't save parameters to file: %s") % e)
698 return False
699 699
700 def getProfilesList(self): 700 def getProfilesList(self):
701 return self.storage.getProfilesList() 701 return self.storage.getProfilesList()
702 702
703 def getProfileName(self, profile_key): 703 def getProfileName(self, profile_key):