From c895f8695d8bd7daa4de1b7e27218ec9345f62e4 Mon Sep 17 00:00:00 2001 From: Max Kutschka <2004maximilian@web.de> Date: Thu, 17 Oct 2024 13:28:49 +0200 Subject: [PATCH] implementing the new changes to the examples --- examples/metricq_client.py | 8 -------- examples/metricq_get_history.py | 18 ++++-------------- examples/metricq_get_history_raw.py | 16 ++-------------- examples/metricq_pandas.py | 16 +++------------- examples/metricq_sink.py | 9 --------- examples/metricq_source.py | 10 ---------- examples/metricq_synchronous_source.py | 17 ++++------------- metricq/cli/__init__.py | 12 +++++++++--- metricq/cli/params.py | 21 ++++----------------- metricq/cli/wrapper.py | 5 +++-- 10 files changed, 29 insertions(+), 103 deletions(-) diff --git a/examples/metricq_client.py b/examples/metricq_client.py index 579be272..7108078b 100755 --- a/examples/metricq_client.py +++ b/examples/metricq_client.py @@ -40,20 +40,13 @@ `telnet localhost 50101` (or `netcat`), inspect tasks and run code in a REPL. """ import asyncio -import logging import aiomonitor # type: ignore -import click_log # type: ignore import metricq from metricq.cli import metricq_command logger = metricq.get_logger() -click_log.basic_config(logger) -logger.setLevel("INFO") -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) async def run(server: str, token: str) -> None: @@ -67,7 +60,6 @@ async def run(server: str, token: str) -> None: @metricq_command(default_token="client-py-example") -@click_log.simple_verbosity_option(logger) # type: ignore def main(server: str, token: str) -> None: asyncio.run(run(server, token)) diff --git a/examples/metricq_get_history.py b/examples/metricq_get_history.py index ee5fbcb4..71e21310 100755 --- a/examples/metricq_get_history.py +++ b/examples/metricq_get_history.py @@ -28,26 +28,17 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import asyncio -import logging import pprint from datetime import timedelta import click -import click_log # type: ignore import metricq from metricq.cli import metricq_command -from metricq.cli.params import metric_input +from metricq.cli.wrapper import metricq_metric_option +from metricq.logging import get_logger -logger = metricq.get_logger() - -click_log.basic_config(logger) -logger.setLevel("INFO") -# Use this if we ever use threads -# logger.handlers[0].formatter = logging.Formatter(fmt='%(asctime)s %(threadName)-16s %(levelname)-8s %(message)s') -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) +logger = get_logger() async def aget_history( @@ -101,10 +92,9 @@ async def aget_history( @metricq_command(default_token="history-py-dummy") -@metric_input() +@metricq_metric_option() @click.option("--list-metrics", is_flag=True) @click.option("--list-metadata", is_flag=True) -@click_log.simple_verbosity_option(logger) # type: ignore def get_history( server: str, token: str, metric: str, list_metrics: bool, list_metadata: bool ) -> None: diff --git a/examples/metricq_get_history_raw.py b/examples/metricq_get_history_raw.py index 7167daa2..d4372b10 100755 --- a/examples/metricq_get_history_raw.py +++ b/examples/metricq_get_history_raw.py @@ -28,26 +28,15 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import asyncio -import logging from datetime import timedelta import click -import click_log # type: ignore import metricq from metricq.cli import metricq_command +from metricq.cli.wrapper import metricq_metric_option from metricq.history_client import HistoryRequestType -logger = metricq.get_logger() - -click_log.basic_config(logger) -logger.setLevel("INFO") -# Use this if we ever use threads -# logger.handlers[0].formatter = logging.Formatter(fmt='%(asctime)s %(threadName)-16s %(levelname)-8s %(message)s') -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) - async def aget_history(server: str, token: str, metric: str) -> None: client = metricq.HistoryClient(token=token, url=server) @@ -85,8 +74,7 @@ async def aget_history(server: str, token: str, metric: str) -> None: @metricq_command(default_token="history-py-dummy") -@click.argument("metric") -@click_log.simple_verbosity_option(logger) # type: ignore +@metricq_metric_option() def get_history(server: str, token: str, metric: str) -> None: asyncio.run(aget_history(server, token, metric)) diff --git a/examples/metricq_pandas.py b/examples/metricq_pandas.py index 26a1cf5f..05eec51e 100755 --- a/examples/metricq_pandas.py +++ b/examples/metricq_pandas.py @@ -28,26 +28,17 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import asyncio -import logging from datetime import timedelta import click -import click_log # type: ignore import metricq -from metricq.cli import metric_input, metricq_command +from metricq.cli import metricq_command +from metricq.cli.wrapper import metricq_metric_option from metricq.pandas import PandasHistoryClient logger = metricq.get_logger() -click_log.basic_config(logger) -logger.setLevel("INFO") -# Use this if we ever use threads -# logger.handlers[0].formatter = logging.Formatter(fmt='%(asctime)s %(threadName)-16s %(levelname)-8s %(message)s') -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) - async def aget_history(server: str, token: str, metric: str) -> None: async with PandasHistoryClient(token=token, url=server) as client: @@ -83,8 +74,7 @@ async def aget_history(server: str, token: str, metric: str) -> None: @metricq_command(default_token="history-py-dummy") -@metric_input(default="example.quantity") -@click_log.simple_verbosity_option(logger) # type: ignore +@metricq_metric_option(default="example.quantity") def get_history(server: str, token: str, metric: str) -> None: asyncio.run(aget_history(server, token, metric)) diff --git a/examples/metricq_sink.py b/examples/metricq_sink.py index a1a2f8f0..847e3950 100755 --- a/examples/metricq_sink.py +++ b/examples/metricq_sink.py @@ -27,11 +27,9 @@ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import logging from typing import Any import click -import click_log # type: ignore import metricq from metricq import Metric @@ -40,12 +38,6 @@ logger = get_logger() -click_log.basic_config(logger) -logger.setLevel("INFO") -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) - # To implement a MetricQ Sink, subclass metricq.Sink class DummySink(metricq.Sink): @@ -79,7 +71,6 @@ async def on_data( @metricq_command(default_token="sink-py-dummy") @click.option("-m", "--metrics", multiple=True, required=True) -@click_log.simple_verbosity_option(logger) # type: ignore def source(server: str, token: str, metrics: list[Metric]) -> None: # Initialize the DummySink class with a list of metrics given on the # command line. diff --git a/examples/metricq_source.py b/examples/metricq_source.py index c7a189fc..edd9e635 100755 --- a/examples/metricq_source.py +++ b/examples/metricq_source.py @@ -27,24 +27,15 @@ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import logging import random from typing import Any -import click_log # type: ignore - import metricq from metricq.cli import metricq_command from metricq.logging import get_logger logger = get_logger() -click_log.basic_config(logger) -logger.setLevel("INFO") -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) - class DummySource(metricq.IntervalSource): def __init__(self, *args: Any, **kwargs: Any): @@ -75,7 +66,6 @@ async def update(self) -> None: @metricq_command(default_token="source-py-dummy") -@click_log.simple_verbosity_option(logger) # type: ignore def source(server: str, token: str) -> None: src = DummySource(token=token, url=server) src.run() diff --git a/examples/metricq_synchronous_source.py b/examples/metricq_synchronous_source.py index 715cc763..32b8c7eb 100755 --- a/examples/metricq_synchronous_source.py +++ b/examples/metricq_synchronous_source.py @@ -29,27 +29,18 @@ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import logging + import random import time -import click_log # type: ignore - -from metricq import SynchronousSource, Timestamp, get_logger +from metricq import SynchronousSource, Timestamp from metricq.cli import metricq_command - -logger = get_logger() - -click_log.basic_config(logger) -logger.setLevel("INFO") -logger.handlers[0].formatter = logging.Formatter( - fmt="%(asctime)s [%(levelname)-8s] [%(name)-20s] %(message)s" -) +from metricq.logging import get_logger @metricq_command(default_token="source-py-dummy") -@click_log.simple_verbosity_option(logger) # type: ignore def synchronous_source(server: str, token: str) -> None: + logger = get_logger() ssource = SynchronousSource(token=token, url=server) ssource.declare_metrics( { diff --git a/metricq/cli/__init__.py b/metricq/cli/__init__.py index e3777b26..c393106e 100644 --- a/metricq/cli/__init__.py +++ b/metricq/cli/__init__.py @@ -4,9 +4,13 @@ DurationParam, TemplateStringParam, TimestampParam, - metric_input, ) -from .wrapper import metricq_command +from .wrapper import ( + metricq_command, + metricq_metric_option, + metricq_server_option, + metricq_token_option, +) __all__ = [ "ChoiceParam", @@ -15,5 +19,7 @@ "TemplateStringParam", "TimestampParam", "metricq_command", - "metric_input", + "metricq_metric_option", + "metricq_server_option", + "metricq_token_option", ] diff --git a/metricq/cli/params.py b/metricq/cli/params.py index 92bbd8a1..766acbfb 100644 --- a/metricq/cli/params.py +++ b/metricq/cli/params.py @@ -3,12 +3,11 @@ from getpass import getuser from socket import gethostname from string import Template -from typing import Any, Generic, List, Optional, Type, TypeVar, Union, cast, Callable +from typing import Any, Generic, List, Optional, Type, TypeVar, Union, cast -from click import Context, Parameter, ParamType, option +from click import Context, Parameter, ParamType from ..timeseries import Timedelta, Timestamp -from .types import FC _C = TypeVar("_C", covariant=True) @@ -171,26 +170,14 @@ def convert( return Template(value).safe_substitute(self.mapping) -class MetricInputParam(ParamType): +class MetricParam(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") + raise TypeError("expected a string type for the MetricParam") 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 2db95839..31e9c30c 100644 --- a/metricq/cli/wrapper.py +++ b/metricq/cli/wrapper.py @@ -1,5 +1,5 @@ import logging -from typing import Callable, cast +from typing import Callable, Optional, cast import click import click_log # type: ignore @@ -7,7 +7,7 @@ from dotenv import find_dotenv, load_dotenv from .. import get_logger -from .params import TemplateStringParam +from .params import MetricParam, TemplateStringParam from .types import FC # We do not interpolate (i.e. replace ${VAR} with corresponding environment variables). @@ -53,6 +53,7 @@ def metricq_metric_option(default: Optional[str] = None) -> Callable[[FC], FC]: ) + def get_metric_command_logger() -> logging.Logger: logger = get_logger() logger.setLevel(logging.WARNING)