plainbox.impl.commands – shared code for plainbox sub-commands

Warning

THIS MODULE DOES NOT HAVE STABLE PUBLIC API

class plainbox.impl.commands.PlainBoxCommand[source]

Simple interface class for plainbox commands.

Command objects like this are consumed by PlainBoxTool subclasses to implement hierarchical command system. The API supports arbitrary many sub commands in arbitrary nesting arrangement.

autopager()[source]

Enable automatic pager.

This invokes autopager() which wraps execution in a pager program so that long output is not a problem to read. Do not call this in interactive commands.

invoked(ns)[source]

Implement what should happen when the command gets invoked

The ns is the namespace produced by argument parser

register_parser(subparsers)[source]

Implement what should happen to register the additional parser for this command. The subparsers argument is the return value of ArgumentParser.add_subparsers()

class plainbox.impl.commands.PlainBoxToolBase[source]

Base class for implementing commands like ‘plainbox’.

The tools support a variety of sub-commands, logging and debugging support. If argcomplete module is available and used properly in the shell then advanced tab-completion is also available.

There are three methods to implement for a basic tool. Those are:

  1. get_config_cls() – to know which config to use
  2. get_exec_name() – to know how the command will be called
  3. add_subcommands() – to add some actual commands to execute

This class has some complex control flow to support important and interesting use cases. There are some concerns to people that subclass this in order to implement their own command line tools.

The first concern is that input is parsed with two parsers, the early parser and the full parser. The early parser quickly checks for a fraction of supported arguments and uses that data to initialize environment before construction of a full parser is possible. The full parser sees the reminder of the input and does not re-parse things that where already handled.

The second concern is that this command natively supports the concept of a config object and a provider object. This may not be desired by all users but it is the current state as of this writing. This means that by the time eary init is done we have a known provider and config objects that can be used to instantiate command objects in add_subcommands(). This API might change when full multi-provider is available but details are not known yet.

add_early_parser_arguments(parser)[source]
add_subcommands(subparsers)[source]

Add top-level subcommands to the argument parser.

This can be overriden by subclasses to use a different set of top-level subcommands.

construct_early_parser()[source]

Create a parser that captures some of the early data we need to be able to have a real parser and initialize the rest.

construct_parser()[source]
dispatch_and_catch_exceptions(ns)[source]
dispatch_command(ns)[source]
early_init()[source]

Do very early initialization. This is where we initalize stuff even without seeing a shred of command line data or anything else.

final_init(ns)[source]

Do some final initialization just before the command gets dispatched. This is empty here but maybe useful for subclasses.

classmethod get_config_cls()[source]

Get the Config class that is used by this implementation.

This can be overriden by subclasses to use a different config class that is suitable for the particular application.

classmethod get_exec_name()[source]

Get the name of this executable

classmethod get_exec_version()[source]

Get the version reported by this executable

late_init(early_ns)[source]

Initialize with early command line arguments being already parsed

main(argv=None)[source]

Run as if invoked from command line directly

plainbox.impl.commands.autopager(pager_list=['sensible-pager', 'less', 'more'])[source]

Enable automatic pager

Parameters:pager_list – List of pager programs to try.
Returns:Nothing immedaitely if auto-pagerification cannot be turned on. This is true when running on windows or when sys.stdout is not a tty.

This function executes the following steps:

  • A pager is selected
  • A pipe is created
  • The current process forks
  • The parent uses execlp() and becomes the pager
  • The child/python carries on the execution of python code.
  • The parent/pager stdin is connected to the childs stdout.
  • The child/python stderr is connected to parent/pager stdin only when sys.stderr is connected to a tty

Note

Pager selection is influenced by the pager environment variabe. if set it will be prepended to the pager_list. This makes the expected behavior of allowing users to customize their environment work okay.

Warning

This function must not be used for interactive commands. Doing so will prevent users from feeding any input to plainbox as all input will be “stolen” by the pager process.

plainbox.impl.commands.find_exec(name_list)[source]

Find the first executable from name_list in PATH

Parameters:name_list – List of names of executable programs to look for, in the order of preference. Only basenames should be passed here (not absolute pathnames)
Returns:Tuple (name, pathname), if the executable can be found
Raises:LookupError if none of the names in name_list are executable programs in PATH

Previous topic

plainbox.impl.color – ANSI color codes

Next topic

plainbox.impl.commands.analyze – analyze sub-command

This Page