Skip to content

Commit

Permalink
Autocomplete for products
Browse files Browse the repository at this point in the history
  • Loading branch information
romainx committed Jul 26, 2024
1 parent 63caff9 commit a95806b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies = [
"python-dateutil",
"python-slugify",
"termcolor>=2.1",
"argcomplete>=3.4"
]
optional-dependencies.tests = [
"freezegun",
Expand Down
10 changes: 5 additions & 5 deletions src/norwegianblue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions src/norwegianblue/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

import argparse
import argcomplete
import atexit
import logging
import platform
Expand All @@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a95806b

Please sign in to comment.