comparison src/core/sat_main.py @ 627:d207c2186519

core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised: - new BridgeInitError and BridgeExceptionNoService are in core.exceptions - D-Bus NotSupported is catched in bridge, and launch a BridgeInitError - BridgeInitError stop SàT core, jp, and quick_frontends with an explanation message. fix bug 27
author Goffi <goffi@goffi.org>
date Fri, 21 Jun 2013 02:00:37 +0200
parents 84a6e83157c2
children 7ea6d5a86e58
comparison
equal deleted inserted replaced
626:190ccc6dc36c 627:d207c2186519
35 import sys 35 import sys
36 import os.path 36 import os.path
37 37
38 from sat.core.default_config import CONST 38 from sat.core.default_config import CONST
39 from sat.core import xmpp 39 from sat.core import xmpp
40 from sat.core.exceptions import ProfileUnknownError, UnknownEntityError, ProfileNotInCacheError 40 from sat.core import exceptions
41 from sat.memory.memory import Memory 41 from sat.memory.memory import Memory
42 from sat.tools.xml_tools import tupleList2dataForm 42 from sat.tools.xml_tools import tupleList2dataForm
43 from sat.tools.misc import TriggerManager 43 from sat.tools.misc import TriggerManager
44 from glob import glob 44 from glob import glob
45 45
103 if not os.path.exists(local_dir): 103 if not os.path.exists(local_dir):
104 os.makedirs(local_dir) 104 os.makedirs(local_dir)
105 105
106 self.trigger = TriggerManager() # trigger are used to change SàT behaviour 106 self.trigger = TriggerManager() # trigger are used to change SàT behaviour
107 107
108 self.bridge = DBusBridge() 108 try:
109 self.bridge = DBusBridge()
110 except exceptions.BridgeInitError:
111 print (u"Bridge can't be initialised, can't start SàT core") # reactor is not launched yet, so we can't use error log
112 sys.exit(1)
109 self.bridge.register("getVersion", lambda: self.get_const('client_version')) 113 self.bridge.register("getVersion", lambda: self.get_const('client_version'))
110 self.bridge.register("getProfileName", self.memory.getProfileName) 114 self.bridge.register("getProfileName", self.memory.getProfileName)
111 self.bridge.register("getProfilesList", self.memory.getProfilesList) 115 self.bridge.register("getProfilesList", self.memory.getProfilesList)
112 self.bridge.register("getEntityData", lambda _jid, keys, profile: self.memory.getEntityData(jid.JID(_jid), keys, profile)) 116 self.bridge.register("getEntityData", lambda _jid, keys, profile: self.memory.getEntityData(jid.JID(_jid), keys, profile))
113 self.bridge.register("createProfile", self.memory.createProfile) 117 self.bridge.register("createProfile", self.memory.createProfile)
205 """ 209 """
206 210
207 profile = self.memory.getProfileName(profile_key) 211 profile = self.memory.getProfileName(profile_key)
208 if not profile: 212 if not profile:
209 error(_('Trying to connect a non-exsitant profile')) 213 error(_('Trying to connect a non-exsitant profile'))
210 raise ProfileUnknownError(profile_key) 214 raise exceptions.ProfileUnknownError(profile_key)
211 215
212 if self.isConnected(profile): 216 if self.isConnected(profile):
213 info(_("already connected !")) 217 info(_("already connected !"))
214 return defer.succeed("None") 218 return defer.succeed("None")
215 219
275 disconnected_cb(profile) 279 disconnected_cb(profile)
276 280
277 def getContacts(self, profile_key): 281 def getContacts(self, profile_key):
278 client = self.getClient(profile_key) 282 client = self.getClient(profile_key)
279 if not client: 283 if not client:
280 raise ProfileUnknownError(_('Asking contacts for a non-existant profile')) 284 raise exceptions.ProfileUnknownError(_('Asking contacts for a non-existant profile'))
281 ret = [] 285 ret = []
282 for item in client.roster.getItems(): # we get all items for client's roster 286 for item in client.roster.getItems(): # we get all items for client's roster
283 # and convert them to expected format 287 # and convert them to expected format
284 attr = client.roster.getAttributes(item) 288 attr = client.roster.getAttributes(item)
285 ret.append([item.jid.userhost(), attr, item.groups]) 289 ret.append([item.jid.userhost(), attr, item.groups])
286 return ret 290 return ret
287 291
288 def getContactsFromGroup(self, group, profile_key): 292 def getContactsFromGroup(self, group, profile_key):
289 client = self.getClient(profile_key) 293 client = self.getClient(profile_key)
290 if not client: 294 if not client:
291 raise ProfileUnknownError(_("Asking group's contacts for a non-existant profile")) 295 raise exceptions.ProfileUnknownError(_("Asking group's contacts for a non-existant profile"))
292 return client.roster.getJidsFromGroup(group) 296 return client.roster.getJidsFromGroup(group)
293 297
294 def purgeClient(self, profile): 298 def purgeClient(self, profile):
295 """Remove reference to a profile client and purge cache 299 """Remove reference to a profile client and purge cache
296 the garbage collector can then free the memory""" 300 the garbage collector can then free the memory"""
456 460
457 def getWaitingConf(self, profile_key=None): 461 def getWaitingConf(self, profile_key=None):
458 assert(profile_key) 462 assert(profile_key)
459 client = self.getClient(profile_key) 463 client = self.getClient(profile_key)
460 if not client: 464 if not client:
461 raise ProfileNotInCacheError 465 raise exceptions.ProfileNotInCacheError
462 ret = [] 466 ret = []
463 for conf_id in client._waiting_conf: 467 for conf_id in client._waiting_conf:
464 conf_type, data = client._waiting_conf[conf_id][:2] 468 conf_type, data = client._waiting_conf[conf_id][:2]
465 ret.append((conf_id, conf_type, data)) 469 ret.append((conf_id, conf_type, data))
466 return ret 470 return ret
489 elif not mess_data["to"].resource: # if to JID has a resource, the type is not 'groupchat' 493 elif not mess_data["to"].resource: # if to JID has a resource, the type is not 'groupchat'
490 # we may have a groupchat message, we check if the we know this jid 494 # we may have a groupchat message, we check if the we know this jid
491 try: 495 try:
492 entity_type = self.memory.getEntityData(mess_data["to"], ['type'], profile)["type"] 496 entity_type = self.memory.getEntityData(mess_data["to"], ['type'], profile)["type"]
493 #FIXME: should entity_type manage ressources ? 497 #FIXME: should entity_type manage ressources ?
494 except (UnknownEntityError, KeyError): 498 except (exceptions.UnknownEntityError, KeyError):
495 entity_type = "contact" 499 entity_type = "contact"
496 500
497 if entity_type == "chatroom": 501 if entity_type == "chatroom":
498 mess_data["type"] = 'groupchat' 502 mess_data["type"] = 'groupchat'
499 else: 503 else:
640 @param data: data (depend of confirmation conf_type) 644 @param data: data (depend of confirmation conf_type)
641 @param cb: callback called with the answer 645 @param cb: callback called with the answer
642 """ 646 """
643 client = self.getClient(profile) 647 client = self.getClient(profile)
644 if not client: 648 if not client:
645 raise ProfileUnknownError(_("Asking confirmation a non-existant profile")) 649 raise exceptions.ProfileUnknownError(_("Asking confirmation a non-existant profile"))
646 if conf_id in client._waiting_conf: 650 if conf_id in client._waiting_conf:
647 error(_("Attempt to register two callbacks for the same confirmation")) 651 error(_("Attempt to register two callbacks for the same confirmation"))
648 else: 652 else:
649 client._waiting_conf[conf_id] = (conf_type, data, cb) 653 client._waiting_conf[conf_id] = (conf_type, data, cb)
650 self.bridge.askConfirmation(conf_id, conf_type, data, profile) 654 self.bridge.askConfirmation(conf_id, conf_type, data, profile)
651 655
652 def confirmationAnswer(self, conf_id, accepted, data, profile): 656 def confirmationAnswer(self, conf_id, accepted, data, profile):
653 """Called by frontends to answer confirmation requests""" 657 """Called by frontends to answer confirmation requests"""
654 client = self.getClient(profile) 658 client = self.getClient(profile)
655 if not client: 659 if not client:
656 raise ProfileUnknownError(_("Confirmation answer from a non-existant profile")) 660 raise exceptions.ProfileUnknownError(_("Confirmation answer from a non-existant profile"))
657 debug(_("Received confirmation answer for conf_id [%(conf_id)s]: %(success)s") % {'conf_id': conf_id, 'success': _("accepted") if accepted else _("refused")}) 661 debug(_("Received confirmation answer for conf_id [%(conf_id)s]: %(success)s") % {'conf_id': conf_id, 'success': _("accepted") if accepted else _("refused")})
658 if conf_id not in client._waiting_conf: 662 if conf_id not in client._waiting_conf:
659 error(_("Received an unknown confirmation (%(id)s for %(profile)s)") % {'id': conf_id, 'profile': profile}) 663 error(_("Received an unknown confirmation (%(id)s for %(profile)s)") % {'id': conf_id, 'profile': profile})
660 else: 664 else:
661 cb = client._waiting_conf[conf_id][-1] 665 cb = client._waiting_conf[conf_id][-1]
664 668
665 def registerProgressCB(self, progress_id, CB, profile): 669 def registerProgressCB(self, progress_id, CB, profile):
666 """Register a callback called when progress is requested for id""" 670 """Register a callback called when progress is requested for id"""
667 client = self.getClient(profile) 671 client = self.getClient(profile)
668 if not client: 672 if not client:
669 raise ProfileUnknownError 673 raise exceptions.ProfileUnknownError
670 client._progress_cb_map[progress_id] = CB 674 client._progress_cb_map[progress_id] = CB
671 675
672 def removeProgressCB(self, progress_id, profile): 676 def removeProgressCB(self, progress_id, profile):
673 """Remove a progress callback""" 677 """Remove a progress callback"""
674 client = self.getClient(profile) 678 client = self.getClient(profile)
675 if not client: 679 if not client:
676 raise ProfileUnknownError 680 raise exceptions.ProfileUnknownError
677 if progress_id not in client._progress_cb_map: 681 if progress_id not in client._progress_cb_map:
678 error(_("Trying to remove an unknow progress callback")) 682 error(_("Trying to remove an unknow progress callback"))
679 else: 683 else:
680 del client._progress_cb_map[progress_id] 684 del client._progress_cb_map[progress_id]
681 685
684 data['position'] : current possition 688 data['position'] : current possition
685 data['size'] : end_position 689 data['size'] : end_position
686 """ 690 """
687 client = self.getClient(profile) 691 client = self.getClient(profile)
688 if not profile: 692 if not profile:
689 raise ProfileNotInCacheError 693 raise exceptions.ProfileNotInCacheError
690 data = {} 694 data = {}
691 try: 695 try:
692 client._progress_cb_map[progress_id](progress_id, data, profile) 696 client._progress_cb_map[progress_id](progress_id, data, profile)
693 except KeyError: 697 except KeyError:
694 pass 698 pass