File: importers.py

package info (click to toggle)
plotly 5.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 82,992 kB
  • sloc: python: 368,735; javascript: 195,632; sh: 50; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import importlib


def relative_import(parent_name, rel_modules=(), rel_classes=()):
    """
    Helper function to import submodules lazily in Python 3.7+

    Parameters
    ----------
    rel_modules: list of str
        list of submodules to import, of the form .submodule
    rel_classes: list of str
        list of submodule classes/variables to import, of the form ._submodule.Foo

    Returns
    -------
    tuple
        Tuple that should be assigned to __all__, __getattr__ in the caller
    """
    module_names = {rel_module.split(".")[-1]: rel_module for rel_module in rel_modules}
    class_names = {rel_path.split(".")[-1]: rel_path for rel_path in rel_classes}

    def __getattr__(import_name):
        # In Python 3.7+, lazy import submodules

        # Check for submodule
        if import_name in module_names:
            rel_import = module_names[import_name]
            return importlib.import_module(rel_import, parent_name)

        # Check for submodule class
        if import_name in class_names:
            rel_path_parts = class_names[import_name].split(".")
            rel_module = ".".join(rel_path_parts[:-1])
            class_name = import_name
            class_module = importlib.import_module(rel_module, parent_name)
            return getattr(class_module, class_name)

        raise AttributeError(
            "module {__name__!r} has no attribute {name!r}".format(
                name=import_name, __name__=parent_name
            )
        )

    __all__ = list(module_names) + list(class_names)

    def __dir__():
        return __all__

    return __all__, __getattr__, __dir__