-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy path_loader.py
More file actions
27 lines (19 loc) · 861 Bytes
/
Copy path_loader.py
File metadata and controls
27 lines (19 loc) · 861 Bytes
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
"""Import an example module from its path.
Examples are loaded by path rather than by package import so that the
``docs/`` directory never has to end up on ``sys.path`` for consumers
other than the test suite, and so no example can shadow the top-level
``examples.py``.
"""
from __future__ import annotations
import importlib.util
import types
from ._registry import Demo
MODULE_PREFIX = 'progressbar_docs_examples'
def load_example(demo: Demo) -> types.ModuleType:
module_name = f'{MODULE_PREFIX}.{demo.name.replace("/", ".")}'
spec = importlib.util.spec_from_file_location(module_name, demo.path)
if spec is None or spec.loader is None: # pragma: no cover - unreachable
raise ImportError(f'cannot load example: {demo.path}')
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module