From a44293e9d20baab98d3488ec52f14aa78480c00b Mon Sep 17 00:00:00 2001 From: Max Kutschka <2004maximilian@web.de> Date: Thu, 17 Oct 2024 11:54:31 +0200 Subject: [PATCH] feat: metric input is now a InputParamClass --- examples/metricq_get_history.py | 5 ++++- metricq/cli/__init__.py | 3 ++- metricq/cli/params.py | 25 +++++++++++++++++++++++++ metricq/cli/wrapper.py | 27 +-------------------------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/examples/metricq_get_history.py b/examples/metricq_get_history.py index 8078dbff..05bda1fe 100755 --- a/examples/metricq_get_history.py +++ b/examples/metricq_get_history.py @@ -36,7 +36,8 @@ import click_log # type: ignore import metricq -from metricq.cli import metric_input, metricq_command +from metricq.cli import metricq_command +from metricq.cli.params import metric_input logger = metricq.get_logger() @@ -107,6 +108,8 @@ async def aget_history( def get_history( server: str, token: str, metric: str, list_metrics: bool, list_metadata: bool ) -> None: + print(metric) + exit(0) if not (list_metrics or list_metadata) and metric is None: metric = "example.quantity" asyncio.run(aget_history(server, token, metric, list_metrics, list_metadata)) diff --git a/metricq/cli/__init__.py b/metricq/cli/__init__.py index 4f4b10f8..30419513 100644 --- a/metricq/cli/__init__.py +++ b/metricq/cli/__init__.py @@ -5,8 +5,9 @@ OutputFormat, TemplateStringParam, TimestampParam, + metric_input, ) -from .wrapper import metric_input, metricq_command +from .wrapper import metricq_command __all__ = [ "ChoiceParam", diff --git a/metricq/cli/params.py b/metricq/cli/params.py index 7bc97fba..abaef7de 100644 --- a/metricq/cli/params.py +++ b/metricq/cli/params.py @@ -189,3 +189,28 @@ def convert( if not isinstance(value, str): raise TypeError("expected a string type for TemplateStringParam") return Template(value).safe_substitute(self.mapping) + + +class MetricInputParam(ParamType): + pattern = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+\.)+[a-zA-Z][a-zA-Z0-9_]+") + + def convert( + self, value: Any, param: Optional[Parameter], ctx: Optional[Context] + ) -> str: + if not isinstance(value, str): + raise TypeError("expected a string type for the metric input") + if not self.pattern.match(value): + raise ValueError(f"Invalid metric format: '{value}'.") + return value + + +def metric_input(default: Optional[str] = None) -> Callable[[FC], FC]: + return option( + "--metric", + type=MetricInputParam(), + metavar="METRIC", + show_default=True, + required=default is None, + default=default, + help="Use the -–metric parameter to specify which metric the program should use", + ) diff --git a/metricq/cli/wrapper.py b/metricq/cli/wrapper.py index f6a773b6..ba3a0fa8 100644 --- a/metricq/cli/wrapper.py +++ b/metricq/cli/wrapper.py @@ -1,7 +1,5 @@ import logging -import re -from functools import wraps -from typing import Callable, Optional, cast +from typing import Callable, cast import click import click_log # type: ignore @@ -85,26 +83,3 @@ def decorator(func: FC) -> click.Command: ) return decorator - - -def metric_input( - required: bool = True, default: Optional[str] = None -) -> Callable[[FC], FC]: - valid_metric_regex = r"([a-zA-Z][a-zA-Z0-9_]+\.)+[a-zA-Z][a-zA-Z0-9_]+" - - def decorator(func): # type: ignore - @click.option("--metric", default=default, help="Metric input") - @wraps(func) - def wrapper(*args, metric, **kwargs): # type: ignore - if metric is not None: - if not re.match(valid_metric_regex, metric): - raise ValueError(f"Invalid metric format: {metric}") - - if metric is None and required: - raise Exception("Input metric is missing.") - - return func(*args, metric=metric, **kwargs) - - return wrapper - - return decorator