command.helper

Module with convenient function for defining user commands.

lmi.scripts.common.command.helper.make_list_command(func, name=None, columns=None, verify_func=None, transform_func=None)[source]

Create a command subclassed from LmiLister. Please refer to this class for detailed usage.

Parameters:
  • func (string or callable) – Contents of CALLABLE property.
  • name (string) – Optional name of resulting class. If not given, it will be made from the name of associated function.
  • columns (tuple) – Contents of COLUMNS property.
  • verify_func (callable) – Callable overriding py:meth:~.endpoint.LmiEndPointCommand.verify_options method.
  • transform_func (callable) – Callable overriding transform_options() method.
Returns:

Subclass of LmiLister.

Return type:

type

lmi.scripts.common.command.helper.register_subcommands(command_name, usage, command_map, fallback_command=None)[source]

Create a multiplexer command (a node in a tree of commands).

Parameters:
  • command_name (string) – Name of created command. The same as will be given on a command line.
  • usage (string) – Usage string parseable by docopt.
  • command_map (dictionary) – Dictionary of subcommands. Associates command names to their factories. It’s assigned to COMMANDS property.
  • fallback_command (LmiEndPointCommand) – Command factory used when no command is given on command line.
Returns:

Subclass of LmiCommandMultiplexer.

Return type:

type

lmi.scripts.common.command.helper.select_command(command_name, *args, **kwargs)[source]

Create command selector that loads command whose requirements are met.

Example of invocation:

Hardware = select_command('Hardware',
    ("Openlmi-Hardware >= 0.4.2", "lmi.scripts.hardware.current.Cmd"),
    ("Openlmi-Hardware < 0.4.2" , "lmi.scripts.hardware.pre042.Cmd"),
    default=HwMissing
)

Above example checks remote broker for OpenLMI-Hardware provider. If it is installed and its version is equal or higher than 0.4.2, command from current module will be used. For older registered versions command contained in pre042 module will be loaded. If hardware provider is not available, HwMissing command will be loaded instead.

See also

Check out the grammer describing language used in these conditions at lmi.scripts.common.versioncheck.parser.

Parameters:
  • args – List of pairs (condition, command) that are inspected in given order until single condition is satisfied. Associated command is then loaded. Command is either a reference to command class or path to it given as string. In latter case last dot divides module’s import path and command name.
  • default – This command will be loaded when no condition from args is satisfied.