comparison 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
comparison
equal deleted inserted replaced
556:14dcbcdb6222 557:4f856dd4c0d0
69 } 69 }
70 70
71 def load_default_params(self): 71 def load_default_params(self):
72 self.dom = minidom.parseString(Params.default_xml.encode('utf-8')) 72 self.dom = minidom.parseString(Params.default_xml.encode('utf-8'))
73 73
74 def load_xml(self, file): 74 def _mergeParams(self, source_node, dest_node):
75 """Look for every node in source_node and recursively copy them to dest if they don't exists"""
76 def getNodesMap(children):
77 ret = {}
78 for child in children:
79 if child.nodeType == child.ELEMENT_NODE:
80 ret[(child.tagName, child.getAttribute('name'))] = child
81 return ret
82 source_map = getNodesMap(source_node.childNodes)
83 dest_map = getNodesMap(dest_node.childNodes)
84 source_set = set(source_map.keys())
85 dest_set = set(dest_map.keys())
86 to_add = source_set.difference(dest_set)
87
88 for node_key in to_add:
89 dest_node.appendChild(source_map[node_key].cloneNode(True))
90
91 to_recurse = source_set - to_add
92 for node_key in to_recurse:
93 self._mergeParams(source_map[node_key], dest_map[node_key])
94
95 def load_xml(self, xml_file):
75 """Load parameters template from file""" 96 """Load parameters template from file"""
76 self.dom = minidom.parse(file) 97 self.dom = minidom.parse(xml_file)
98 default_dom = minidom.parseString(Params.default_xml.encode('utf-8'))
99 self._mergeParams(default_dom.documentElement, self.dom.documentElement)
77 100
78 def loadGenParams(self): 101 def loadGenParams(self):
79 """Load general parameters data from storage 102 """Load general parameters data from storage
80 @return: deferred triggered once params are loaded""" 103 @return: deferred triggered once params are loaded"""
81 return self.storage.loadGenParams(self.params_gen) 104 return self.storage.loadGenParams(self.params_gen)
148 if not self.storage.hasProfile(profile): 171 if not self.storage.hasProfile(profile):
149 error(_('Trying to delete an unknown profile')) 172 error(_('Trying to delete an unknown profile'))
150 return True 173 return True
151 if self.host.isConnected(profile): 174 if self.host.isConnected(profile):
152 error(_("Trying to delete a connected profile")) 175 error(_("Trying to delete a connected profile"))
153 raise exceptions.ConnectedProfileError 176 raise exceptions.NotConnectedProfileError
154 self.storage.deleteProfile(profile) 177 self.storage.deleteProfile(profile)
155 return False 178 return False
156 179
157 def getProfileName(self, profile_key): 180 def getProfileName(self, profile_key):
158 """return profile according to profile_key 181 """return profile according to profile_key
275 error(_('Requesting a param for an non-existant profile')) 298 error(_('Requesting a param for an non-existant profile'))
276 raise exceptions.ProfileUnknownError 299 raise exceptions.ProfileUnknownError
277 300
278 if profile not in self.params: 301 if profile not in self.params:
279 error(_('Requesting synchronous param for not connected profile')) 302 error(_('Requesting synchronous param for not connected profile'))
280 raise exceptions.ConnectedProfileError 303 raise exceptions.NotConnectedProfileError(profile)
281 304
282 if attr == "value": 305 if attr == "value":
283 value = self.__getParam(profile, category, name) 306 value = self.__getParam(profile, category, name)
284 return self.__getAttr(node[1], attr, value) 307 return self.__getAttr(node[1], attr, value)
285 308
563 #parameters template 586 #parameters template
564 if os.path.exists(param_file_xml): 587 if os.path.exists(param_file_xml):
565 try: 588 try:
566 self.params.load_xml(param_file_xml) 589 self.params.load_xml(param_file_xml)
567 debug(_("params template loaded")) 590 debug(_("params template loaded"))
568 except: 591 except Exception as e:
569 error (_("Can't load params template !")) 592 error (_("Can't load params template: %s") % (e,))
570 self.params.load_default_params() 593 self.params.load_default_params()
571 else: 594 else:
572 info (_("No params template, using default template")) 595 info (_("No params template, using default template"))
573 self.params.load_default_params() 596 self.params.load_default_params()
574 597