comparison tools/memory.py @ 22:bb72c29f3432

added action cb mechanism for buttons. Tested with a temporary new user registration button.
author Goffi <goffi@goffi.org>
date Tue, 01 Dec 2009 04:56:08 +0100
parents 633c5ed65701
children a544b376b6f0
comparison
equal deleted inserted replaced
21:633c5ed65701 22:bb72c29f3432
52 """ 52 """
53 53
54 def load_default_params(self): 54 def load_default_params(self):
55 self.dom = minidom.parseString(Param.default_xml) 55 self.dom = minidom.parseString(Param.default_xml)
56 56
57 def __init__(self): 57 def __init__(self, host):
58 debug("Parameters init") 58 debug("Parameters init")
59 self.host = host
59 self.load_default_params() 60 self.load_default_params()
61 host.registerGeneralCB("registerNewAccount", host.registerNewAccountCB)
60 62
61 def __get_unique_node(self, parent, tag, name): 63 def __get_unique_node(self, parent, tag, name):
62 """return node with given tag, create a new one if the node doesn't exist 64 """return node with given tag, create a new one if the node doesn't exist
63 @param parent: parent of nodes to check (e.g. documentElement) 65 @param parent: parent of nodes to check (e.g. documentElement)
64 @param tag: tag to check (e.g. "category") 66 @param tag: tag to check (e.g. "category")
77 @param parent: parent class (usefull for callbacks) 79 @param parent: parent class (usefull for callbacks)
78 @param xml: parameters in xml form""" 80 @param xml: parameters in xml form"""
79 src_dom = minidom.parseString(xml) 81 src_dom = minidom.parseString(xml)
80 82
81 def import_node(tgt_parent, src_parent): 83 def import_node(tgt_parent, src_parent):
82 print "import_node [%s, %s]" % (tgt_parent.nodeName, src_parent.nodeName)
83 for child in src_parent.childNodes: 84 for child in src_parent.childNodes:
84 if child.nodeName == '#text': 85 if child.nodeName == '#text':
85 continue 86 continue
86 node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name")) 87 node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name"))
87 if not node: #The node is new 88 if not node: #The node is new
103 @param name: name of the parameter 104 @param name: name of the parameter
104 @param category: category of the parameter 105 @param category: category of the parameter
105 @param callback: must return a string with the value (use deferred if needed) 106 @param callback: must return a string with the value (use deferred if needed)
106 @param errback: must manage the error with args failure, name, category 107 @param errback: must manage the error with args failure, name, category
107 """ 108 """
109 #TODO: send signal param update if value changed
108 node = self.__getParamNode(name, category) 110 node = self.__getParamNode(name, category)
109 if not node: 111 if not node:
110 error("Requested param [%s] in category [%s] doesn't exist !" , name, category) 112 error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
111 return 113 return
112 if node.getAttribute('default_cb') == 'yes': 114 if node.getAttribute('default_cb') == 'yes':
113 del node.attributes['default_cb'] 115 del node.attributes['default_cb']
114 d = defer.maybeDeferred(callback) 116 d = defer.maybeDeferred(callback)
115 d.addCallback(self.__default_ok, name, category) 117 d.addCallback(self.__default_ok, name, category)
116 d.addErrback(errback or self.__default_ko, name, category) 118 d.addErrback(errback or self.__default_ko, name, category)
117 119
118 def getParamV(self, name, category): 120 def getParamA(self, name, category, attr="value"):
119 """Helper method to get the value in the good type 121 """Helper method to get a specific attribute
120 getParamV stands for GetParamValue""" 122 @param name: name of the parameter
123 @param category: category of the parameter
124 @param attr: name of the attribute (default: "value")
125
126 @return: attribute"""
121 node = self.__getParamNode(name, category) 127 node = self.__getParamNode(name, category)
122 if not node: 128 if not node:
123 error("Requested param [%s] in category [%s] doesn't exist !" , name, category) 129 error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
124 return None 130 return None
125 return node.getAttribute("value") 131 return node.getAttribute(attr)
126 132
127 def getParams(self): 133 def getParams(self):
128 """Return the whole params XML""" 134 """Return the whole params XML"""
129 return self.dom.toxml() 135 return self.dom.toxml()
130 136
153 159
154 def setParam(self, name, value, category): 160 def setParam(self, name, value, category):
155 node = self.__getParamNode(name, category) 161 node = self.__getParamNode(name, category)
156 if not node: 162 if not node:
157 return #TODO: throw an error 163 return #TODO: throw an error
158 node.setAttribute("value", value) 164 type = node.getAttribute("type")
165 if type=="button":
166 print "clique",node.toxml()
167 else:
168 node.setAttribute("value", value)
169 self.host.bridge.paramUpdate(name, value, category)
159 170
160 class Memory: 171 class Memory:
161 """This class manage all persistent informations""" 172 """This class manage all persistent informations"""
162 173
163 def __init__(self): 174 def __init__(self, host):
164 info ("Memory manager init") 175 info ("Memory manager init")
165 self.contact={} 176 self.contact={}
166 self.presenceStatus={} 177 self.presenceStatus={}
167 self.params=Param() 178 self.params=Param(host)
168 self.history={} 179 self.history={}
169 self.disco={} #XXX: maybe best in a separate class 180 self.disco={} #XXX: maybe best in a separate class
170 self.features={} 181 self.features={}
171 self.load() 182 self.load()
172 183
174 """Load parameters and all memory things from file/db""" 185 """Load parameters and all memory things from file/db"""
175 186
176 #first parameters 187 #first parameters
177 if os.path.exists(const_SAVEFILE_PARAM): 188 if os.path.exists(const_SAVEFILE_PARAM):
178 try: 189 try:
190 #gof: FIXME FIXME FIXME: sauver l'xml et non plus le pickle !!!!!!!
179 with open(const_SAVEFILE_PARAM, 'r') as params_pickle: 191 with open(const_SAVEFILE_PARAM, 'r') as params_pickle:
180 self.params=pickle.load(params_pickle) 192 self.params=pickle.load(params_pickle)
181 debug("params loaded") 193 debug("params loaded")
182 except: 194 except:
183 error ("Can't load params !") 195 error ("Can't load params !")
258 for contact, contactStatus in self.presenceStatus.items(): 270 for contact, contactStatus in self.presenceStatus.items():
259 status.append([contact]+contactStatus) 271 status.append([contact]+contactStatus)
260 debug ("Memory getPresenceStatus (%s)", status) 272 debug ("Memory getPresenceStatus (%s)", status)
261 return status 273 return status
262 274
263 def getParamV(self, name, category): 275 def getParamA(self, name, category, attr="value"):
264 return self.params.getParamV(name, category) 276 return self.params.getParamA(name, category, attr)
265 277
266 def getParams(self): 278 def getParams(self):
267 return self.params.getParams() 279 return self.params.getParams()
268 280
269 def getParamsForCategory(self, category): 281 def getParamsForCategory(self, category):