comparison src/memory/sqlite.py @ 587:952322b1d490

Remove trailing whitespaces.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:34 +0100
parents ca13633d3b6b
children beaf6bec2fcd
comparison
equal deleted inserted replaced
586:6a718ede8be1 587:952322b1d490
66 init_defers.append(d) 66 init_defers.append(d)
67 67
68 def fillProfileCache(ignore): 68 def fillProfileCache(ignore):
69 d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache) 69 d = self.dbpool.runQuery("SELECT name,id FROM profiles").addCallback(self._profilesCache)
70 d.chainDeferred(self.initialized) 70 d.chainDeferred(self.initialized)
71 71
72 defer.DeferredList(init_defers).addCallback(fillProfileCache) 72 defer.DeferredList(init_defers).addCallback(fillProfileCache)
73 73
74 #Profiles 74 #Profiles
75 def _profilesCache(self, profiles_result): 75 def _profilesCache(self, profiles_result):
76 """Fill the profiles cache 76 """Fill the profiles cache
77 @param profiles_result: result of the sql profiles query""" 77 @param profiles_result: result of the sql profiles query"""
78 for profile in profiles_result: 78 for profile in profiles_result:
79 name, id = profile 79 name, id = profile
80 self.profiles[name] = id 80 self.profiles[name] = id
81 81
82 def getProfilesList(self): 82 def getProfilesList(self):
83 """"Return list of all registered profiles""" 83 """"Return list of all registered profiles"""
84 return self.profiles.keys() 84 return self.profiles.keys()
85 85
86 def hasProfile(self, profile_name): 86 def hasProfile(self, profile_name):
87 """return True if profile_name exists 87 """return True if profile_name exists
88 @param profile_name: name of the profile to check""" 88 @param profile_name: name of the profile to check"""
89 return self.profiles.has_key(profile_name) 89 return self.profiles.has_key(profile_name)
90 90
91 def createProfile(self, name): 91 def createProfile(self, name):
92 """Create a new profile 92 """Create a new profile
93 @param name: name of the profile 93 @param name: name of the profile
94 @return: deferred triggered once profile is actually created""" 94 @return: deferred triggered once profile is actually created"""
95 def getProfileId(ignore): 95 def getProfileId(ignore):
96 return self.dbpool.runQuery("SELECT (id) FROM profiles WHERE name = ?", (name,)) 96 return self.dbpool.runQuery("SELECT (id) FROM profiles WHERE name = ?", (name,))
97 97
98 def profile_created(profile_id): 98 def profile_created(profile_id):
99 _id = profile_id[0][0] 99 _id = profile_id[0][0]
100 self.profiles[name] = _id #we synchronise the cache 100 self.profiles[name] = _id #we synchronise the cache
101 101
102 d = self.dbpool.runQuery("INSERT INTO profiles(name) VALUES (?)", (name,)) 102 d = self.dbpool.runQuery("INSERT INTO profiles(name) VALUES (?)", (name,))
103 d.addCallback(getProfileId) 103 d.addCallback(getProfileId)
104 d.addCallback(profile_created) 104 d.addCallback(profile_created)
105 return d 105 return d
106 106
107 def deleteProfile(self, name): 107 def deleteProfile(self, name):
108 """Delete profile 108 """Delete profile
109 @param name: name of the profile 109 @param name: name of the profile
110 @return: deferred triggered once profile is actually deleted""" 110 @return: deferred triggered once profile is actually deleted"""
111 def deletionError(failure): 111 def deletionError(failure):
124 @return: deferred""" 124 @return: deferred"""
125 def fillParams(result): 125 def fillParams(result):
126 for param in result: 126 for param in result:
127 category,name,value = param 127 category,name,value = param
128 params_gen[(category, name)] = value 128 params_gen[(category, name)] = value
129 debug(_("loading general parameters from database")) 129 debug(_("loading general parameters from database"))
130 return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams) 130 return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams)
131 131
132 def loadIndParams(self, params_ind, profile): 132 def loadIndParams(self, params_ind, profile):
133 """Load individual parameters 133 """Load individual parameters
134 @param params_ind: dictionary to fill 134 @param params_ind: dictionary to fill
136 @return: deferred""" 136 @return: deferred"""
137 def fillParams(result): 137 def fillParams(result):
138 for param in result: 138 for param in result:
139 category,name,value = param 139 category,name,value = param
140 params_ind[(category, name)] = value 140 params_ind[(category, name)] = value
141 debug(_("loading individual parameters from database")) 141 debug(_("loading individual parameters from database"))
142 d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile],)) 142 d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile],))
143 d.addCallback(fillParams) 143 d.addCallback(fillParams)
144 return d 144 return d
145 145
146 def getIndParam(self, category, name, profile): 146 def getIndParam(self, category, name, profile):
216 values.append(_jid.userhost()) 216 values.append(_jid.userhost())
217 if _jid.resource: 217 if _jid.resource:
218 values.append(_jid.resource) 218 values.append(_jid.resource)
219 return '(%s=? AND %s_res=?)' % (_type, _type) 219 return '(%s=? AND %s_res=?)' % (_type, _type)
220 return '%s=?' % (_type,) 220 return '%s=?' % (_type,)
221 221
222 if between: 222 if between:
223 query_parts.append("(%s OR %s) AND (%s or %s)" % (test_jid('source', from_jid), 223 query_parts.append("(%s OR %s) AND (%s or %s)" % (test_jid('source', from_jid),
224 test_jid('source', to_jid), 224 test_jid('source', to_jid),
225 test_jid('dest', to_jid), 225 test_jid('dest', to_jid),
226 test_jid('dest', from_jid))) 226 test_jid('dest', from_jid)))
231 query_parts.append("ORDER BY timestamp DESC") 231 query_parts.append("ORDER BY timestamp DESC")
232 232
233 if limit: 233 if limit:
234 query_parts.append("LIMIT ?") 234 query_parts.append("LIMIT ?")
235 values.append(limit) 235 values.append(limit)
236 236
237 d = self.dbpool.runQuery(" ".join(query_parts), values) 237 d = self.dbpool.runQuery(" ".join(query_parts), values)
238 return d.addCallback(sqliteToDict) 238 return d.addCallback(sqliteToDict)
239 239
240 #Private values 240 #Private values
241 def loadGenPrivates(self, private_gen, namespace): 241 def loadGenPrivates(self, private_gen, namespace):
245 @return: deferred""" 245 @return: deferred"""
246 def fillPrivates(result): 246 def fillPrivates(result):
247 for private in result: 247 for private in result:
248 key,value = private 248 key,value = private
249 private_gen[key] = value 249 private_gen[key] = value
250 debug(_("loading general private values [namespace: %s] from database") % (namespace,)) 250 debug(_("loading general private values [namespace: %s] from database") % (namespace,))
251 d = self.dbpool.runQuery("SELECT key,value FROM private_gen WHERE namespace=?", (namespace,)).addCallback(fillPrivates) 251 d = self.dbpool.runQuery("SELECT key,value FROM private_gen WHERE namespace=?", (namespace,)).addCallback(fillPrivates)
252 return d.addErrback(lambda x: debug(_("No data present in database for namespace %s") % namespace)) 252 return d.addErrback(lambda x: debug(_("No data present in database for namespace %s") % namespace))
253 253
254 def loadIndPrivates(self, private_ind, namespace, profile): 254 def loadIndPrivates(self, private_ind, namespace, profile):
255 """Load individual private values 255 """Load individual private values
271 @param category: category of the privateeter 271 @param category: category of the privateeter
272 @param key: key of the private value 272 @param key: key of the private value
273 @param value: value to set 273 @param value: value to set
274 @return: deferred""" 274 @return: deferred"""
275 d = self.dbpool.runQuery("REPLACE INTO private_gen(namespace,key,value) VALUES (?,?,?)", (namespace,key,value)) 275 d = self.dbpool.runQuery("REPLACE INTO private_gen(namespace,key,value) VALUES (?,?,?)", (namespace,key,value))
276 d.addErrback(lambda ignore: error(_("Can't set general private value (%(key)s) [namespace:%(namespace)s] in database" % 276 d.addErrback(lambda ignore: error(_("Can't set general private value (%(key)s) [namespace:%(namespace)s] in database" %
277 {"namespace":namespace, "key":key}))) 277 {"namespace":namespace, "key":key})))
278 return d 278 return d
279 279
280 def setIndPrivate(self, namespace, key, value, profile): 280 def setIndPrivate(self, namespace, key, value, profile):
281 """Save the individual private value in database 281 """Save the individual private value in database
293 """Delete the general private value from database 293 """Delete the general private value from database
294 @param category: category of the privateeter 294 @param category: category of the privateeter
295 @param key: key of the private value 295 @param key: key of the private value
296 @return: deferred""" 296 @return: deferred"""
297 d = self.dbpool.runQuery("DELETE FROM private_gen WHERE namespace=? AND key=?", (namespace,key)) 297 d = self.dbpool.runQuery("DELETE FROM private_gen WHERE namespace=? AND key=?", (namespace,key))
298 d.addErrback(lambda ignore: error(_("Can't delete general private value (%(key)s) [namespace:%(namespace)s] in database" % 298 d.addErrback(lambda ignore: error(_("Can't delete general private value (%(key)s) [namespace:%(namespace)s] in database" %
299 {"namespace":namespace, "key":key}))) 299 {"namespace":namespace, "key":key})))
300 return d 300 return d
301 301
302 def delIndPrivate(self, namespace, key, profile): 302 def delIndPrivate(self, namespace, key, profile):
303 """Delete the individual private value from database 303 """Delete the individual private value from database
318 @return: deferred""" 318 @return: deferred"""
319 def fillPrivates(result): 319 def fillPrivates(result):
320 for private in result: 320 for private in result:
321 key,value = private 321 key,value = private
322 private_gen[key] = pickle.loads(str(value)) 322 private_gen[key] = pickle.loads(str(value))
323 debug(_("loading general private binary values [namespace: %s] from database") % (namespace,)) 323 debug(_("loading general private binary values [namespace: %s] from database") % (namespace,))
324 d = self.dbpool.runQuery("SELECT key,value FROM private_gen_bin WHERE namespace=?", (namespace,)).addCallback(fillPrivates) 324 d = self.dbpool.runQuery("SELECT key,value FROM private_gen_bin WHERE namespace=?", (namespace,)).addCallback(fillPrivates)
325 return d.addErrback(lambda x: debug(_("No binary data present in database for namespace %s") % namespace)) 325 return d.addErrback(lambda x: debug(_("No binary data present in database for namespace %s") % namespace))
326 326
327 def loadIndPrivatesBinary(self, private_ind, namespace, profile): 327 def loadIndPrivatesBinary(self, private_ind, namespace, profile):
328 """Load individual private binary values 328 """Load individual private binary values
344 @param category: category of the privateeter 344 @param category: category of the privateeter
345 @param key: key of the private value 345 @param key: key of the private value
346 @param value: value to set 346 @param value: value to set
347 @return: deferred""" 347 @return: deferred"""
348 d = self.dbpool.runQuery("REPLACE INTO private_gen_bin(namespace,key,value) VALUES (?,?,?)", (namespace,key,pickle.dumps(value,0))) 348 d = self.dbpool.runQuery("REPLACE INTO private_gen_bin(namespace,key,value) VALUES (?,?,?)", (namespace,key,pickle.dumps(value,0)))
349 d.addErrback(lambda ignore: error(_("Can't set general private binary value (%(key)s) [namespace:%(namespace)s] in database" % 349 d.addErrback(lambda ignore: error(_("Can't set general private binary value (%(key)s) [namespace:%(namespace)s] in database" %
350 {"namespace":namespace, "key":key}))) 350 {"namespace":namespace, "key":key})))
351 return d 351 return d
352 352
353 def setIndPrivateBinary(self, namespace, key, value, profile): 353 def setIndPrivateBinary(self, namespace, key, value, profile):
354 """Save the individual private binary value in database 354 """Save the individual private binary value in database
366 """Delete the general private binary value from database 366 """Delete the general private binary value from database
367 @param category: category of the privateeter 367 @param category: category of the privateeter
368 @param key: key of the private value 368 @param key: key of the private value
369 @return: deferred""" 369 @return: deferred"""
370 d = self.dbpool.runQuery("DELETE FROM private_gen_bin WHERE namespace=? AND key=?", (namespace,key)) 370 d = self.dbpool.runQuery("DELETE FROM private_gen_bin WHERE namespace=? AND key=?", (namespace,key))
371 d.addErrback(lambda ignore: error(_("Can't delete general private binary value (%(key)s) [namespace:%(namespace)s] in database" % 371 d.addErrback(lambda ignore: error(_("Can't delete general private binary value (%(key)s) [namespace:%(namespace)s] in database" %
372 {"namespace":namespace, "key":key}))) 372 {"namespace":namespace, "key":key})))
373 return d 373 return d
374 374
375 def delIndPrivateBinary(self, namespace, key, profile): 375 def delIndPrivateBinary(self, namespace, key, profile):
376 """Delete the individual private binary value from database 376 """Delete the individual private binary value from database