From 9ce91cf5347f805ba61c5b920383ee26c528b843 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 4 Dec 2016 18:16:37 +0100 Subject: [PATCH] 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. --- src/core/constants.py | 4 +++ src/core/sat_main.py | 15 ++++++++++-- src/tools/common/dynamic_import.py | 39 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/tools/common/dynamic_import.py diff --git a/src/core/constants.py b/src/core/constants.py index 3234f8bb..3576217a 100644 --- a/src/core/constants.py +++ b/src/core/constants.py @@ -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 diff --git a/src/core/sat_main.py b/src/core/sat_main.py index f743aa14..d96497db 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -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")) diff --git a/src/tools/common/dynamic_import.py b/src/tools/common/dynamic_import.py new file mode 100644 index 00000000..0813626f --- /dev/null +++ b/src/tools/common/dynamic_import.py @@ -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 . + +""" 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