comparison src/memory/sqlite.py @ 1409:3265a2639182

massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
author souliane <souliane@mailoo.org>
date Thu, 16 Apr 2015 14:57:57 +0200
parents 069ad98b360d
children 4fb3280c4568
comparison
equal deleted inserted replaced
1408:8a7145138330 1409:3265a2639182
112 112
113 def commitStatements(statements): 113 def commitStatements(statements):
114 114
115 if statements is None: 115 if statements is None:
116 return defer.succeed(None) 116 return defer.succeed(None)
117 log.debug("===== COMMITING STATEMENTS =====\n%s\n============\n\n" % '\n'.join(statements)) 117 log.debug(u"===== COMMITING STATEMENTS =====\n%s\n============\n\n" % '\n'.join(statements))
118 d = self.dbpool.runInteraction(self._updateDb, tuple(statements)) 118 d = self.dbpool.runInteraction(self._updateDb, tuple(statements))
119 return d 119 return d
120 120
121 init_defer.addCallback(lambda ignore: getNewBaseSql() if new_base else getUpdateSql()) 121 init_defer.addCallback(lambda ignore: getNewBaseSql() if new_base else getUpdateSql())
122 init_defer.addCallback(commitStatements) 122 init_defer.addCallback(commitStatements)
168 def deleteProfile(self, name): 168 def deleteProfile(self, name):
169 """Delete profile 169 """Delete profile
170 @param name: name of the profile 170 @param name: name of the profile
171 @return: deferred triggered once profile is actually deleted""" 171 @return: deferred triggered once profile is actually deleted"""
172 def deletionError(failure): 172 def deletionError(failure):
173 log.error(_("Can't delete profile [%s]") % name) 173 log.error(_(u"Can't delete profile [%s]") % name)
174 return failure 174 return failure
175 175
176 def delete(txn): 176 def delete(txn):
177 del self.profiles[name] 177 del self.profiles[name]
178 txn.execute("PRAGMA foreign_keys = ON") 178 txn.execute("PRAGMA foreign_keys = ON")
192 192
193 def fillParams(result): 193 def fillParams(result):
194 for param in result: 194 for param in result:
195 category, name, value = param 195 category, name, value = param
196 params_gen[(category, name)] = value 196 params_gen[(category, name)] = value
197 log.debug(_("loading general parameters from database")) 197 log.debug(_(u"loading general parameters from database"))
198 return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams) 198 return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams)
199 199
200 def loadIndParams(self, params_ind, profile): 200 def loadIndParams(self, params_ind, profile):
201 """Load individual parameters 201 """Load individual parameters
202 @param params_ind: dictionary to fill 202 @param params_ind: dictionary to fill
205 205
206 def fillParams(result): 206 def fillParams(result):
207 for param in result: 207 for param in result:
208 category, name, value = param 208 category, name, value = param
209 params_ind[(category, name)] = value 209 params_ind[(category, name)] = value
210 log.debug(_("loading individual parameters from database")) 210 log.debug(_(u"loading individual parameters from database"))
211 d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile], )) 211 d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile], ))
212 d.addCallback(fillParams) 212 d.addCallback(fillParams)
213 return d 213 return d
214 214
215 def getIndParam(self, category, name, profile): 215 def getIndParam(self, category, name, profile):
227 @param category: category of the parameter 227 @param category: category of the parameter
228 @param name: name of the parameter 228 @param name: name of the parameter
229 @param value: value to set 229 @param value: value to set
230 @return: deferred""" 230 @return: deferred"""
231 d = self.dbpool.runQuery("REPLACE INTO param_gen(category,name,value) VALUES (?,?,?)", (category, name, value)) 231 d = self.dbpool.runQuery("REPLACE INTO param_gen(category,name,value) VALUES (?,?,?)", (category, name, value))
232 d.addErrback(lambda ignore: log.error(_("Can't set general parameter (%(category)s/%(name)s) in database" % {"category": category, "name": name}))) 232 d.addErrback(lambda ignore: log.error(_(u"Can't set general parameter (%(category)s/%(name)s) in database" % {"category": category, "name": name})))
233 return d 233 return d
234 234
235 def setIndParam(self, category, name, value, profile): 235 def setIndParam(self, category, name, value, profile):
236 """Save the individual parameters in database 236 """Save the individual parameters in database
237 @param category: category of the parameter 237 @param category: category of the parameter
238 @param name: name of the parameter 238 @param name: name of the parameter
239 @param value: value to set 239 @param value: value to set
240 @param profile: a profile which *must* exist 240 @param profile: a profile which *must* exist
241 @return: deferred""" 241 @return: deferred"""
242 d = self.dbpool.runQuery("REPLACE INTO param_ind(category,name,profile_id,value) VALUES (?,?,?,?)", (category, name, self.profiles[profile], value)) 242 d = self.dbpool.runQuery("REPLACE INTO param_ind(category,name,profile_id,value) VALUES (?,?,?,?)", (category, name, self.profiles[profile], value))
243 d.addErrback(lambda ignore: log.error(_("Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category": category, "name": name, "profile": profile}))) 243 d.addErrback(lambda ignore: log.error(_(u"Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category": category, "name": name, "profile": profile})))
244 return d 244 return d
245 245
246 #History 246 #History
247 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile=None): 247 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile=None):
248 """Store a new message in history 248 """Store a new message in history
258 extra = {} 258 extra = {}
259 extra_ = pickle.dumps({k: v.encode('utf-8') for k, v in extra.items()}, 0).decode('utf-8') 259 extra_ = pickle.dumps({k: v.encode('utf-8') for k, v in extra.items()}, 0).decode('utf-8')
260 d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, type, extra, profile_id) VALUES (?,?,?,?,?,?,?,?,?)", 260 d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, type, extra, profile_id) VALUES (?,?,?,?,?,?,?,?,?)",
261 (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time(), 261 (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time(),
262 message, _type, extra_, self.profiles[profile])) 262 message, _type, extra_, self.profiles[profile]))
263 d.addErrback(lambda ignore: log.error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" % 263 d.addErrback(lambda ignore: log.error(_(u"Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" %
264 {"from_jid": from_jid.full(), "to_jid": to_jid.full(), "message": message}))) 264 {"from_jid": from_jid.full(), "to_jid": to_jid.full(), "message": message})))
265 return d 265 return d
266 266
267 def getHistory(self, from_jid, to_jid, limit=None, between=True, search=None, profile=None): 267 def getHistory(self, from_jid, to_jid, limit=None, between=True, search=None, profile=None):
268 """Retrieve messages in history 268 """Retrieve messages in history
335 335
336 def fillPrivates(result): 336 def fillPrivates(result):
337 for private in result: 337 for private in result:
338 key, value = private 338 key, value = private
339 private_gen[key] = value 339 private_gen[key] = value
340 log.debug(_("loading general private values [namespace: %s] from database") % (namespace, )) 340 log.debug(_(u"loading general private values [namespace: %s] from database") % (namespace,))
341 d = self.dbpool.runQuery("SELECT key,value FROM private_gen WHERE namespace=?", (namespace, )).addCallback(fillPrivates) 341 d = self.dbpool.runQuery("SELECT key,value FROM private_gen WHERE namespace=?", (namespace, )).addCallback(fillPrivates)
342 return d.addErrback(lambda x: log.debug(_("No data present in database for namespace %s") % namespace)) 342 return d.addErrback(lambda x: log.debug(_(u"No data present in database for namespace %s") % namespace))
343 343
344 def loadIndPrivates(self, private_ind, namespace, profile): 344 def loadIndPrivates(self, private_ind, namespace, profile):
345 """Load individual private values 345 """Load individual private values
346 @param privates_ind: dictionary to fill 346 @param privates_ind: dictionary to fill
347 @param namespace: namespace of the values 347 @param namespace: namespace of the values
350 350
351 def fillPrivates(result): 351 def fillPrivates(result):
352 for private in result: 352 for private in result:
353 key, value = private 353 key, value = private
354 private_ind[key] = value 354 private_ind[key] = value
355 log.debug(_("loading individual private values [namespace: %s] from database") % (namespace, )) 355 log.debug(_(u"loading individual private values [namespace: %s] from database") % (namespace,))
356 d = self.dbpool.runQuery("SELECT key,value FROM private_ind WHERE namespace=? AND profile_id=?", (namespace, self.profiles[profile])) 356 d = self.dbpool.runQuery("SELECT key,value FROM private_ind WHERE namespace=? AND profile_id=?", (namespace, self.profiles[profile]))
357 d.addCallback(fillPrivates) 357 d.addCallback(fillPrivates)
358 return d.addErrback(lambda x: log.debug(_("No data present in database for namespace %s") % namespace)) 358 return d.addErrback(lambda x: log.debug(_(u"No data present in database for namespace %s") % namespace))
359 359
360 def setGenPrivate(self, namespace, key, value): 360 def setGenPrivate(self, namespace, key, value):
361 """Save the general private value in database 361 """Save the general private value in database
362 @param category: category of the privateeter 362 @param category: category of the privateeter
363 @param key: key of the private value 363 @param key: key of the private value
364 @param value: value to set 364 @param value: value to set
365 @return: deferred""" 365 @return: deferred"""
366 d = self.dbpool.runQuery("REPLACE INTO private_gen(namespace,key,value) VALUES (?,?,?)", (namespace, key, value)) 366 d = self.dbpool.runQuery("REPLACE INTO private_gen(namespace,key,value) VALUES (?,?,?)", (namespace, key, value))
367 d.addErrback(lambda ignore: log.error(_("Can't set general private value (%(key)s) [namespace:%(namespace)s] in database" % 367 d.addErrback(lambda ignore: log.error(_(u"Can't set general private value (%(key)s) [namespace:%(namespace)s] in database" %
368 {"namespace": namespace, "key": key}))) 368 {"namespace": namespace, "key": key})))
369 return d 369 return d
370 370
371 def setIndPrivate(self, namespace, key, value, profile): 371 def setIndPrivate(self, namespace, key, value, profile):
372 """Save the individual private value in database 372 """Save the individual private value in database
374 @param key: key of the private value 374 @param key: key of the private value
375 @param value: value to set 375 @param value: value to set
376 @param profile: a profile which *must* exist 376 @param profile: a profile which *must* exist
377 @return: deferred""" 377 @return: deferred"""
378 d = self.dbpool.runQuery("REPLACE INTO private_ind(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], value)) 378 d = self.dbpool.runQuery("REPLACE INTO private_ind(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], value))
379 d.addErrback(lambda ignore: log.error(_("Can't set individual private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % 379 d.addErrback(lambda ignore: log.error(_(u"Can't set individual private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" %
380 {"namespace": namespace, "key": key, "profile": profile}))) 380 {"namespace": namespace, "key": key, "profile": profile})))
381 return d 381 return d
382 382
383 def delGenPrivate(self, namespace, key): 383 def delGenPrivate(self, namespace, key):
384 """Delete the general private value from database 384 """Delete the general private value from database
385 @param category: category of the privateeter 385 @param category: category of the privateeter
386 @param key: key of the private value 386 @param key: key of the private value
387 @return: deferred""" 387 @return: deferred"""
388 d = self.dbpool.runQuery("DELETE FROM private_gen WHERE namespace=? AND key=?", (namespace, key)) 388 d = self.dbpool.runQuery("DELETE FROM private_gen WHERE namespace=? AND key=?", (namespace, key))
389 d.addErrback(lambda ignore: log.error(_("Can't delete general private value (%(key)s) [namespace:%(namespace)s] in database" % 389 d.addErrback(lambda ignore: log.error(_(u"Can't delete general private value (%(key)s) [namespace:%(namespace)s] in database" %
390 {"namespace": namespace, "key": key}))) 390 {"namespace": namespace, "key": key})))
391 return d 391 return d
392 392
393 def delIndPrivate(self, namespace, key, profile): 393 def delIndPrivate(self, namespace, key, profile):
394 """Delete the individual private value from database 394 """Delete the individual private value from database
395 @param namespace: namespace of the value 395 @param namespace: namespace of the value
396 @param key: key of the private value 396 @param key: key of the private value
397 @param profile: a profile which *must* exist 397 @param profile: a profile which *must* exist
398 @return: deferred""" 398 @return: deferred"""
399 d = self.dbpool.runQuery("DELETE FROM private_ind WHERE namespace=? AND key=? AND profile=?)", (namespace, key, self.profiles[profile])) 399 d = self.dbpool.runQuery("DELETE FROM private_ind WHERE namespace=? AND key=? AND profile=?)", (namespace, key, self.profiles[profile]))
400 d.addErrback(lambda ignore: log.error(_("Can't delete individual private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % 400 d.addErrback(lambda ignore: log.error(_(u"Can't delete individual private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" %
401 {"namespace": namespace, "key": key, "profile": profile}))) 401 {"namespace": namespace, "key": key, "profile": profile})))
402 return d 402 return d
403 403
404 def loadGenPrivatesBinary(self, private_gen, namespace): 404 def loadGenPrivatesBinary(self, private_gen, namespace):
405 """Load general private binary values 405 """Load general private binary values
409 409
410 def fillPrivates(result): 410 def fillPrivates(result):
411 for private in result: 411 for private in result:
412 key, value = private 412 key, value = private
413 private_gen[key] = pickle.loads(str(value)) 413 private_gen[key] = pickle.loads(str(value))
414 log.debug(_("loading general private binary values [namespace: %s] from database") % (namespace, )) 414 log.debug(_(u"loading general private binary values [namespace: %s] from database") % (namespace,))
415 d = self.dbpool.runQuery("SELECT key,value FROM private_gen_bin WHERE namespace=?", (namespace, )).addCallback(fillPrivates) 415 d = self.dbpool.runQuery("SELECT key,value FROM private_gen_bin WHERE namespace=?", (namespace, )).addCallback(fillPrivates)
416 return d.addErrback(lambda x: log.debug(_("No binary data present in database for namespace %s") % namespace)) 416 return d.addErrback(lambda x: log.debug(_(u"No binary data present in database for namespace %s") % namespace))
417 417
418 def loadIndPrivatesBinary(self, private_ind, namespace, profile): 418 def loadIndPrivatesBinary(self, private_ind, namespace, profile):
419 """Load individual private binary values 419 """Load individual private binary values
420 @param privates_ind: dictionary to fill 420 @param privates_ind: dictionary to fill
421 @param namespace: namespace of the values 421 @param namespace: namespace of the values
424 424
425 def fillPrivates(result): 425 def fillPrivates(result):
426 for private in result: 426 for private in result:
427 key, value = private 427 key, value = private
428 private_ind[key] = pickle.loads(str(value)) 428 private_ind[key] = pickle.loads(str(value))
429 log.debug(_("loading individual private binary values [namespace: %s] from database") % (namespace, )) 429 log.debug(_(u"loading individual private binary values [namespace: %s] from database") % (namespace,))
430 d = self.dbpool.runQuery("SELECT key,value FROM private_ind_bin WHERE namespace=? AND profile_id=?", (namespace, self.profiles[profile])) 430 d = self.dbpool.runQuery("SELECT key,value FROM private_ind_bin WHERE namespace=? AND profile_id=?", (namespace, self.profiles[profile]))
431 d.addCallback(fillPrivates) 431 d.addCallback(fillPrivates)
432 return d.addErrback(lambda x: log.debug(_("No binary data present in database for namespace %s") % namespace)) 432 return d.addErrback(lambda x: log.debug(_(u"No binary data present in database for namespace %s") % namespace))
433 433
434 def setGenPrivateBinary(self, namespace, key, value): 434 def setGenPrivateBinary(self, namespace, key, value):
435 """Save the general private binary value in database 435 """Save the general private binary value in database
436 @param category: category of the privateeter 436 @param category: category of the privateeter
437 @param key: key of the private value 437 @param key: key of the private value
438 @param value: value to set 438 @param value: value to set
439 @return: deferred""" 439 @return: deferred"""
440 d = self.dbpool.runQuery("REPLACE INTO private_gen_bin(namespace,key,value) VALUES (?,?,?)", (namespace, key, pickle.dumps(value, 0))) 440 d = self.dbpool.runQuery("REPLACE INTO private_gen_bin(namespace,key,value) VALUES (?,?,?)", (namespace, key, pickle.dumps(value, 0)))
441 d.addErrback(lambda ignore: log.error(_("Can't set general private binary value (%(key)s) [namespace:%(namespace)s] in database" % 441 d.addErrback(lambda ignore: log.error(_(u"Can't set general private binary value (%(key)s) [namespace:%(namespace)s] in database" %
442 {"namespace": namespace, "key": key}))) 442 {"namespace": namespace, "key": key})))
443 return d 443 return d
444 444
445 def setIndPrivateBinary(self, namespace, key, value, profile): 445 def setIndPrivateBinary(self, namespace, key, value, profile):
446 """Save the individual private binary value in database 446 """Save the individual private binary value in database
448 @param key: key of the private value 448 @param key: key of the private value
449 @param value: value to set 449 @param value: value to set
450 @param profile: a profile which *must* exist 450 @param profile: a profile which *must* exist
451 @return: deferred""" 451 @return: deferred"""
452 d = self.dbpool.runQuery("REPLACE INTO private_ind_bin(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], pickle.dumps(value, 0))) 452 d = self.dbpool.runQuery("REPLACE INTO private_ind_bin(namespace,key,profile_id,value) VALUES (?,?,?,?)", (namespace, key, self.profiles[profile], pickle.dumps(value, 0)))
453 d.addErrback(lambda ignore: log.error(_("Can't set individual binary private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % 453 d.addErrback(lambda ignore: log.error(_(u"Can't set individual binary private value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" %
454 {"namespace": namespace, "key": key, "profile": profile}))) 454 {"namespace": namespace, "key": key, "profile": profile})))
455 return d 455 return d
456 456
457 def delGenPrivateBinary(self, namespace, key): 457 def delGenPrivateBinary(self, namespace, key):
458 """Delete the general private binary value from database 458 """Delete the general private binary value from database
459 @param category: category of the privateeter 459 @param category: category of the privateeter
460 @param key: key of the private value 460 @param key: key of the private value
461 @return: deferred""" 461 @return: deferred"""
462 d = self.dbpool.runQuery("DELETE FROM private_gen_bin WHERE namespace=? AND key=?", (namespace, key)) 462 d = self.dbpool.runQuery("DELETE FROM private_gen_bin WHERE namespace=? AND key=?", (namespace, key))
463 d.addErrback(lambda ignore: log.error(_("Can't delete general private binary value (%(key)s) [namespace:%(namespace)s] in database" % 463 d.addErrback(lambda ignore: log.error(_(u"Can't delete general private binary value (%(key)s) [namespace:%(namespace)s] in database" %
464 {"namespace": namespace, "key": key}))) 464 {"namespace": namespace, "key": key})))
465 return d 465 return d
466 466
467 def delIndPrivateBinary(self, namespace, key, profile): 467 def delIndPrivateBinary(self, namespace, key, profile):
468 """Delete the individual private binary value from database 468 """Delete the individual private binary value from database
469 @param namespace: namespace of the value 469 @param namespace: namespace of the value
470 @param key: key of the private value 470 @param key: key of the private value
471 @param profile: a profile which *must* exist 471 @param profile: a profile which *must* exist
472 @return: deferred""" 472 @return: deferred"""
473 d = self.dbpool.runQuery("DELETE FROM private_ind_bin WHERE namespace=? AND key=? AND profile=?)", (namespace, key, self.profiles[profile])) 473 d = self.dbpool.runQuery("DELETE FROM private_ind_bin WHERE namespace=? AND key=? AND profile=?)", (namespace, key, self.profiles[profile]))
474 d.addErrback(lambda ignore: log.error(_("Can't delete individual private binary value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" % 474 d.addErrback(lambda ignore: log.error(_(u"Can't delete individual private binary value (%(key)s) [namespace: %(namespace)s] for [%(profile)s] in database" %
475 {"namespace": namespace, "key": key, "profile": profile}))) 475 {"namespace": namespace, "key": key, "profile": profile})))
476 return d 476 return d
477 ##Helper methods## 477 ##Helper methods##
478 478
479 def __getFirstResult(self, result): 479 def __getFirstResult(self, result):
779 779
780 def prepare_queries(result, xmpp_password): 780 def prepare_queries(result, xmpp_password):
781 try: 781 try:
782 id_ = result[0][0] 782 id_ = result[0][0]
783 except IndexError: 783 except IndexError:
784 log.error("Profile of id %d is referenced in 'param_ind' but it doesn't exist!" % profile_id) 784 log.error(u"Profile of id %d is referenced in 'param_ind' but it doesn't exist!" % profile_id)
785 return defer.succeed(None) 785 return defer.succeed(None)
786 786
787 sat_password = xmpp_password 787 sat_password = xmpp_password
788 d1 = PasswordHasher.hash(sat_password) 788 d1 = PasswordHasher.hash(sat_password)
789 personal_key = BlockCipher.getRandomKey(base64=True) 789 personal_key = BlockCipher.getRandomKey(base64=True)