plainbox.impl.clitools – support code for command line utilities

Warning

THIS MODULE DOES NOT HAVE STABLE PUBLIC API

class plainbox.impl.clitools.CommandBase[source]

Simple interface class for sub-commands of ToolBase.

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

Subcommands need to be registered inside the register_parser(), either manually by calling add_parser() on the passed subparsers instance, or by calling the helper add_subcommand() method. By common convention each subclass of CommandBase adds exactly one subcommand to the parser.

add_subcommand(subparsers)[source]

Add a parser to the specified subparsers instance.

Returns:The new parser for the added subcommand

This command works by convention, depending on get_command_name(), :meth:`get_command_help(), get_command_description() and get_command_epilog().

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.

get_command_description()[source]

Get a multi-line description string associated with this command, as seen on command line.

The description is printed after command usage but before argument and option definitions.

Returns:self.description, if defined
Returns:A substring of the class docstring between the first line (which goes to get_command_help()) and the string @EPILOG@, if present, or the end of the docstring, if any.
Returns:None, otherwise
get_command_epilog()[source]

Get a multi-line description string associated with this command, as seen on command line.

The epilog is printed after the definitions of arguments and options

Returns:self.epilog, if defined
Returns:A substring of the class docstring between the string @EPILOG and the end of the docstring, if defined
Returns:None, otherwise
get_command_help()[source]

Get a single-line help string associated with this command, as seen on command line.

Returns:self.help, if defined
Returns:The first line of the docstring of this class, if any
Returns:None, otherwise
get_command_name()[source]

Get the name of the command, as seen on command line.

Returns:self.name, if defined
Returns:lower-cased class name, with the string “command” stripped out
get_gettext_domain()[source]

Get the gettext translation domain associated with this command.

The domain will be used to translate the description, epilog and help string, as obtained by their respective methods.

Returns:self.gettext_domain, if defined
Returns:None, otherwise. Note that it will cause the string to be translated with the globally configured domain.
get_localized_docstring()[source]

Get a cleaned-up, localized copy of docstring of this class.

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.clitools.ToolBase[source]

Base class for implementing programs with hierarchical subcommands

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_exec_name() – to know how the tool will be called
  2. get_exec_version() – to know how the version of the tool
  3. add_subcommands() – to add some actual commands to execute

This class has some complex control flow to support important and interesting use cases. It is important to know 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.

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

Add top-level subcommands to the argument parser.

This can be overridden 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]
create_parser_object()[source]

Construct a bare parser object.

This method is responsible for creating the main parser object and adding –version and other basic top-level properties to it (but not any of the commands).

It exists as a separate method in case some special customization is required, so that subclasses can still use standard version of construct_parser().

Returns:argparse.ArgumentParser instance.
dispatch_and_catch_exceptions(ns)[source]
dispatch_command(ns)[source]
early_init()[source]

Do very early initialization. This is where we initialize 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 format_version_tuple(version_tuple)[source]
classmethod get_exec_name()[source]

Get the name of this executable

classmethod get_exec_version()[source]

Get the version reported by this executable

get_gettext_domain()[source]

Get the name of the gettext domain that should be used by this tool.

The value returned will be used to select translations to global calls to gettext() and ngettext() everywhere in python.

get_locale_dir()[source]

Get the path of the gettext translation catalogs for this tool.

This value is used to bind the domain returned by get_gettext_domain() to a specific directory. By default None is returned, which means that standard, system-wide locations are used.

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

setup_i18n()[source]

Setup i18n and l10n system.

plainbox.impl.clitools.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 variable. 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.clitools.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

<no title>

Next topic

plainbox.impl.color – ANSI color codes

This Page