From a96e4eb307b123660092df02e38dc7cc62e4926e Mon Sep 17 00:00:00 2001 From: Victoria Slocum Date: Tue, 27 Jun 2023 10:10:07 +0200 Subject: [PATCH] Add cli for finding locations of registered func --- spacy/cli/__init__.py | 1 + spacy/cli/find_loc.py | 51 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 spacy/cli/find_loc.py diff --git a/spacy/cli/__init__.py b/spacy/cli/__init__.py index 549a27616..89d917fe4 100644 --- a/spacy/cli/__init__.py +++ b/spacy/cli/__init__.py @@ -14,6 +14,7 @@ from .debug_diff import debug_diff # noqa: F401 from .debug_model import debug_model # noqa: F401 from .download import download # noqa: F401 from .evaluate import evaluate # noqa: F401 +from .find_loc import find_loc # noqa: F401 from .find_threshold import find_threshold # noqa: F401 from .info import info # noqa: F401 from .init_config import fill_config, init_config # noqa: F401 diff --git a/spacy/cli/find_loc.py b/spacy/cli/find_loc.py new file mode 100644 index 000000000..30ccd4eb2 --- /dev/null +++ b/spacy/cli/find_loc.py @@ -0,0 +1,51 @@ +from wasabi import msg + +from ._util import Arg, Opt, app + +from ..util import registry +from catalogue import RegistryError + + +@app.command("find-loc") +def find_loc( + # fmt: off + func_name: str = Arg(..., help="Name of the registered function."), + registry_name: str = Opt(None, help="Name of the catalogue registry."), + # fmt: on +): + """ + Find the module, path and line number to the file the registered + function is defined in, if available. + + func_name (str): Name of the registered function. + registry_name (str): Name of the catalogue registry. + """ + registry_names = registry.get_registry_names() + for name in registry_names: + if registry.has(name, func_name): + registry_name = name + break + + find_loc(func_name, registry_name) + + +def find_loc(registry_func, registry_name): + try: + registry_desc = registry.find(registry_name, registry_func) + + except RegistryError as e: + msg.fail( + f"Couldn't find registered function: {registry_func}\n\n{e}", + exits=1, + ) + + if registry_desc["file"]: + registry_path = registry_desc["file"] + line_no = registry_desc["line_no"] + else: + msg.fail( + f"Couldn't find path to registered function: {registry_func}", + exits=1, + ) + + msg.good(f"Found registered function at file://{registry_path}:{line_no}")