Skip to content

Commit

Permalink
Add load_plugins to pulp-glue
Browse files Browse the repository at this point in the history
This allows to auto-detect and load all installed pulp-glue plugins. It
is primarily useful for workflows where knowing all sub-types of an
Entity is only important at runtime.
  • Loading branch information
mdellweg committed Oct 3, 2024
1 parent 85dd5eb commit 271aa85
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/pulp-glue/+load_plugins.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `load_plugins` to `pulp_glue.common` so plugins providing a "pulp_glue.plugins" entrypoint can be enumerated and loaded.
3 changes: 3 additions & 0 deletions pulp-glue/docs/dev/reference/common.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pulp_glue.common

::: pulp_glue.common
31 changes: 31 additions & 0 deletions pulp-glue/pulp_glue/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
import sys
import typing as t

if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
else:
from importlib_metadata import entry_points

__version__ = "0.30.0.dev"

# Keep track to prevent loading plugins twice
loaded_plugins: t.Set[str] = set()


def load_plugins(enabled_plugins: t.Optional[t.List[str]] = None) -> None:
"""
Load glue plugins that provide a `pulp_glue.plugins` entrypoint.
This may be needed when you rely on the `TYPE_REGISTRY` attributes but cannot load the modules
explicitely.
Parameters:
enabled_plugins: Optional list of plugins to consider for loading.
"""
for entry_point in entry_points(group="pulp_glue.plugins"):
name = entry_point.name
if (
enabled_plugins is None or entry_point.name in enabled_plugins
) and entry_point.name not in loaded_plugins:
plugin = entry_point.load()
if hasattr(plugin, "mount"):
plugin.mount()
loaded_plugins.add(name)
9 changes: 9 additions & 0 deletions pulp-glue/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ documentation = "https://pulpproject.org/pulp-glue/docs/dev/"
repository = "https://github.com/pulp/pulp-cli"
changelog = "https://pulpproject.org/pulp-cli/changes/"

[project.entry-points."pulp_glue.plugins"]
ansible = "pulp_glue.ansible"
certguard = "pulp_glue.certguard"
container = "pulp_glue.container"
core = "pulp_glue.core"
file = "pulp_glue.file"
python = "pulp_glue.python"
rpm = "pulp_glue.rpm"

[tool.setuptools.packages.find]
where = ["."]
include = ["pulp_glue.*"]
Expand Down
10 changes: 10 additions & 0 deletions pulp-glue/tests/test_entity_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from pulp_glue.common import load_plugins, loaded_plugins
from pulp_glue.common.context import PulpContext, PulpRepositoryContext
from pulp_glue.file.context import PulpFileRepositoryContext

Expand All @@ -18,6 +19,15 @@ def file_repository(pulp_ctx: PulpContext) -> t.Dict[str, t.Any]:
file_repository_ctx.delete()


def test_plugin_loading() -> None:
load_plugins()
assert "core" in loaded_plugins


def test_type_registry() -> None:
assert "file:file" in PulpRepositoryContext.TYPE_REGISTRY


def test_detail_context(pulp_ctx: PulpContext, file_repository: t.Dict[str, t.Any]) -> None:
master_ctx = PulpRepositoryContext(pulp_ctx)
detail_ctx = master_ctx.detail_context(pulp_href=file_repository["pulp_href"])
Expand Down

0 comments on commit 271aa85

Please sign in to comment.