Mercurial > libervia-backend
changeset 2088:c02f96756d5c
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.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 04 Dec 2016 18:16:37 +0100 (2016-12-04) |
parents | 159250d66407 |
children | 0931b5a6213c |
files | src/core/constants.py src/core/sat_main.py src/tools/common/dynamic_import.py |
diffstat | 3 files changed, 56 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/core/constants.py Wed Oct 05 22:07:51 2016 +0200 +++ b/src/core/constants.py Sun Dec 04 18:16:37 2016 +0100 @@ -35,6 +35,10 @@ APP_URL = u'http://salut-a-toi.org' + # Main config ## + DEFAULT_BRIDGE = 'dbus' + + # Protocol XMPP_C2S_PORT = 5222 XMPP_KEEP_ALIFE = 180
--- a/src/core/sat_main.py Wed Oct 05 22:07:51 2016 +0200 +++ b/src/core/sat_main.py Sun Dec 04 18:16:37 2016 +0100 @@ -25,7 +25,6 @@ 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.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 @@ 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 @@ 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"))
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/common/dynamic_import.py Sun Dec 04 18:16:37 2016 +0100 @@ -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