From fcdefa33afcbbd8d212ec85af578c5cf411df7ca Mon Sep 17 00:00:00 2001 From: kamangir Date: Sat, 9 Nov 2024 16:36:47 -0800 Subject: [PATCH] help refactor - kamangir/bolt#746 --- README.md | 2 +- abcli/.abcli/plugins/pypi.sh | 9 +-- abcli/.abcli/plugins/pypi/browse.sh | 11 ---- abcli/.abcli/plugins/pypi/build.sh | 12 ---- abcli/.abcli/plugins/pypi/install.sh | 8 --- abcli/.abcli/tests/help.sh | 7 +++ abcli/__init__.py | 2 +- abcli/help/functions.py | 60 +++++++++++--------- abcli/help/generic.py | 6 +- abcli/help/git.py | 4 +- abcli/help/pypi.py | 85 ++++++++++++++++++++++++++++ 11 files changed, 135 insertions(+), 71 deletions(-) create mode 100644 abcli/help/pypi.py diff --git a/README.md b/README.md index 8936bd1e..dc1627be 100644 --- a/README.md +++ b/README.md @@ -63,4 +63,4 @@ create a copy of [`sample.env`](./abcli/sample.env) as `.env` and fill in the se [![pylint](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pylint.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pylint.yml) [![pytest](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pytest.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/pytest.yml) [![bashtest](https://github.com/kamangir/awesome-bash-cli/actions/workflows/bashtest.yml/badge.svg)](https://github.com/kamangir/awesome-bash-cli/actions/workflows/bashtest.yml) [![PyPI version](https://img.shields.io/pypi/v/abcli.svg)](https://pypi.org/project/abcli/) [![PyPI - Downloads](https://img.shields.io/pypi/dd/abcli)](https://pypistats.org/packages/abcli) -built by 🌀 [`blue_options-4.153.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.411.1`](https://github.com/kamangir/awesome-bash-cli). +built by 🌀 [`blue_options-4.153.1`](https://github.com/kamangir/awesome-bash-cli), based on 🪄 [`abcli-9.412.1`](https://github.com/kamangir/awesome-bash-cli). diff --git a/abcli/.abcli/plugins/pypi.sh b/abcli/.abcli/plugins/pypi.sh index 3cf1991f..53352b03 100644 --- a/abcli/.abcli/plugins/pypi.sh +++ b/abcli/.abcli/plugins/pypi.sh @@ -6,20 +6,13 @@ function abcli_pypi() { local options=$2 local plugin_name=$(abcli_option "$options" plugin abcli) - if [[ "$task" == "help" ]]; then - abcli_pypi_browse "$1,$2" - abcli_pypi_build "$1,$2" - abcli_pypi_install "$1,$2" - return - fi - local function_name=abcli_pypi_$task if [[ $(type -t $function_name) == "function" ]]; then $function_name "${@:2}" return fi - abcli_log_error "-$plugin_name: pypi: $task: command not found." + abcli_log_error "$plugin_name: pypi: $task: command not found." return 1 } diff --git a/abcli/.abcli/plugins/pypi/browse.sh b/abcli/.abcli/plugins/pypi/browse.sh index fe125c24..b5ca1d6c 100644 --- a/abcli/.abcli/plugins/pypi/browse.sh +++ b/abcli/.abcli/plugins/pypi/browse.sh @@ -2,18 +2,7 @@ function abcli_pypi_browse() { local options=$1 - local plugin_name=$(abcli_option "$options" plugin abcli) - - if [ $(abcli_option_int "$options" help 0) == 1 ]; then - local callable=$([[ "$plugin_name" == "abcli" ]] && echo "@" || echo "$plugin_name ") - options="token" - [[ "$plugin_name" == abcli ]] && options="$options$EOP,plugin=$EOPE" - abcli_show_usage "${callable}pypi browse$ABCUL$options" \ - "browse pypi/$plugin_name." - return - fi - local do_token=$(abcli_option_int "$options" token 0) local module_name=$(abcli_get_module_name_from_plugin $plugin_name) diff --git a/abcli/.abcli/plugins/pypi/build.sh b/abcli/.abcli/plugins/pypi/build.sh index bb07e974..45fedfdc 100644 --- a/abcli/.abcli/plugins/pypi/build.sh +++ b/abcli/.abcli/plugins/pypi/build.sh @@ -1,21 +1,9 @@ #! /usr/bin/env bash -export abcli_pypi_build_options="browse,install,~rm_dist,~upload" - function abcli_pypi_build() { local options=$1 local plugin_name=$(abcli_option "$options" plugin abcli) - - if [ $(abcli_option_int "$options" help 0) == 1 ]; then - local callable=$([[ "$plugin_name" == "abcli" ]] && echo "@" || echo "$plugin_name ") - options="${EOP}$abcli_pypi_build_options$EOPE" - [[ "$plugin_name" == abcli ]] && options="$options$EOP,plugin=$EOPE" - abcli_show_usage "${callable}pypi build$ABCUL$options" \ - "$plugin_name -> pypi." - return - fi - local do_install=$(abcli_option_int "$options" install 0) local do_upload=$(abcli_option_int "$options" upload 1) local do_browse=$(abcli_option_int "$options" browse 0) diff --git a/abcli/.abcli/plugins/pypi/install.sh b/abcli/.abcli/plugins/pypi/install.sh index dd8d4d3a..a071a071 100644 --- a/abcli/.abcli/plugins/pypi/install.sh +++ b/abcli/.abcli/plugins/pypi/install.sh @@ -1,14 +1,6 @@ #! /usr/bin/env bash function abcli_pypi_install() { - local options=$1 - - if [ $(abcli_option_int "$options" help 0) == 1 ]; then - abcli_show_usage "@pypi install" \ - "install pypi." - return - fi - pip3 install --upgrade setuptools wheel twine python3 -m pip install --upgrade build } diff --git a/abcli/.abcli/tests/help.sh b/abcli/.abcli/tests/help.sh index 5cb547f8..6882294f 100644 --- a/abcli/.abcli/tests/help.sh +++ b/abcli/.abcli/tests/help.sh @@ -5,6 +5,13 @@ function test_abcli_help() { local module for module in \ + "@pypi" \ + "@pypi browse" \ + "@pypi build" \ + "@pypi install" \ + \ + "@pytest" \ + \ "@batch browse" \ "@batch cat" \ "@batch eval" \ diff --git a/abcli/__init__.py b/abcli/__init__.py index 4e4429c6..484e1e61 100644 --- a/abcli/__init__.py +++ b/abcli/__init__.py @@ -6,7 +6,7 @@ DESCRIPTION = f"{ICON} a language to speak AI." -VERSION = "9.411.1" +VERSION = "9.412.1" REPO_NAME = "awesome-bash-cli" diff --git a/abcli/help/functions.py b/abcli/help/functions.py index 0a72b870..533c8772 100644 --- a/abcli/help/functions.py +++ b/abcli/help/functions.py @@ -1,3 +1,5 @@ +from abcli.help.generic import help_functions as generic_help_functions + from abcli.help.aws_batch import help_functions as help_aws_batch from abcli.help.browse import help_browse from abcli.help.cp import help_cp @@ -16,6 +18,7 @@ from abcli.help.notebooks import help_functions as help_notebooks from abcli.help.plugins import help_functions as help_plugins from abcli.help.open import help_open +from abcli.help.pypi import help_functions as help_pypi from abcli.help.pytest import help_pytest from abcli.help.repeat import help_repeat from abcli.help.sagemaker import help_functions as help_sagemaker @@ -24,31 +27,34 @@ from abcli.help.upload import help_upload from abcli.help.watch import help_watch +help_functions = generic_help_functions(plugin_name="abcli") + -help_functions = { - "aws_batch": help_aws_batch, - "browse": help_browse, - "cp": help_cp, - "docker": help_docker, - "download": help_download, - "env": help_env, - "eval": help_eval, - "gif": help_gif, - "git": help_git, - "gpu": help_gpu, - "init": help_init, - "latex": help_latex, - "log": help_log, - "metadata": help_metadata, - "mlflow": help_mlflow, - "notebooks": help_notebooks, - "open": help_open, - "plugins": help_plugins, - "pytest": help_pytest, - "repeat": help_repeat, - "sagemaker": help_sagemaker, - "sleep": help_sleep, - "terraform": help_terraform, - "upload": help_upload, - "watch": help_watch, -} +help_functions.update( + { + "aws_batch": help_aws_batch, + "browse": help_browse, + "cp": help_cp, + "docker": help_docker, + "download": help_download, + "env": help_env, + "eval": help_eval, + "gif": help_gif, + "git": help_git, + "gpu": help_gpu, + "init": help_init, + "latex": help_latex, + "log": help_log, + "metadata": help_metadata, + "mlflow": help_mlflow, + "notebooks": help_notebooks, + "open": help_open, + "plugins": help_plugins, + "repeat": help_repeat, + "sagemaker": help_sagemaker, + "sleep": help_sleep, + "terraform": help_terraform, + "upload": help_upload, + "watch": help_watch, + } +) diff --git a/abcli/help/generic.py b/abcli/help/generic.py index 8ed8a414..2e0ae516 100644 --- a/abcli/help/generic.py +++ b/abcli/help/generic.py @@ -1,12 +1,14 @@ from typing import List, Dict, Callable, Union -from abcli.help.functions import help_pytest +from abcli.help.pytest import help_pytest +from abcli.help.pypi import help_functions as help_pypi -def generic_help_functions( +def help_functions( plugin_name: str = "abcli", ) -> Union[Callable, Dict[str, Union[Callable, Dict]]]: return { + "pypi": help_pypi(plugin_name=plugin_name), "pytest": lambda tokens, mono: help_pytest( tokens, mono=mono, diff --git a/abcli/help/git.py b/abcli/help/git.py index d5407f62..9cdce3dc 100644 --- a/abcli/help/git.py +++ b/abcli/help/git.py @@ -3,6 +3,8 @@ from blue_options.terminal import show_usage, xtra +from abcli.help.pypi import build_options as pypi_build_options + def help( tokens: List[str], @@ -198,7 +200,7 @@ def help_push( ] ) - build_options = "build,{}".format(os.getenv("abcli_pypi_build_options")) + build_options = f"build,{pypi_build_options}" return show_usage( [ diff --git a/abcli/help/pypi.py b/abcli/help/pypi.py new file mode 100644 index 00000000..efbef576 --- /dev/null +++ b/abcli/help/pypi.py @@ -0,0 +1,85 @@ +from typing import List, Dict, Callable, Union + +from blue_options.terminal import show_usage, xtra + +build_options = "browse,install,~rm_dist,~upload" + + +def help_browse( + tokens: List[str], + mono: bool, + plugin_name: str = "abcli", +) -> str: + options = "token" + + callable = f"{plugin_name} pypi" + + if plugin_name == "abcli": + options = f"plugin=,{options}" + callable = "@pypi" + + return show_usage( + callable.split(" ") + + [ + "browse", + f"[{options}]", + ], + f"browse pypi/{plugin_name}.", + mono=mono, + ) + + +def help_build( + tokens: List[str], + mono: bool, + plugin_name: str = "abcli", +) -> str: + options = xtra(build_options, mono=mono) + + callable = f"{plugin_name} pypi" + + if plugin_name == "abcli": + options = f"{options},plugin=" + callable = "@pypi" + + return show_usage( + callable.split(" ") + + [ + "build", + f"[{options}]", + ], + f"build pypi/{plugin_name}.", + mono=mono, + ) + + +def help_install( + tokens: List[str], + mono: bool, +) -> str: + return show_usage( + [ + "@pypi", + "install", + ], + "install pypi.", + mono=mono, + ) + + +def help_functions( + plugin_name: str = "abcli", +) -> Union[Callable, Dict[str, Union[Callable, Dict]]]: + return { + "browse": lambda tokens, mono: help_browse( + tokens, + mono=mono, + plugin_name=plugin_name, + ), + "build": lambda tokens, mono: help_build( + tokens, + mono=mono, + plugin_name=plugin_name, + ), + "install": help_install, + }