core: bridge can now be changed in conf

by default DBus bridge is used, but this can be changed in sat.conf's DEFAULT section using "bridge" option.
    bridge can be "dbus" (or equivalent "dbus_bridge") or embedded at the moment.
    Bridge name used is saved in SAT.bridge_name, and load_bridge method can be used by frontends.
This commit is contained in:
Goffi 2016-12-04 18:16:37 +01:00
commit 9ce91cf534
3 changed files with 56 additions and 2 deletions

View file

@ -35,6 +35,10 @@ class Const(object):
APP_URL = u'http://salut-a-toi.org'
# Main config ##
DEFAULT_BRIDGE = 'dbus'
# Protocol
XMPP_C2S_PORT = 5222
XMPP_KEEP_ALIFE = 180

View file

@ -25,7 +25,6 @@ from twisted.words.protocols.jabber import jid
from twisted.words.xish import domish
from twisted.internet import reactor
from wokkel.xmppim import RosterItem
from sat.bridge.dbus_bridge import Bridge
from sat.core import xmpp
from sat.core import exceptions
from sat.core.log import getLogger
@ -34,6 +33,7 @@ from sat.core.constants import Const as C
from sat.memory.memory import Memory
from sat.tools import trigger
from sat.tools import utils
from sat.tools.common import dynamic_import
from sat.stdui import ui_contact_list, ui_profile_manager
from glob import glob
from uuid import uuid4
@ -60,8 +60,15 @@ class SAT(service.Service):
self.memory = Memory(self)
self.trigger = trigger.TriggerManager() # trigger are used to change SàT behaviour
bridge_name = self.memory.getConfig('', 'bridge', 'dbus')
bridge_module = dynamic_import.bridge(bridge_name)
if bridge_module is None:
log.error(u"Can't find bridge module of name {}".format(bridge_name))
sys.exit(1)
log.info(u"using {} bridge".format(bridge_name))
try:
self.bridge = Bridge()
self.bridge = bridge_module.Bridge()
except exceptions.BridgeInitError:
log.error(u"Bridge can't be initialised, can't start SàT core")
sys.exit(1)
@ -132,6 +139,10 @@ class SAT(service.Service):
else:
return version
@property
def bridge_name(self):
return os.path.splitext(os.path.basename(self.bridge.__file__))[0]
def _postMemoryInit(self, ignore):
"""Method called after memory initialization is done"""
log.info(_("Memory initialised"))

View file

@ -0,0 +1,39 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# SàT: a XMPP
# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# 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/>.
""" tools dynamic import """
from importlib import import_module
def bridge(name, module_path='sat.bridge'):
"""Import bridge module
@param module_path(str): path of the module to import
@param name(str): name of the bridge to import (e.g.: dbus)
@return (module, None): imported module or None if nothing is found
"""
try:
bridge_module = import_module(module_path + '.' + name)
except ImportError:
try:
bridge_module = import_module(module_path + '.' + name + '_bridge')
except ImportError:
bridge_module = None
return bridge_module