__init__.py 1.85 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright(C) 2010-2021 Romain Bignon
#
# This file is part of woob.
#
# woob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# woob 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with woob. If not, see <http://www.gnu.org/licenses/>.
19

20 21 22 23
import sys
from importlib.abc import MetaPathFinder, Loader
import importlib

24
import woob
25

26 27 28 29 30

__title__ = woob.__title__
__version__ = woob.__version__
__author__ = woob.__author__
__copyright__ = woob.__copyright__
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


# Use case: the core does "import woob.x" but some app does "import weboob.x".
# If we merely set __path__, python will generate different modules
# though they have the same path. So the references will be different,
# isinstance(woob.X(), weboob.X) will fail, etc.
# Instead, we must return the same module to prevent Python from generating
# another one.
# Trick found at https://stackoverflow.com/a/56872393

class AliasLoader(Loader):
    def module_repr(self, module):
        return repr(module)

    def load_module(self, fullname):
        new_name = fullname.replace("weboob", "woob")
        module = importlib.import_module(new_name)
        sys.modules[fullname] = module
        return module


class AliasImporter(MetaPathFinder):
    def find_module(self, fullname, path=None):
        root_name, _, __ = fullname.partition(".")
        if root_name == "weboob":
            return AliasLoader()


59
sys.meta_path.insert(0, AliasImporter())