From 984d6db429bbfca657516b9d49019531f859cddf Mon Sep 17 00:00:00 2001 From: Daniel D'Avella Date: Thu, 6 Jun 2024 14:46:13 -0400 Subject: [PATCH] Allow arbitrary filtering of codemod registry --- src/codemodder/registry.py | 14 +++++++++++--- tests/test_codemod_docs.py | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/codemodder/registry.py b/src/codemodder/registry.py index 3ecddd1d..6a7a92dc 100644 --- a/src/codemodder/registry.py +++ b/src/codemodder/registry.py @@ -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 @@ -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, diff --git a/tests/test_codemod_docs.py b/tests/test_codemod_docs.py index 2442fe6a..7cf8e19d 100644 --- a/tests/test_codemod_docs.py +++ b/tests/test_codemod_docs.py @@ -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):