diff 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
line wrap: on
line diff
--- a/src/memory/memory.py	Fri Nov 08 17:48:27 2013 +0100
+++ b/src/memory/memory.py	Fri Oct 18 19:23:03 2013 +0200
@@ -32,7 +32,6 @@
 from sat.memory.persistent import PersistentDict
 from sat.core import exceptions
 
-SAVEFILE_PARAM_XML = "/param"  # xml parameters template
 SAVEFILE_DATABASE = "/sat.db"
 NO_SECURITY_LIMIT = -1
 
@@ -137,7 +136,6 @@
         self.default_profile = None
         self.params = {}
         self.params_gen = {}
-        host.set_const('savefile_param_xml', SAVEFILE_PARAM_XML)
         host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
 
     def createProfile(self, profile):
@@ -603,7 +601,7 @@
         self.initialized = defer.Deferred()
         self.host = host
         self.entitiesCache = {}  # XXX: keep presence/last resource/other data in cache
-                              #     /!\ an entity is not necessarily in roster
+                                 #     /!\ an entity is not necessarily in roster
         self.subscriptions = {}
         self.server_features = {}  # used to store discovery's informations
         self.server_identities = {}
@@ -614,7 +612,8 @@
         self.storage = SqliteStorage(database_file)
         PersistentDict.storage = self.storage
         self.params = Params(host, self.storage)
-        self.loadFiles()
+        info(_("Loading default params template"))
+        self.params.load_default_params()
         d = self.storage.initialized.addCallback(lambda ignore: self.load())
         self.memory_data = PersistentDict("memory")
         d.addCallback(lambda ignore: self.memory_data.load())
@@ -644,22 +643,19 @@
 
         return os.path.expanduser(_value) if name.endswith('_path') or name.endswith('_dir') else _value
 
-    def loadFiles(self):
-        """Load parameters and all memory things from file/db"""
-        param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') +
-                                            self.host.get_const('savefile_param_xml'))
-
-        #parameters template
-        if os.path.exists(param_file_xml):
+    def load_xml(self, filename):
+        """Load parameters template from xml file"""
+        if filename is None:
+            return False
+        filename = os.path.expanduser(filename)
+        if os.path.exists(filename):
             try:
-                self.params.load_xml(param_file_xml)
-                debug(_("params template loaded"))
+                self.params.load_xml(filename)
+                debug(_("Parameters loaded from file: %s") % filename)
+                return True
             except Exception as e:
-                error(_("Can't load params template: %s") % (e, ))
-                self.params.load_default_params()
-        else:
-            info(_("No params template, using default template"))
-            self.params.load_default_params()
+                error(_("Can't load parameters from file: %s") % e)
+        return False
 
     def load(self):
         """Load parameters and all memory things from db"""
@@ -687,15 +683,19 @@
         except KeyError:
             error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile)
 
-    def save(self):
-        """Save parameters and all memory things to file/db"""
+    def save_xml(self, filename=None):
+        """Save parameters template to xml file"""
+        if filename is None:
+            return False
         #TODO: need to encrypt files (at least passwords !) and set permissions
-        param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') +
-                                            self.host.get_const('savefile_param_xml'))
-
-        # TODO: check if this method is still needed
-        #self.params.save_xml(param_file_xml)
-        debug(_("params saved"))
+        filename = os.path.expanduser(filename)
+        try:
+            self.params.save_xml(filename)
+            debug(_("Parameters saved to file: %s") % filename)
+            return True
+        except Exception as e:
+            error(_("Can't save parameters to file: %s") % e)
+        return False
 
     def getProfilesList(self):
         return self.storage.getProfilesList()