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
This commit is contained in:
Goffi 2013-06-21 02:00:37 +02:00
commit 8e2f394b5b
8 changed files with 55 additions and 26 deletions

View file

@ -20,6 +20,7 @@
from bridge_frontend import BridgeFrontend
import dbus
from logging import debug, error
from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
@ -31,10 +32,6 @@ const_CORE_SUFFIX = ".core"
const_PLUGIN_SUFFIX = ".plugin"
class BridgeExceptionNoService(Exception):
pass
class DBusBridgeFrontend(BridgeFrontend):
def __init__(self):
try:
@ -48,6 +45,9 @@ class DBusBridgeFrontend(BridgeFrontend):
except dbus.exceptions.DBusException, e:
if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown':
raise BridgeExceptionNoService
elif e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported':
print u"D-Bus is not launched, please see README to see instructions on how to launch it"
raise BridgeInitError
else:
raise e
#props = self.db_core_iface.getProperties()

View file

@ -53,7 +53,8 @@ from os.path import abspath, basename, dirname
from optparse import OptionParser
from sat.tools.jid import JID
import gobject
from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
from sat_frontends.bridge.DBus import DBusBridgeFrontend
from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
from sat.tools.utils import clean_ustr
import tarfile
import tempfile
@ -74,7 +75,9 @@ class JP(object):
self.bridge=DBusBridgeFrontend()
except BridgeExceptionNoService:
print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
import sys
sys.exit(1)
except BridgeInitError:
print(_(u"Can't init bridge"))
sys.exit(1)
self.transfer_data = None

View file

@ -17,9 +17,11 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from logging import debug, info, warning, error
from sat.tools.jid import JID
from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
from sat_frontends.bridge.DBus import DBusBridgeFrontend
from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate
from optparse import OptionParser
@ -41,7 +43,9 @@ class QuickApp(object):
self.bridge=DBusBridgeFrontend()
except BridgeExceptionNoService:
print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
import sys
sys.exit(1)
except BridgeInitError:
print(_(u"Can't init bridge"))
sys.exit(1)
self.bridge.register("connected", self.connected)
self.bridge.register("disconnected", self.disconnected)

View file

@ -24,6 +24,7 @@ import dbus.mainloop.glib
import inspect
from logging import debug, info, error
from twisted.internet.defer import Deferred
from sat.core.exceptions import BridgeInitError
const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
@ -506,7 +507,12 @@ class DBusBridge(Bridge):
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Bridge.__init__(self)
info("Init DBus...")
self.session_bus = dbus.SessionBus()
try:
self.session_bus = dbus.SessionBus()
except dbus.DBusException as e:
if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported':
print u"D-Bus is not launched, please see README to see instructions on how to launch it"
raise BridgeInitError
self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)

View file

@ -24,6 +24,7 @@ import dbus.mainloop.glib
import inspect
from logging import debug, info, error
from twisted.internet.defer import Deferred
from sat.core.exceptions import BridgeInitError
const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
@ -204,7 +205,12 @@ class DBusBridge(Bridge):
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Bridge.__init__(self)
info("Init DBus...")
self.session_bus = dbus.SessionBus()
try:
self.session_bus = dbus.SessionBus()
except dbus.DBusException as e:
if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported':
print u"D-Bus is not launched, please see README to see instructions on how to launch it"
raise BridgeInitError
self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH)

View file

@ -20,6 +20,7 @@
from bridge_frontend import BridgeFrontend
import dbus
from logging import debug, error
from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
@ -31,10 +32,6 @@ const_CORE_SUFFIX = ".core"
const_PLUGIN_SUFFIX = ".plugin"
class BridgeExceptionNoService(Exception):
pass
class DBusBridgeFrontend(BridgeFrontend):
def __init__(self):
try:
@ -48,6 +45,9 @@ class DBusBridgeFrontend(BridgeFrontend):
except dbus.exceptions.DBusException, e:
if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown':
raise BridgeExceptionNoService
elif e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported':
print u"D-Bus is not launched, please see README to see instructions on how to launch it"
raise BridgeInitError
else:
raise e
#props = self.db_core_iface.getProperties()

View file

@ -44,3 +44,9 @@ class NotFound(Exception):
class DataError(Exception):
pass
class BridgeInitError(Exception):
pass
class BridgeExceptionNoService(Exception):
pass

View file

@ -37,7 +37,7 @@ import os.path
from sat.core.default_config import CONST
from sat.core import xmpp
from sat.core.exceptions import ProfileUnknownError, UnknownEntityError, ProfileNotInCacheError
from sat.core import exceptions
from sat.memory.memory import Memory
from sat.tools.xml_tools import tupleList2dataForm
from sat.tools.misc import TriggerManager
@ -105,7 +105,11 @@ class SAT(service.Service):
self.trigger = TriggerManager() # trigger are used to change SàT behaviour
self.bridge = DBusBridge()
try:
self.bridge = DBusBridge()
except exceptions.BridgeInitError:
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
sys.exit(1)
self.bridge.register("getVersion", lambda: self.get_const('client_version'))
self.bridge.register("getProfileName", self.memory.getProfileName)
self.bridge.register("getProfilesList", self.memory.getProfilesList)
@ -207,7 +211,7 @@ class SAT(service.Service):
profile = self.memory.getProfileName(profile_key)
if not profile:
error(_('Trying to connect a non-exsitant profile'))
raise ProfileUnknownError(profile_key)
raise exceptions.ProfileUnknownError(profile_key)
if self.isConnected(profile):
info(_("already connected !"))
@ -277,7 +281,7 @@ class SAT(service.Service):
def getContacts(self, profile_key):
client = self.getClient(profile_key)
if not client:
raise ProfileUnknownError(_('Asking contacts for a non-existant profile'))
raise exceptions.ProfileUnknownError(_('Asking contacts for a non-existant profile'))
ret = []
for item in client.roster.getItems(): # we get all items for client's roster
# and convert them to expected format
@ -288,7 +292,7 @@ class SAT(service.Service):
def getContactsFromGroup(self, group, profile_key):
client = self.getClient(profile_key)
if not client:
raise ProfileUnknownError(_("Asking group's contacts for a non-existant profile"))
raise exceptions.ProfileUnknownError(_("Asking group's contacts for a non-existant profile"))
return client.roster.getJidsFromGroup(group)
def purgeClient(self, profile):
@ -458,7 +462,7 @@ class SAT(service.Service):
assert(profile_key)
client = self.getClient(profile_key)
if not client:
raise ProfileNotInCacheError
raise exceptions.ProfileNotInCacheError
ret = []
for conf_id in client._waiting_conf:
conf_type, data = client._waiting_conf[conf_id][:2]
@ -491,7 +495,7 @@ class SAT(service.Service):
try:
entity_type = self.memory.getEntityData(mess_data["to"], ['type'], profile)["type"]
#FIXME: should entity_type manage ressources ?
except (UnknownEntityError, KeyError):
except (exceptions.UnknownEntityError, KeyError):
entity_type = "contact"
if entity_type == "chatroom":
@ -642,7 +646,7 @@ class SAT(service.Service):
"""
client = self.getClient(profile)
if not client:
raise ProfileUnknownError(_("Asking confirmation a non-existant profile"))
raise exceptions.ProfileUnknownError(_("Asking confirmation a non-existant profile"))
if conf_id in client._waiting_conf:
error(_("Attempt to register two callbacks for the same confirmation"))
else:
@ -653,7 +657,7 @@ class SAT(service.Service):
"""Called by frontends to answer confirmation requests"""
client = self.getClient(profile)
if not client:
raise ProfileUnknownError(_("Confirmation answer from a non-existant profile"))
raise exceptions.ProfileUnknownError(_("Confirmation answer from a non-existant profile"))
debug(_("Received confirmation answer for conf_id [%(conf_id)s]: %(success)s") % {'conf_id': conf_id, 'success': _("accepted") if accepted else _("refused")})
if conf_id not in client._waiting_conf:
error(_("Received an unknown confirmation (%(id)s for %(profile)s)") % {'id': conf_id, 'profile': profile})
@ -666,14 +670,14 @@ class SAT(service.Service):
"""Register a callback called when progress is requested for id"""
client = self.getClient(profile)
if not client:
raise ProfileUnknownError
raise exceptions.ProfileUnknownError
client._progress_cb_map[progress_id] = CB
def removeProgressCB(self, progress_id, profile):
"""Remove a progress callback"""
client = self.getClient(profile)
if not client:
raise ProfileUnknownError
raise exceptions.ProfileUnknownError
if progress_id not in client._progress_cb_map:
error(_("Trying to remove an unknow progress callback"))
else:
@ -686,7 +690,7 @@ class SAT(service.Service):
"""
client = self.getClient(profile)
if not profile:
raise ProfileNotInCacheError
raise exceptions.ProfileNotInCacheError
data = {}
try:
client._progress_cb_map[progress_id](progress_id, data, profile)