comparison src/memory/memory.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 b003dbd2b4e9
comparison
equal deleted inserted replaced
1408:8a7145138330 1409:3265a2639182
64 """ 64 """
65 if session_id is None: 65 if session_id is None:
66 session_id = str(uuid4()) 66 session_id = str(uuid4())
67 elif session_id in self._sessions: 67 elif session_id in self._sessions:
68 self._sessions[session_id][0].cancel() 68 self._sessions[session_id][0].cancel()
69 log.warning("Session [{id}] is going to be re-initialised".format(id=session_id)) 69 log.warning(u"Session [{id}] is going to be re-initialised".format(id=session_id))
70 timer = reactor.callLater(self.timeout, self._purgeSession, session_id) 70 timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
71 if session_data is None: 71 if session_data is None:
72 session_data = {} 72 session_data = {}
73 self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile) 73 self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile)
74 return session_id, session_data 74 return session_id, session_data
75 75
76 def _purgeSession(self, session_id): 76 def _purgeSession(self, session_id):
77 del self._sessions[session_id] 77 del self._sessions[session_id]
78 log.debug("Session [%s] purged" % session_id) 78 log.debug(u"Session [%s] purged" % session_id)
79 79
80 def __len__(self): 80 def __len__(self):
81 return len(self._sessions) 81 return len(self._sessions)
82 82
83 def __contains__(self, session_id): 83 def __contains__(self, session_id):
111 try: 111 try:
112 timer = self._sessions[session_id][0] 112 timer = self._sessions[session_id][0]
113 timer.cancel() 113 timer.cancel()
114 self._purgeSession(session_id) 114 self._purgeSession(session_id)
115 except KeyError: 115 except KeyError:
116 log.debug("Session [%s] doesn't exists, timeout expired?" % session_id) 116 log.debug(u"Session [%s] doesn't exists, timeout expired?" % session_id)
117 117
118 def keys(self): 118 def keys(self):
119 return self._sessions.keys() 119 return self._sessions.keys()
120 120
121 def iterkeys(self): 121 def iterkeys(self):
202 if current_value: 202 if current_value:
203 return # nothing to do 203 return # nothing to do
204 old_default = '~/.sat' 204 old_default = '~/.sat'
205 if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE): 205 if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
206 if not silent: 206 if not silent:
207 log.warning(_("A database has been found in the default local_dir for previous versions (< 0.5)")) 207 log.warning(_(u"A database has been found in the default local_dir for previous versions (< 0.5)"))
208 tools_config.fixConfigOption('', 'local_dir', old_default, silent) 208 tools_config.fixConfigOption('', 'local_dir', old_default, silent)
209 209
210 210
211 class Memory(object): 211 class Memory(object):
212 """This class manage all the persistent information""" 212 """This class manage all the persistent information"""
267 return False 267 return False
268 filename = os.path.expanduser(filename) 268 filename = os.path.expanduser(filename)
269 if os.path.exists(filename): 269 if os.path.exists(filename):
270 try: 270 try:
271 self.params.load_xml(filename) 271 self.params.load_xml(filename)
272 log.debug(_("Parameters loaded from file: %s") % filename) 272 log.debug(_(u"Parameters loaded from file: %s") % filename)
273 return True 273 return True
274 except Exception as e: 274 except Exception as e:
275 log.error(_("Can't load parameters from file: %s") % e) 275 log.error(_(u"Can't load parameters from file: %s") % e)
276 return False 276 return False
277 277
278 def save_xml(self, filename): 278 def save_xml(self, filename):
279 """Save parameters template to xml file 279 """Save parameters template to xml file
280 280
285 return False 285 return False
286 #TODO: need to encrypt files (at least passwords !) and set permissions 286 #TODO: need to encrypt files (at least passwords !) and set permissions
287 filename = os.path.expanduser(filename) 287 filename = os.path.expanduser(filename)
288 try: 288 try:
289 self.params.save_xml(filename) 289 self.params.save_xml(filename)
290 log.debug(_("Parameters saved to file: %s") % filename) 290 log.debug(_(u"Parameters saved to file: %s") % filename)
291 return True 291 return True
292 except Exception as e: 292 except Exception as e:
293 log.error(_("Can't save parameters to file: %s") % e) 293 log.error(_(u"Can't save parameters to file: %s") % e)
294 return False 294 return False
295 295
296 def load(self): 296 def load(self):
297 """Load parameters and all memory things from db""" 297 """Load parameters and all memory things from db"""
298 #parameters data 298 #parameters data
321 @return: a deferred None value 321 @return: a deferred None value
322 """ 322 """
323 def gotPersonalKey(personal_key): 323 def gotPersonalKey(personal_key):
324 """Create the session for this profile and store the personal key""" 324 """Create the session for this profile and store the personal key"""
325 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile) 325 self.auth_sessions.newSession({C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile)
326 log.debug('auth session created for profile %s' % profile) 326 log.debug(u'auth session created for profile %s' % profile)
327 327
328 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load() 328 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load()
329 d.addCallback(lambda data: BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY])) 329 d.addCallback(lambda data: BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY]))
330 return d.addCallback(gotPersonalKey) 330 return d.addCallback(gotPersonalKey)
331 331
335 log.info(_("[%s] Profile session purge" % profile)) 335 log.info(_("[%s] Profile session purge" % profile))
336 self.params.purgeProfile(profile) 336 self.params.purgeProfile(profile)
337 try: 337 try:
338 del self._entities_cache[profile] 338 del self._entities_cache[profile]
339 except KeyError: 339 except KeyError:
340 log.error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile) 340 log.error(_(u"Trying to purge roster status cache for a profile not in memory: [%s]") % profile)
341 341
342 def getProfilesList(self): 342 def getProfilesList(self):
343 return self.storage.getProfilesList() 343 return self.storage.getProfilesList()
344 344
345 def getProfileName(self, profile_key, return_profile_keys=False): 345 def getProfileName(self, profile_key, return_profile_keys=False):
493 full_jid = copy.copy(entity_jid) 493 full_jid = copy.copy(entity_jid)
494 full_jid.resource = resource 494 full_jid.resource = resource
495 try: 495 try:
496 presence_data = self.getEntityDatum(full_jid, "presence", profile_key) 496 presence_data = self.getEntityDatum(full_jid, "presence", profile_key)
497 except KeyError: 497 except KeyError:
498 log.debug("Can't get presence data for {}".format(full_jid)) 498 log.debug(u"Can't get presence data for {}".format(full_jid))
499 else: 499 else:
500 if presence_data.show != C.PRESENCE_UNAVAILABLE: 500 if presence_data.show != C.PRESENCE_UNAVAILABLE:
501 available.append(resource) 501 available.append(resource)
502 return available 502 return available
503 503
525 full_jid = copy.copy(entity_jid) 525 full_jid = copy.copy(entity_jid)
526 full_jid.resource = resource 526 full_jid.resource = resource
527 try: 527 try:
528 presence_data = self.getEntityDatum(full_jid, "presence", profile_key) 528 presence_data = self.getEntityDatum(full_jid, "presence", profile_key)
529 except KeyError: 529 except KeyError:
530 log.debug("No presence information for {}".format(full_jid)) 530 log.debug(u"No presence information for {}".format(full_jid))
531 continue 531 continue
532 priority_resources.append((resource, presence_data.priority)) 532 priority_resources.append((resource, presence_data.priority))
533 try: 533 try:
534 return max(priority_resources, key=lambda res_tuple: res_tuple[1])[0] 534 return max(priority_resources, key=lambda res_tuple: res_tuple[1])[0]
535 except ValueError: 535 except ValueError:
536 log.warning("No resource found at all for {}".format(entity_jid)) 536 log.warning(u"No resource found at all for {}".format(entity_jid))
537 return None 537 return None
538 538
539 ## Entities data ## 539 ## Entities data ##
540 540
541 def _getProfileCache(self, profile_key): 541 def _getProfileCache(self, profile_key):
799 return data.force(data_key) 799 return data.force(data_key)
800 800
801 return d.addCallback(cb) 801 return d.addCallback(cb)
802 802
803 def done(dummy): 803 def done(dummy):
804 log.debug(_('Personal data (%(ns)s, %(key)s) has been successfuly encrypted') % 804 log.debug(_(u'Personal data (%(ns)s, %(key)s) has been successfuly encrypted') %
805 {'ns': C.MEMORY_CRYPTO_NAMESPACE, 'key': data_key}) 805 {'ns': C.MEMORY_CRYPTO_NAMESPACE, 'key': data_key})
806 806
807 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load() 807 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load()
808 return d.addCallback(gotIndMemory).addCallback(done) 808 return d.addCallback(gotIndMemory).addCallback(done)
809 809
885 if not entity_jid.resource: 885 if not entity_jid.resource:
886 return bool(self.getAvailableResources) # is any resource is available, entity is available 886 return bool(self.getAvailableResources) # is any resource is available, entity is available
887 try: 887 try:
888 presence_data = self.getEntityDatum(entity_jid, "presence", profile_key) 888 presence_data = self.getEntityDatum(entity_jid, "presence", profile_key)
889 except KeyError: 889 except KeyError:
890 log.debug("No presence information for {}".format(entity_jid)) 890 log.debug(u"No presence information for {}".format(entity_jid))
891 return False 891 return False
892 return presence_data.show != C.PRESENCE_UNAVAILABLE 892 return presence_data.show != C.PRESENCE_UNAVAILABLE