comparison src/core/sat_main.py @ 941:c6d8fc63b1db

core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
author Goffi <goffi@goffi.org>
date Fri, 28 Mar 2014 18:07:02 +0100
parents cbf4122baae7
children 71926ec2114d
comparison
equal deleted inserted replaced
940:92e41e7c7e00 941:c6d8fc63b1db
24 from twisted.words.protocols.jabber import jid, xmlstream 24 from twisted.words.protocols.jabber import jid, xmlstream
25 from twisted.words.xish import domish 25 from twisted.words.xish import domish
26 26
27 from twisted.internet import reactor 27 from twisted.internet import reactor
28 28
29 from wokkel import compat
30 from wokkel.xmppim import RosterItem 29 from wokkel.xmppim import RosterItem
31 30
32 from sat.bridge.DBus import DBusBridge 31 from sat.bridge.DBus import DBusBridge
33 import logging 32 import logging
34 from logging import debug, info, warning, error 33 from logging import debug, info, warning, error
38 37
39 from sat.core import xmpp 38 from sat.core import xmpp
40 from sat.core import exceptions 39 from sat.core import exceptions
41 from sat.core.constants import Const as C 40 from sat.core.constants import Const as C
42 from sat.memory.memory import Memory 41 from sat.memory.memory import Memory
43 from sat.tools.xml_tools import tupleList2dataForm
44 from sat.tools.misc import TriggerManager 42 from sat.tools.misc import TriggerManager
45 from glob import glob 43 from glob import glob
46 from uuid import uuid4 44 from uuid import uuid4
47 45
48 try: 46 try:
293 if disconnected_cb: 291 if disconnected_cb:
294 disconnected_cb(profile) 292 disconnected_cb(profile)
295 293
296 def getContacts(self, profile_key): 294 def getContacts(self, profile_key):
297 client = self.getClient(profile_key) 295 client = self.getClient(profile_key)
298 if not client:
299 raise exceptions.ProfileUnknownError(_('Asking contacts for a non-existant profile'))
300 ret = [] 296 ret = []
301 for item in client.roster.getItems(): # we get all items for client's roster 297 for item in client.roster.getItems(): # we get all items for client's roster
302 # and convert them to expected format 298 # and convert them to expected format
303 attr = client.roster.getAttributes(item) 299 attr = client.roster.getAttributes(item)
304 ret.append([item.jid.userhost(), attr, item.groups]) 300 ret.append([item.jid.userhost(), attr, item.groups])
305 return ret 301 return ret
306 302
307 def getContactsFromGroup(self, group, profile_key): 303 def getContactsFromGroup(self, group, profile_key):
308 client = self.getClient(profile_key) 304 client = self.getClient(profile_key)
309 if not client: 305 return [jid_.full() for jid_ in client.roster.getJidsFromGroup(group)]
310 raise exceptions.ProfileUnknownError(_("Asking group's contacts for a non-existant profile"))
311 return [jid.full() for jid in client.roster.getJidsFromGroup(group)]
312 306
313 def purgeClient(self, profile): 307 def purgeClient(self, profile):
314 """Remove reference to a profile client and purge cache 308 """Remove reference to a profile client and purge cache
315 the garbage collector can then free the memory""" 309 the garbage collector can then free the memory"""
316 try: 310 try:
348 def getClient(self, profile_key): 342 def getClient(self, profile_key):
349 """Convenient method to get client from profile key 343 """Convenient method to get client from profile key
350 @return: client or None if it doesn't exist""" 344 @return: client or None if it doesn't exist"""
351 profile = self.memory.getProfileName(profile_key) 345 profile = self.memory.getProfileName(profile_key)
352 if not profile: 346 if not profile:
353 return None 347 raise exceptions.ProfileKeyUnknownError
354 return self.profiles[profile] 348 return self.profiles[profile]
355 349
356 def getClients(self, profile_key): 350 def getClients(self, profile_key):
357 """Convenient method to get list of clients from profile key (manage list through profile_key like @ALL@) 351 """Convenient method to get list of clients from profile key (manage list through profile_key like @ALL@)
358 @param profile_key: %(doc_profile_key)s 352 @param profile_key: %(doc_profile_key)s
441 ## jabber methods ## 435 ## jabber methods ##
442 436
443 def getWaitingConf(self, profile_key=None): 437 def getWaitingConf(self, profile_key=None):
444 assert(profile_key) 438 assert(profile_key)
445 client = self.getClient(profile_key) 439 client = self.getClient(profile_key)
446 if not client:
447 raise exceptions.ProfileNotInCacheError
448 ret = [] 440 ret = []
449 for conf_id in client._waiting_conf: 441 for conf_id in client._waiting_conf:
450 conf_type, data = client._waiting_conf[conf_id][:2] 442 conf_type, data = client._waiting_conf[conf_id][:2]
451 ret.append((conf_id, conf_type, data)) 443 ret.append((conf_id, conf_type, data))
452 return ret 444 return ret
767 @param data: data (depend of confirmation conf_type) 759 @param data: data (depend of confirmation conf_type)
768 @param cb: callback called with the answer 760 @param cb: callback called with the answer
769 """ 761 """
770 # FIXME: use XMLUI and *callback methods for dialog 762 # FIXME: use XMLUI and *callback methods for dialog
771 client = self.getClient(profile) 763 client = self.getClient(profile)
772 if not client:
773 raise exceptions.ProfileUnknownError(_("Asking confirmation a non-existant profile"))
774 if conf_id in client._waiting_conf: 764 if conf_id in client._waiting_conf:
775 error(_("Attempt to register two callbacks for the same confirmation")) 765 error(_("Attempt to register two callbacks for the same confirmation"))
776 else: 766 else:
777 client._waiting_conf[conf_id] = (conf_type, data, cb) 767 client._waiting_conf[conf_id] = (conf_type, data, cb)
778 self.bridge.askConfirmation(conf_id, conf_type, data, profile) 768 self.bridge.askConfirmation(conf_id, conf_type, data, profile)
779 769
780 def confirmationAnswer(self, conf_id, accepted, data, profile): 770 def confirmationAnswer(self, conf_id, accepted, data, profile):
781 """Called by frontends to answer confirmation requests""" 771 """Called by frontends to answer confirmation requests"""
782 client = self.getClient(profile) 772 client = self.getClient(profile)
783 if not client:
784 raise exceptions.ProfileUnknownError(_("Confirmation answer from a non-existant profile"))
785 debug(_("Received confirmation answer for conf_id [%(conf_id)s]: %(success)s") % {'conf_id': conf_id, 'success': _("accepted") if accepted else _("refused")}) 773 debug(_("Received confirmation answer for conf_id [%(conf_id)s]: %(success)s") % {'conf_id': conf_id, 'success': _("accepted") if accepted else _("refused")})
786 if conf_id not in client._waiting_conf: 774 if conf_id not in client._waiting_conf:
787 error(_("Received an unknown confirmation (%(id)s for %(profile)s)") % {'id': conf_id, 'profile': profile}) 775 error(_("Received an unknown confirmation (%(id)s for %(profile)s)") % {'id': conf_id, 'profile': profile})
788 else: 776 else:
789 cb = client._waiting_conf[conf_id][-1] 777 cb = client._waiting_conf[conf_id][-1]
791 cb(conf_id, accepted, data, profile) 779 cb(conf_id, accepted, data, profile)
792 780
793 def registerProgressCB(self, progress_id, CB, profile): 781 def registerProgressCB(self, progress_id, CB, profile):
794 """Register a callback called when progress is requested for id""" 782 """Register a callback called when progress is requested for id"""
795 client = self.getClient(profile) 783 client = self.getClient(profile)
796 if not client:
797 raise exceptions.ProfileUnknownError
798 client._progress_cb_map[progress_id] = CB 784 client._progress_cb_map[progress_id] = CB
799 785
800 def removeProgressCB(self, progress_id, profile): 786 def removeProgressCB(self, progress_id, profile):
801 """Remove a progress callback""" 787 """Remove a progress callback"""
802 client = self.getClient(profile) 788 client = self.getClient(profile)
803 if not client:
804 raise exceptions.ProfileUnknownError
805 if progress_id not in client._progress_cb_map: 789 if progress_id not in client._progress_cb_map:
806 error(_("Trying to remove an unknow progress callback")) 790 error(_("Trying to remove an unknow progress callback"))
807 else: 791 else:
808 del client._progress_cb_map[progress_id] 792 del client._progress_cb_map[progress_id]
809 793
811 """Return a dict with progress information 795 """Return a dict with progress information
812 data['position'] : current possition 796 data['position'] : current possition
813 data['size'] : end_position 797 data['size'] : end_position
814 """ 798 """
815 client = self.getClient(profile) 799 client = self.getClient(profile)
816 if not profile:
817 raise exceptions.ProfileNotInCacheError
818 data = {} 800 data = {}
819 try: 801 try:
820 client._progress_cb_map[progress_id](progress_id, data, profile) 802 client._progress_cb_map[progress_id](progress_id, data, profile)
821 except KeyError: 803 except KeyError:
822 pass 804 pass