From a95806b6937bfca5a08aa01fb24a3007abcd9b75 Mon Sep 17 00:00:00 2001 From: romainx Date: Fri, 26 Jul 2024 08:33:28 +0200 Subject: [PATCH 1/3] Autocomplete for products --- pyproject.toml | 1 + src/norwegianblue/__init__.py | 10 +++++----- src/norwegianblue/cli.py | 8 ++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 15fb961..ad90c1c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,6 +47,7 @@ dependencies = [ "python-dateutil", "python-slugify", "termcolor>=2.1", + "argcomplete>=3.4" ] optional-dependencies.tests = [ "freezegun", diff --git a/src/norwegianblue/__init__.py b/src/norwegianblue/__init__.py index 1e70c9f..c0e8434 100644 --- a/src/norwegianblue/__init__.py +++ b/src/norwegianblue/__init__.py @@ -99,20 +99,20 @@ def norwegianblue( return output +def all_products() -> list[str]: + """Get all known products from the API or cache""" + return norwegianblue("all").splitlines() @lru_cache(maxsize=None) def suggest_product(product: str) -> str: + """Provide the best suggestion based on a typed product""" import difflib - # Get all known products from the API or cache - all_products = norwegianblue("all").splitlines() - # Find the closest match - result = difflib.get_close_matches(product, all_products, n=1) + result = difflib.get_close_matches(product, all_products(), n=1) logging.info("Suggestion:\t%s (score: %d)", *result) return result[0] if result else "" - def _ltsify(data: list[dict]) -> list[dict]: """If a cycle is LTS, append LTS to the cycle version and remove the LTS column""" for cycle in data: diff --git a/src/norwegianblue/cli.py b/src/norwegianblue/cli.py index 46cc43f..714d22f 100644 --- a/src/norwegianblue/cli.py +++ b/src/norwegianblue/cli.py @@ -14,6 +14,7 @@ from __future__ import annotations import argparse +import argcomplete import atexit import logging import platform @@ -26,6 +27,9 @@ atexit.register(_cache.clear) +def ProductCompleter(**kwargs): + """The list of all products to feed autocompletion""" + return norwegianblue.all_products() def main() -> None: parser = argparse.ArgumentParser( @@ -36,7 +40,7 @@ def main() -> None: nargs="*", default=["all"], help="product to check, or 'all' to list all available (default: 'all')", - ) + ).completer = ProductCompleter parser.add_argument( "-f", "--format", @@ -115,7 +119,7 @@ def main() -> None: help=f"output in {help_text}", ) parser.set_defaults(formatter="pretty") - + argcomplete.autocomplete(parser) args = parser.parse_args() if args.format: From f4eefd779f2c15068eafb35d2ede16d9bad02756 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 06:41:57 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyproject.toml | 2 +- src/norwegianblue/__init__.py | 3 +++ src/norwegianblue/cli.py | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ad90c1c..54df250 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ dynamic = [ "version", ] dependencies = [ + "argcomplete>=3.4", "httpx>=0.19", "platformdirs", "prettytable>=2.4", @@ -47,7 +48,6 @@ dependencies = [ "python-dateutil", "python-slugify", "termcolor>=2.1", - "argcomplete>=3.4" ] optional-dependencies.tests = [ "freezegun", diff --git a/src/norwegianblue/__init__.py b/src/norwegianblue/__init__.py index c0e8434..b95f068 100644 --- a/src/norwegianblue/__init__.py +++ b/src/norwegianblue/__init__.py @@ -99,10 +99,12 @@ def norwegianblue( return output + def all_products() -> list[str]: """Get all known products from the API or cache""" return norwegianblue("all").splitlines() + @lru_cache(maxsize=None) def suggest_product(product: str) -> str: """Provide the best suggestion based on a typed product""" @@ -113,6 +115,7 @@ def suggest_product(product: str) -> str: logging.info("Suggestion:\t%s (score: %d)", *result) return result[0] if result else "" + def _ltsify(data: list[dict]) -> list[dict]: """If a cycle is LTS, append LTS to the cycle version and remove the LTS column""" for cycle in data: diff --git a/src/norwegianblue/cli.py b/src/norwegianblue/cli.py index 714d22f..a803a9b 100644 --- a/src/norwegianblue/cli.py +++ b/src/norwegianblue/cli.py @@ -14,12 +14,12 @@ from __future__ import annotations import argparse -import argcomplete import atexit import logging import platform import sys +import argcomplete from termcolor import colored import norwegianblue @@ -27,10 +27,12 @@ atexit.register(_cache.clear) + def ProductCompleter(**kwargs): """The list of all products to feed autocompletion""" return norwegianblue.all_products() + def main() -> None: parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter @@ -40,7 +42,7 @@ def main() -> None: nargs="*", default=["all"], help="product to check, or 'all' to list all available (default: 'all')", - ).completer = ProductCompleter + ).completer = ProductCompleter parser.add_argument( "-f", "--format", From 6a61c191bff4c7a3fedd7ea037b4631dd6ee75fd Mon Sep 17 00:00:00 2001 From: romainx Date: Fri, 26 Jul 2024 10:31:36 +0200 Subject: [PATCH 3/3] Added flag to work with global completion --- src/norwegianblue/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/norwegianblue/cli.py b/src/norwegianblue/cli.py index a803a9b..b6714bd 100644 --- a/src/norwegianblue/cli.py +++ b/src/norwegianblue/cli.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# PYTHON_ARGCOMPLETE_OK """ CLI to show end-of-life dates for a number of products, from https://endoflife.date