|
| 1 | +"""Shared tools for loading injectors.""" |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +from importlib.metadata import entry_points as _load_matching_entry_points |
| 5 | +from os import PathLike |
| 6 | +from typing import Protocol, cast |
| 7 | + |
| 8 | + |
| 9 | +_INJECTOR_ENTRY_POINT_GROUP_NAME = 'awx_plugins.injector' |
| 10 | + |
| 11 | + |
| 12 | +class _CredentialInput(Protocol): |
| 13 | + def get_input( |
| 14 | + self: '_CredentialInput', |
| 15 | + name: str, |
| 16 | + default: object = None, |
| 17 | + ) -> bool | str: |
| 18 | + """Get an input from this credential. |
| 19 | +
|
| 20 | + :param name: Input name to check. |
| 21 | + :type name: str |
| 22 | + :param default: Fallback for a missing input. |
| 23 | + :type default: object |
| 24 | + """ |
| 25 | + |
| 26 | + def has_input(self: '_CredentialInput', name: str) -> bool: |
| 27 | + """Check if an input is present. |
| 28 | +
|
| 29 | + :param name: Input name to check. |
| 30 | + :type name: str |
| 31 | + """ |
| 32 | + |
| 33 | + |
| 34 | +InjectorCallableType = Callable[ |
| 35 | + [_CredentialInput, dict[str, str], PathLike[str] | str], |
| 36 | + None, |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +def load_injector_callable(injector_name: str, /) -> InjectorCallableType: |
| 41 | + """Load an injector discovered by name into runtime. |
| 42 | +
|
| 43 | + :param injector_name: Entry-point key name under the |
| 44 | + "awx_plugins.injector" group in packaging metadata. |
| 45 | + :type injector_name: str |
| 46 | + :raises LookupError: When no distribution packages provide the |
| 47 | + requested entry-point name. |
| 48 | + :returns: An injector callable. |
| 49 | + :rtype: InjectorCallableType |
| 50 | + """ |
| 51 | + injector_entry_points = _load_matching_entry_points( |
| 52 | + group=_INJECTOR_ENTRY_POINT_GROUP_NAME, |
| 53 | + name=injector_name, |
| 54 | + ) |
| 55 | + if not injector_entry_points: |
| 56 | + raise LookupError |
| 57 | + |
| 58 | + injector_ep = injector_entry_points[injector_name] |
| 59 | + return cast(InjectorCallableType, injector_ep.load()) |
| 60 | + |
| 61 | + |
| 62 | +__all__ = () # noqa: WPS410 |
0 commit comments