comparison libervia/backend/tools/common/dynamic_import.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/tools/common/dynamic_import.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3 # Libervia: an XMPP client
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 """ tools dynamic import """
20
21 from importlib import import_module
22 from libervia.backend.core.log import getLogger
23
24
25 log = getLogger(__name__)
26
27
28 def bridge(name, module_path="libervia.backend.bridge"):
29 """import bridge module
30
31 @param module_path(str): path of the module to import
32 @param name(str): name of the bridge to import (e.g.: dbus)
33 @return (module, None): imported module or None if nothing is found
34 """
35 try:
36 bridge_module = import_module(module_path + "." + name)
37 except ImportError:
38 try:
39 bridge_module = import_module(module_path + "." + name + "_bridge")
40 except ImportError as e:
41 log.warning(f"Can't import bridge {name!r}: {e}")
42 bridge_module = None
43 return bridge_module