Skip to content

Commit

Permalink
fix: docstrings and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
qdelamea-aneo committed Nov 2, 2024
1 parent 8188db8 commit 62c302e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/armonik_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import rich_click as click


from armonik_cli import commands, version
from armonik_cli import commands, __version__


@click.group(name="armonik")
@click.version_option(version=version.__version__, prog_name="armonik")
@click.version_option(version=__version__, prog_name="armonik")
def cli() -> None:
"""
ArmoniK CLI is a tool to monitor and manage ArmoniK clusters.
Expand Down
60 changes: 54 additions & 6 deletions src/armonik_cli/commands/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import rich_click as click

from datetime import timedelta
from typing import cast, Tuple, Union


endpoint_option = click.option(
"-e",
Expand All @@ -18,33 +20,79 @@
type=click.Choice(["yaml", "json", "table"], case_sensitive=False),
default="json",
show_default=True,
help="Endpoint of the cluster to connect to.",
help="Commands output format.",
metavar="FORMAT",
)
debug_option = click.option(
"--debug", is_flag=True, default=False, help="Print debug logs and internal errors."
)


class KeyValuePairParamType(click.ParamType):
class KeyValuePairParam(click.ParamType):
"""
A custom Click parameter type that parses a key-value pair in the format "key=value".
Attributes:
name (str): The name of the parameter type, used by Click.
"""

name = "key_value_pair"

def convert(self, value, param, ctx):
def convert(
self, value: str, param: Union[click.Parameter, None], ctx: Union[click.Context, None]
) -> Tuple[str, str]:
"""
Converts the input value into a tuple of (key, value) if it matches the required format.
Args:
value (str): The input value to be converted.
param (Union[click.Parameter, None]): The parameter object passed by Click.
ctx (Union[click.Parameter, None]): The context in which the parameter is being used.
Returns:
tuple: A tuple (key, value) if the input matches the format "key=value".
Raises:
click.BadParameter: If the input does not match the expected format.
"""
pattern = r"^([a-zA-Z0-9_-]+)=([a-zA-Z0-9_-]+)$"
match_result = re.match(pattern, value)
if match_result:
return match_result.groups()
return cast(Tuple[str, str], match_result.groups())
self.fail(
f"{value} is not a valid key value pair. Use key=value where both key and value contain only alphanumeric characters, dashes (-), and underscores (_).",
param,
ctx,
)


class TimeDeltaParamType(click.ParamType):
class TimeDeltaParam(click.ParamType):
"""
A custom Click parameter type that parses a time duration string in the format "HH:MM:SS.MS".
Attributes:
name (str): The name of the parameter type, used by Click.
"""

name = "timedelta"

def convert(self, value, param, ctx):
def convert(
self, value: str, param: Union[click.Parameter, None], ctx: Union[click.Context, None]
) -> timedelta:
"""
Converts the input value into a timedelta object if it matches the required time format.
Args:
value (str): The input value to be converted.
param (Union[click.Parameter, None]): The parameter object passed by Click.
ctx (Union[click.Parameter, None]): The context in which the parameter is being used.
Returns:
timedelta: A timedelta object representing the parsed time duration.
Raises:
click.BadParameter: If the input does not match the expected time format.
"""
try:
return self._parse_time_delta(value)
except ValueError:
Expand Down
10 changes: 5 additions & 5 deletions src/armonik_cli/commands/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
endpoint_option,
output_option,
debug_option,
KeyValuePairParamType,
TimeDeltaParamType,
KeyValuePairParam,
TimeDeltaParam,
)


Expand Down Expand Up @@ -51,7 +51,7 @@ def list(endpoint: str, output: str, debug: bool) -> None:
@endpoint_option
@output_option
@debug_option
@click.argument("session-id", required=True, type=str, metavar="SESSION_ID")
@session_argument
@errors.error_handler
def get(endpoint: str, output: str, session_id: str, debug: bool) -> None:
"""Get details of a given session."""
Expand All @@ -73,7 +73,7 @@ def get(endpoint: str, output: str, session_id: str, debug: bool) -> None:
)
@click.option(
"--max-duration",
type=TimeDeltaParamType(),
type=TimeDeltaParam(),
required=True,
help="Maximum default task execution time (format HH:MM:SS.MS).",
metavar="DURATION",
Expand Down Expand Up @@ -125,7 +125,7 @@ def get(endpoint: str, output: str, session_id: str, debug: bool) -> None:
)
@click.option(
"--option",
type=KeyValuePairParamType(),
type=KeyValuePairParam(),
required=False,
multiple=True,
help="Additional default options.",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def test_armonik_version():
runner = CliRunner()
result = runner.invoke(cli.cli, ["--version"])
assert result.exit_code == 0
assert result.output == "armonik, version 0.0.1\n"
assert result.output.startswith("armonik, version ")

0 comments on commit 62c302e

Please sign in to comment.