zabgrab.cli.app

Shared CLI state and central error handling.

zabgrab.cli.app.pass_app

Decorator passing the AppContext to a command.

Type:

callable

zabgrab.cli.app.GLOBAL_OPTIONS

The options every command accepts (see global_options()).

Type:

list

Functions

global_options(function)

Add the global options to a command or group.

http_client(config)

Create a client that talks to a real server over HTTP(S).

Classes

AppContext()

State shared by all commands: the click context object.

GlobalOptions([verbosity, debug, color, ...])

Options accepted by every command (see global_options()).

ZabgrabGroup([name, commands, ...])

Command group that turns exceptions into user-friendly messages.

zabgrab.cli.app.http_client(config)[source]

Create a client that talks to a real server over HTTP(S).

Parameters:

config (ServerConfig) – The server’s URL and connection settings.

Returns:

ZabbixClient – The client, not yet logged in.

Return type:

ZabbixClient

class zabgrab.cli.app.GlobalOptions(verbosity=0, debug=False, color=None, config_path=None, isolated=False, no_input=False)[source]

Bases: object

Options accepted by every command (see global_options()).

Parameters:
verbosity

The count of -v minus the count of -q.

Type:

int

debug

--debug.

Type:

bool

color

--color or --no-color, if given.

Type:

bool or None

config_path

--config.

Type:

Path or None

isolated

--isolated.

Type:

bool

no_input

--no-input.

Type:

bool

class zabgrab.cli.app.AppContext[source]

Bases: object

State shared by all commands: the click context object.

Global options update options while the command line is parsed, at whichever level they appear; everything else is built on first use. Tests replace the adapters (secrets, client factory, clock, …) with fakes.

options

The global options.

Type:

GlobalOptions

env

The environment variables and platform paths.

Type:

Environment

editor

Opens config files for editing.

Type:

Editor

secrets

Stores API tokens.

Type:

SecretStore

client_factory

Creates Zabbix clients.

Type:

ClientFactory

clock

The current time.

Type:

Clock

property ui: RichUI[source]

The terminal UI, configured by the global options.

layers()[source]

Find the config layers, including those without a file.

Returns:

list of ConfigLayer – The layers, lowest priority first.

Return type:

list[ConfigLayer]

config()[source]

Get the merged config, loaded on first use.

Interactive sessions are offered the chance to fix invalid files.

Returns:

LoadedConfig – The config and the layers it came from.

Raises:

ZabgrabError – If a config file is invalid and isn’t fixed.

Return type:

LoadedConfig

select_server(configured)[source]

Choose the server to use.

See zabgrab.services.servers.select_server(). Custom servers entered at a prompt can be saved to the user config.

Parameters:

configured (str or Choice of str or None) – The configured server, choice of servers, or None to ask.

Returns:

Server – The server.

Return type:

Server

select_servers(cli, configured)[source]

Choose the servers to search.

See zabgrab.services.servers.select_servers().

Parameters:
  • cli (sequence of str) – The --server options given.

  • configured (str or Choice of str or None) – The configured server, used if there are none.

Returns:

list of Server – The servers.

Return type:

list[Server]

save_server(name, url)[source]

Add a server to the user config file.

Parameters:
  • name (str) – The server’s name.

  • url (str) – Its URL.

Return type:

None

update_server_url(name, url)[source]

Change a configured server’s URL in the config file that defines it.

Parameters:
  • name (str) – The server’s name.

  • url (str) – Its new URL.

Return type:

None

async connect(server)[source]

Connect and log in to a server.

Parameters:

server (Server) – The server.

Returns:

ZabbixClient – The logged-in client, to be closed after use.

Raises:

ZabgrabError – If the server can’t be reached or logging in fails.

Return type:

ZabbixClient

zabgrab.cli.app.global_options(function)[source]

Add the global options to a command or group.

Every command gets them, so they can be given at any level, e.g. both zabgrab -v hosts and zabgrab hosts -v.

Parameters:

function (callable) – The command or group’s function.

Returns:

callable – The function, with the options added.

Return type:

F

class zabgrab.cli.app.ZabgrabGroup(name=None, commands=None, invoke_without_command=False, no_args_is_help=None, subcommand_metavar=None, chain=False, result_callback=None, **kwargs)[source]

Bases: Group

Command group that turns exceptions into user-friendly messages.

Parameters:
  • name (str | None)

  • commands (MutableMapping[str, Command])

  • invoke_without_command (bool)

  • no_args_is_help (bool)

  • subcommand_metavar (str)

  • chain (bool)

  • result_callback (t.Callable[..., t.Any] | None)

  • kwargs (t.Any)

invoke(ctx)[source]

Run the command, reporting errors and exiting with their exit codes.

Parameters:

ctx (click.Context) – The group’s context.

Returns:

Any – What the command returns.

Return type:

Any