File: python.py

package info (click to toggle)
python-sybil 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,148 kB
  • sloc: python: 4,510; makefile: 90
file content (45 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download | duplicates (2)
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
import importlib
import sys
from collections.abc import Iterator
from contextlib import contextmanager
from types import ModuleType

from pathlib import Path


@contextmanager
def import_cleanup() -> Iterator[None]:
    """
    Clean up the results of importing modules, including the modification
    of :attr:`sys.path` necessary to do so.
    """
    modules = set(sys.modules)
    path = sys.path.copy()
    yield
    for added_module in set(sys.modules) - modules:
        sys.modules.pop(added_module)
    sys.path[:] = path
    importlib.invalidate_caches()


INIT_FILE = '__init__.py'


def import_path(path: Path) -> ModuleType:
    container = path
    while True:
        container = container.parent
        if not (container / INIT_FILE).exists():
            break
    relative = path.relative_to(container)
    if relative.name == INIT_FILE:
        parts = tuple(relative.parts)[:-1]
    else:
        parts = tuple(relative.parts)[:-1]+(relative.stem,)
    module = '.'.join(parts)
    try:
        return importlib.import_module(module)
    except ImportError as e:
        raise ImportError(
            f'{module!r} not importable from {path} as:\n{type(e).__name__}: {e}'
        ) from None