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

Allow arbitrary filtering of codemod registry #628

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
14 changes: 11 additions & 3 deletions src/codemodder/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import re
from dataclasses import dataclass
from functools import cached_property
from importlib.metadata import entry_points
from importlib.metadata import EntryPoint, entry_points
from itertools import chain
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Callable, Optional

from codemodder.logging import logger

Expand Down Expand Up @@ -140,10 +140,18 @@ def describe_codemods(
return [codemod.describe() for codemod in codemods]


def load_registered_codemods() -> CodemodRegistry:
def load_registered_codemods(ep_filter: Optional[Callable[[EntryPoint], bool]] = None):
registry = CodemodRegistry()
logger.debug("loading registered codemod collections")
for entry_point in entry_points().select(group="codemods"):
if ep_filter and not ep_filter(entry_point):
logger.debug(
'- skipping codemod collection "%s" from "%s as requested"',
entry_point.name,
entry_point.module,
)
continue

logger.debug(
'- loading codemod collection "%s" from "%s"',
entry_point.name,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_codemod_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
def pytest_generate_tests(metafunc):
registry = load_registered_codemods()
if "codemod" in metafunc.fixturenames:
metafunc.parametrize("codemod", registry.codemods)
ids = [codemod.id for codemod in registry.codemods]
metafunc.parametrize("codemod", registry.codemods, ids=ids)


def test_load_codemod_docs_info(codemod: BaseCodemod):
Expand Down
Loading