command.base

Module defining base command class for all possible commands of lmi meta-command.

lmi.scripts.common.command.base.DEFAULT_FORMATTER_OPTIONS = {'padding': 0, 'human_friendly': False, 'no_headings': False}

Default formatting options overriden by options passed onc ommand-line and set in configuration file.

class lmi.scripts.common.command.base.LmiBaseCommand(app, cmd_name, parent=None)[source]

Abstract base class for all commands handling command line arguments. Instances of this class are organized in a tree with root element being the lmi meta-command (if not running in interactive mode). Each such instance can have more child commands if its LmiBaseCommand.is_multiplexer() method return True. Each has one parent command except for the top level one, whose parent property returns None.

Set of commands is organized in a tree, where each command (except for the root) has its own parent. is_end_point() method distinguishes leaves from nodes. The path from root command to the leaf is a sequence of commands passed to command line.

There is also a special command called selector. Its is_selector() method returns True. It selects proper command that shall be passed all the arguments based on expression with profile requirements. It shares its name and parent with selected child.

If the LmiBaseCommand.has_own_usage() returns True, the parent command won’t process the whole command line and the remainder will be passed as a second argument to the LmiBaseCommand.run() method.

Parameters:
  • app – Main application object.
  • cmd_name (string) – Name of command.
  • parent (LmiBaseCommand) – Parent command.
app[source]

Return application object.

classmethod child_commands()[source]

Abstract class method returning dictionary of child commands with structure:

{ "command-name" : cmd_factory, ... }

Dictionary contains just a direct children (commands, which may immediately follow this particular command on command line).

cmd_name[source]

Name of this subcommand as a single word.

cmd_name_parts[source]

Convenience property calling get_cmd_name_parts() to obtain command path as a list of all preceding command names.

Return type:list
format_options[source]

Compose formatting options. Parent commands are queried for defaults. If command has no parent, default options will be taken from DEFAULT_FORMATTER_OPTIONS which are overriden by config settings.

Returns:Arguments passed to formatter factory when formatter is for current command is constructed.
Return type:dictionary
get_cmd_name_parts(all_parts=False, demand_own_usage=True, for_docopt=False)[source]

Get name of this command as a list composed of names of all preceding commands since the top level one. When in interactive mode, only commands following the active one will be present.

Parameters:
  • full (boolean) – Take no heed to the active command or interactive mode. Return all command names since top level node inclusive. This is overriden with for_docopt flag.
  • demand_own_usage (boolean) – Wether to continue the upward traversal through command hieararchy past the active command until the command with its own usage is found. This is the default behaviour.
  • for_docopt (boolean) – Docopt parser needs to be given arguments list without the first item compared to command names in usage string it receives. Thus this option causes skipping the first item that would be otherwise included.
Returns:

Command path. Returned list will always contain at least the name of this command.

Return type:

list

classmethod get_description()[source]

Return description for this command. This is usually a first line of documentation string of a class.

Return type:string
get_usage(proper=False)[source]

Get command usage. Return value of this function is used by docopt parser as usage string. Command tree is traversed upwards until command with defined usage string is found. End point commands (leaves) require manually written usage, so the first command in the sequence of parents with own usage string is obtained and its usage returned. For nodes missing own usage string this can be generated based on its subcommands.

Parameters:proper (boolean) – Says, whether the usage string written manually is required or not. It applies only to node (not a leaf) commands without its own usage string.
classmethod has_own_usage()[source]
Returns:True, if this command has its own usage string, which is returned by LmiBaseCommand.get_description(). Otherwise the parent command must be queried.
Return type:boolean
classmethod is_end_point()[source]
Returns:True, if this command parses the rest of command line and can not have any child subcommands.
Return type:boolean
classmethod is_multiplexer()[source]

Is this command a multiplexer? Note that only one of is_end_point(), is_selector() and this method can evaluate to``True``.

Returns:True if this command is not an end-point command and it’s a multiplexer. It contains one or more subcommands. It consumes the first argument from command-line arguments and passes the rest to one of its subcommands.
Return type:boolean
classmethod is_selector()[source]

Is this command a selector?

Returns:True if this command is a subclass of lmi.scripts.common.command.select.LmiSelectCommand.
Return type:boolean
parent[source]

Return parent command.

run(args)[source]

Handle the command line arguments. If this is not an end point command, it will pass the unhandled arguments to one of it’s child commands. So the arguments are processed recursively by the instances of this class.

Parameters:args (list) – Arguments passed to the command line that were not yet parsed. It’s the contents of sys.argv (if in non-interactive mode) from the current command on.
Returns:Exit code of application. This maybe also be a boolean value or None. None and True are treated as a success causing exit code to be 0.
Return type:integer
session[source]
Returns:Session object. Session for command and all of its children may be overriden with a call to set_session_proxy().
Return type:lmi.scripts.common.session.Session
set_session_proxy(session)[source]

Allows to override session object. This is useful for especially for conditional commands (subclasses of LmiSelectCommand) that devide connections to groups satisfying particular expression. These groups are turned into session proxies containing just a subset of connections in global session object.

Parameters:session – Session object.