nadzoring.utils.decorators module

Common decorators for CLI commands.

nadzoring.utils.decorators._extract_cli_options(kwargs: dict[str, Any]) SimpleNamespace[source]

Extract CLI options from function keyword arguments.

This function removes CLI-specific options from the kwargs dictionary and returns them as a SimpleNamespace object.

Parameters:

kwargs – Dictionary of keyword arguments passed to the wrapped function.

Returns:

SimpleNamespace containing extracted CLI options with default values for any missing options.

nadzoring.utils.decorators._filter_func_kwargs(kwargs: dict[str, Any], cli_options: SimpleNamespace, *, include_verbose: bool, include_quiet: bool, include_no_color: bool, include_output: bool, include_save: bool) dict[str, Any][source]

Filter which CLI options are passed to the wrapped function.

Based on the include flags, this function selectively adds CLI options to the keyword arguments that will be passed to the wrapped function.

Parameters:
  • kwargs – Original keyword arguments dictionary.

  • cli_options – SimpleNamespace containing extracted CLI options.

  • include_verbose – Whether to include the verbose option.

  • include_quiet – Whether to include the quiet option.

  • include_no_color – Whether to include the no_color option.

  • include_output – Whether to include the output option.

  • include_save – Whether to include the save option.

Returns:

Filtered dictionary of keyword arguments for the wrapped function.

nadzoring.utils.decorators._handle_output(result: Any, output_format: str, *, no_color: bool) None[source]

Display command results in the requested format.

Parameters:
  • result – The result data from the command to display.

  • output_format – The output format to use (json, table, csv, html, html_table).

  • no_color – If True, disable colored output in table formatting.

Raises:

click.ClickException – If there’s an error processing the output format.

nadzoring.utils.decorators._handle_save(result: Any, save_path: str | None, output_format: str) None[source]

Save command results to a file if a save path is provided.

Parameters:
  • result – The result data from the command to save.

  • save_path – Path where the results should be saved, or None if not saving.

  • output_format – The output format to use for saving.

Raises:

click.ClickException – If there’s an error saving the file.

nadzoring.utils.decorators._setup_logging(cli_options: SimpleNamespace) None[source]

Configure logging based on CLI options.

Sets up logging with appropriate verbosity, quiet mode, and color settings.

Parameters:

cli_options – SimpleNamespace containing CLI options with verbose, quiet, and no_color attributes.

nadzoring.utils.decorators._show_completion_time(elapsed: float, *, verbose: bool) None[source]

Display command completion time in verbose mode.

Parameters:
  • elapsed – Time elapsed in seconds since command started.

  • verbose – If True, display the completion time.

nadzoring.utils.decorators.common_cli_options(*, include_verbose: bool = False, include_quiet: bool = False, include_no_color: bool = False, include_output: bool = False, include_save: bool = False) Callable[[F], F][source]

Decorator factory that adds common CLI options to click commands.

This decorator provides standardized CLI options across all commands, with selective inclusion based on the command’s needs. It handles: - Logging configuration (verbose/quiet modes) - Output format selection (table, json, csv, html) - Result saving to files - Performance timing in verbose mode

Parameters:
  • include_verbose – If True, pass verbose flag to the wrapped function. Defaults to False.

  • include_quiet – If True, pass quiet flag to the wrapped function. Defaults to False.

  • include_no_color – If True, pass no_color flag to the wrapped function. Defaults to False.

  • include_output – If True, pass output format to the wrapped function. Defaults to False.

  • include_save – If True, pass save path to the wrapped function. Defaults to False.

Returns:

A decorator function that wraps a click command with common options.

Example

@click.command() @common_cli_options(include_verbose=True) def my_command(verbose: bool) -> None:

‘’’My command implementation.’’’ if verbose:

click.echo(“Running in verbose mode”)