diff src/memory/memory.py @ 557:4f856dd4c0d0

core: paramaters are now merged: if a parameter doens't exist in loaded xml but exists in default parameters, it is added
author Goffi <goffi@goffi.org>
date Sun, 16 Dec 2012 18:01:06 +0100
parents 14dcbcdb6222
children 9faccd827657
line wrap: on
line diff
--- a/src/memory/memory.py	Sun Dec 16 17:59:50 2012 +0100
+++ b/src/memory/memory.py	Sun Dec 16 18:01:06 2012 +0100
@@ -71,9 +71,32 @@
     def load_default_params(self):
         self.dom = minidom.parseString(Params.default_xml.encode('utf-8'))
 
-    def load_xml(self, file):
+    def _mergeParams(self, source_node, dest_node):
+        """Look for every node in source_node and recursively copy them to dest if they don't exists"""
+        def getNodesMap(children):
+            ret = {}
+            for child in children:
+                if child.nodeType == child.ELEMENT_NODE:
+                    ret[(child.tagName, child.getAttribute('name'))] = child
+            return ret
+        source_map = getNodesMap(source_node.childNodes)
+        dest_map = getNodesMap(dest_node.childNodes)
+        source_set = set(source_map.keys())
+        dest_set = set(dest_map.keys())
+        to_add = source_set.difference(dest_set)
+        
+        for node_key in to_add:
+            dest_node.appendChild(source_map[node_key].cloneNode(True))
+
+        to_recurse = source_set - to_add
+        for node_key in to_recurse:
+            self._mergeParams(source_map[node_key], dest_map[node_key])
+
+    def load_xml(self, xml_file):
         """Load parameters template from file"""
-        self.dom = minidom.parse(file)
+        self.dom = minidom.parse(xml_file)
+        default_dom = minidom.parseString(Params.default_xml.encode('utf-8'))
+        self._mergeParams(default_dom.documentElement, self.dom.documentElement)
     
     def loadGenParams(self):
         """Load general parameters data from storage
@@ -150,7 +173,7 @@
             return True
         if self.host.isConnected(profile):
             error(_("Trying to delete a connected profile"))
-            raise exceptions.ConnectedProfileError
+            raise exceptions.NotConnectedProfileError
         self.storage.deleteProfile(profile)
         return False
 
@@ -277,7 +300,7 @@
         
         if profile not in self.params:
             error(_('Requesting synchronous param for not connected profile'))
-            raise exceptions.ConnectedProfileError
+            raise exceptions.NotConnectedProfileError(profile)
 
         if attr == "value": 
             value = self.__getParam(profile, category, name)
@@ -565,8 +588,8 @@
             try:
                 self.params.load_xml(param_file_xml)
                 debug(_("params template loaded"))
-            except:
-                error (_("Can't load params template !"))
+            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"))