Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fragment support to entrypoints #134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/validate_pyproject/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ def fragment(self) -> str:


class PluginWrapper:
def __init__(self, tool: str, load_fn: "Plugin"):
def __init__(self, tool: str, load_fn: "Plugin", *, fragment: str = ""):
self._tool = tool
self._load_fn = load_fn
self._fragment = fragment

@property
def id(self) -> str:
Expand All @@ -73,7 +74,7 @@ def schema(self) -> "Schema":

@property
def fragment(self) -> str:
return ""
return self._fragment

@property
def help_text(self) -> str:
Expand All @@ -83,7 +84,10 @@ def help_text(self) -> str:
return Template(tpl).safe_substitute(tool=self.tool, id=self.id)

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.tool!r}, {self.id})"
args = [repr(self.tool), self.id]
if self.fragment:
args.append(f"fragment={self.fragment!r}")
return f"{self.__class__.__name__}({', '.join(args)})"


if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -118,7 +122,8 @@ def load_from_entry_point(entry_point: EntryPoint) -> PluginWrapper:
"""Carefully load the plugin, raising a meaningful message in case of errors"""
try:
fn = entry_point.load()
return PluginWrapper(entry_point.name, fn)
fragment = getattr(fn, "fragment", "")
return PluginWrapper(entry_point.name, fn, fragment=fragment)
except Exception as ex:
raise ErrorLoadingPlugin(entry_point=entry_point) from ex

Expand Down
15 changes: 15 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

import validate_pyproject.api
from validate_pyproject import plugins
from validate_pyproject.plugins import ENTRYPOINT_GROUP, ErrorLoadingPlugin

Expand All @@ -31,6 +32,20 @@ def test_load_from_entry_point__error():
plugins.load_from_entry_point(fake)


def test_load_from_entry_point_fragment(monkeypatch):
monkeypatch.setattr(
validate_pyproject.api.load_builtin_plugin,
"fragment",
"/properties/tool/properties",
raising=False,
)
entry = "validate_pyproject.api:load_builtin_plugin"
real = EntryPoint("cibuildwheel", entry, ENTRYPOINT_GROUP)
p = plugins.load_from_entry_point(real)
assert p.fragment == "/properties/tool/properties"
assert p.tool == "cibuildwheel"


def is_entry_point(ep):
return all(hasattr(ep, attr) for attr in ("name", "load"))

Expand Down