Debian Codegraph @

src/pygpi/entry.py

raw symbols SCIP
1 import importlib
2 import operator
3 import os
4 from typing import Callable, List, Tuple
5
6
7 def load_entry(argv: List[str]) -> None:
8 """Gather entry point information by parsing :envvar:`PYGPI_USERS`."""
9
10 entry_point_str = os.environ.get(
11 "PYGPI_USERS",
12 ",".join(
13 (
14 "cocotb_tools._coverage:start_cocotb_library_coverage",
15 "cocotb.logging:_configure",
16 "cocotb._init:init_package_from_simulation",
17 "cocotb._init:run_regression",
18 )
19 ),
20 )
21
22 # Parse the entry point string of the form "module:func,module:func,...".
23 # Any failure prevents any entry points from being loaded.
24 entry_points: List[Tuple[str, str]] = []
25 try:
26 entry_points_str = entry_point_str.split(",")
27 for entry_point_str in entry_points_str:
28 entry_module_str, entry_func_str = entry_point_str.split(":")
29 # TODO maybe some basic validation of the module and function names.
30 # WITHOUT IMPORTING THEM.
31 entry_points.append((entry_module_str, entry_func_str))
32 except Exception as e:
33 raise RuntimeError(f"Failure to parse PYGPI_USERS ('{entry_point_str}')") from e
34
35 # Run all entry points.
36 # Expect failure to stop the loading of any additional entry points.
37 for entry_module_str, entry_func_str in entry_points:
38 entry_module = importlib.import_module(entry_module_str)
39 entry_func: Callable[[List[str]], object] = operator.attrgetter(entry_func_str)(
40 entry_module
41 )
42 entry_func(argv)