Skip to content

Commit

Permalink
📦 Add an entrypoint-based injector loader
Browse files Browse the repository at this point in the history
It is meant to eliminate import-based injector discovery, allowing the
injector callables to live in different distribution packages.
  • Loading branch information
webknjaz committed Oct 23, 2024
1 parent 2b29da6 commit d6e214d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/awx_plugins/interfaces/_temporary_private_injector_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Shared tools for loading injectors."""

from collections.abc import Callable
from importlib.metadata import entry_points as _load_matching_entry_points
from os import PathLike
from typing import Protocol, cast


_INJECTOR_ENTRY_POINT_GROUP_NAME = 'awx_plugins.injector'


class _CredentialInput(Protocol):
def get_input(
self: '_CredentialInput',
name: str,
default: object = None,
) -> bool | str:
"""Get an input from this credential.
:param name: Input name to check.
:type name: str
:param default: Fallback for a missing input.
:type default: object
"""

def has_input(self: '_CredentialInput', name: str) -> bool:
"""Check if an input is present.
:param name: Input name to check.
:type name: str
"""


_InjectorCallableType = Callable[
[_CredentialInput, dict[str, str], PathLike[str] | str],
None,
]


def load_injector_callable(injector_name: str, /) -> _InjectorCallableType:
"""Load an injector discovered by name into runtime.
:param injector_name: Entry-point key name under the
"awx_plugins.injector" group in packaging metadata.
:type injector_name: str
:raises LookupError: When no distribution packages provide the
requested entry-point name.
:returns: An injector callable.
:rtype: _InjectorCallableType
"""
injector_entry_points = _load_matching_entry_points(
group=_INJECTOR_ENTRY_POINT_GROUP_NAME,
name=injector_name,
)
if not injector_entry_points:
raise LookupError

injector_ep = injector_entry_points[injector_name]
return cast(_InjectorCallableType, injector_ep.load())

Check warning on line 59 in src/awx_plugins/interfaces/_temporary_private_injector_api.py

View check run for this annotation

Codecov / codecov/patch

src/awx_plugins/interfaces/_temporary_private_injector_api.py#L59

Added line #L59 was not covered by tests


__all__ = () # noqa: WPS410

0 comments on commit d6e214d

Please sign in to comment.