From 1561353b20f77f2dfe301c6d1addfd025608fe82 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Thu, 20 Jan 2022 06:30:35 +0100 Subject: [PATCH] feat(shared): allow cli parametrized decorators (#1527) --- analyzer/libretime_analyzer/main.py | 4 +- playout/libretime_liquidsoap/main.py | 2 +- playout/libretime_playout/main.py | 2 +- playout/libretime_playout/notify/main.py | 2 +- shared/README.md | 4 +- shared/libretime_shared/cli.py | 92 +++++++++++++----------- 6 files changed, 58 insertions(+), 48 deletions(-) diff --git a/analyzer/libretime_analyzer/main.py b/analyzer/libretime_analyzer/main.py index b9ed29774..68db2fde1 100644 --- a/analyzer/libretime_analyzer/main.py +++ b/analyzer/libretime_analyzer/main.py @@ -17,8 +17,8 @@ DEFAULT_RETRY_QUEUE_FILEPATH = Path("retry_queue") @click.command() -@cli_logging_options -@cli_config_options +@cli_logging_options() +@cli_config_options() @click.option( "--retry-queue-filepath", envvar=f"{DEFAULT_ENV_PREFIX}_RETRY_QUEUE_FILEPATH", diff --git a/playout/libretime_liquidsoap/main.py b/playout/libretime_liquidsoap/main.py index 03a390f5b..f5c40fce3 100644 --- a/playout/libretime_liquidsoap/main.py +++ b/playout/libretime_liquidsoap/main.py @@ -16,7 +16,7 @@ PYPO_HOME = "/var/tmp/airtime/pypo/" @click.command() -@cli_logging_options +@cli_logging_options() def cli(log_level: int, log_filepath: Optional[Path]): """ Run liquidsoap. diff --git a/playout/libretime_playout/main.py b/playout/libretime_playout/main.py index 2f4ffcb73..e46b684ee 100644 --- a/playout/libretime_playout/main.py +++ b/playout/libretime_playout/main.py @@ -110,7 +110,7 @@ def liquidsoap_startup_test(telnet_lock, ls_host, ls_port): @click.command() -@cli_logging_options +@cli_logging_options() def cli(log_level: str, log_filepath: Optional[Path]): """ Run playout. diff --git a/playout/libretime_playout/notify/main.py b/playout/libretime_playout/notify/main.py index af5fb472d..294be5d81 100644 --- a/playout/libretime_playout/notify/main.py +++ b/playout/libretime_playout/notify/main.py @@ -27,7 +27,7 @@ def api_client(): @click.group() -@cli_logging_options +@cli_logging_options() def cli(log_level: str, log_filepath: Optional[Path]): """ A gateway between Liquidsoap and the API. diff --git a/shared/README.md b/shared/README.md index bc7f60154..7ebd53333 100644 --- a/shared/README.md +++ b/shared/README.md @@ -69,8 +69,8 @@ def cli(): pass @cli.command() -@cli_config_options -@cli_logging_options +@cli_config_options() +@cli_logging_options() def run(**kwargs): app = App(**kwargs) return app.run() diff --git a/shared/libretime_shared/cli.py b/shared/libretime_shared/cli.py index 09ce35cfd..acc572040 100644 --- a/shared/libretime_shared/cli.py +++ b/shared/libretime_shared/cli.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Callable +from typing import Any, Callable, Optional import click @@ -7,51 +7,61 @@ from .config import DEFAULT_ENV_PREFIX from .logging import INFO, LOG_LEVEL_MAP -def cli_logging_options(func: Callable) -> Callable: - """ - Decorator function to add logging options to a click application. +def cli_logging_options() -> Callable: + def decorator(func: Callable) -> Callable: + """ + Decorator function to add logging options to a click application. - This decorator add the following arguments: - - log_level: str - - log_filepath: Optional[Path] - """ - func = click.option( - "--log-level", - "log_level", - envvar=f"{DEFAULT_ENV_PREFIX}_LOG_LEVEL", - type=click.Choice(list(LOG_LEVEL_MAP.keys())), - default=INFO.name, - help="Name of the logging level.", - )(func) + This decorator add the following arguments: + - log_level: str + - log_filepath: Optional[Path] + """ + func = click.option( + "--log-level", + "log_level", + envvar=f"{DEFAULT_ENV_PREFIX}_LOG_LEVEL", + type=click.Choice(list(LOG_LEVEL_MAP.keys())), + default=INFO.name, + help="Name of the logging level.", + )(func) - func = click.option( - "--log-filepath", - "log_filepath", - envvar=f"{DEFAULT_ENV_PREFIX}_LOG_FILEPATH", - type=click.Path(path_type=Path), - help="Path to the logging file.", - default=None, - )(func) + func = click.option( + "--log-filepath", + "log_filepath", + envvar=f"{DEFAULT_ENV_PREFIX}_LOG_FILEPATH", + type=click.Path(path_type=Path), + help="Path to the logging file.", + default=None, + )(func) - return func + return func + + return decorator -def cli_config_options(func: Callable) -> Callable: - """ - Decorator function to add config file options to a click application. +def cli_config_options( + required: bool = False, + default: Optional[Any] = None, +) -> Callable: + def decorator(func: Callable) -> Callable: + """ + Decorator function to add config file options to a click application. - This decorator add the following arguments: - - config_filepath: Optional[Path] - """ + This decorator add the following arguments: + - config_filepath: Optional[Path] or Path + """ - func = click.option( - "--c", - "--config", - "config_filepath", - envvar=f"{DEFAULT_ENV_PREFIX}_CONFIG_FILEPATH", - type=click.Path(path_type=Path), - help="Path to the config file.", - default=None, - )(func) + func = click.option( + "--c", + "--config", + "config_filepath", + envvar=f"{DEFAULT_ENV_PREFIX}_CONFIG_FILEPATH", + type=click.Path(path_type=Path), + help="Path to the config file.", + required=required, + default=default, + )(func) - return func + return func + + return decorator