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

Warning

THIS MODULE DOES NOT HAVE STABLE PUBLIC API

class plainbox.impl.clitools.CommandBase[source]

Bases: object

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_arguments(parser)[source]

Implement to customize which arguments need to be added to a parser.

This method differs from register_parser() in that it allows commands which implement it to be invoked directly from a tool class (without being a subcommand that needs to be selected). If implemented it should be used from within register_parser() to ensure identical behavior in both cases (subcommand and tool-level command)

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

Bases: object

Mix-in class for ToolBase that improves responsiveness by loading subcommands lazily on demand and using some heuristic that works well in the common case of running one command.

Unlike the original, this implementation uses a custom version of add_subcommands() which uses the early_ns argument as a hint to not load or register commands that are not going to be needed.

In practice tool --help doesn’t benefit much but tool <cmd> can now be much, much faster (and more responsive) as it only loads that one command.

Concrete subclasses must implement the get_command_collection() method which must return a IPlugInCollection (ideally the LazyPlugInCollection that contains extra optimizations for low-cost key enumeration and one-at-a-time value loading).

add_subcommands()[source]

Add top-level subcommands to the argument parser.

Parameters:
  • subparsers – A part of argparse that can be used to create additional parsers for specific subcommands.
  • early_ns – (optional) An argparse namespace from earlier parsing. If it is not None, it must have the rest attribute which is used as a list of hints.

Note

This method is customized by LazyLoadingToolMixIn and should not be overriden directly. To register your commands use get_command_collection()

add_subcommands_with_hints()[source]

Add top-level subcommands to the argument parser, using a list of hints.

Parameters:
  • subparsers – A part of argparse that can be used to create additional parsers for specific subcommands.
  • hint_list – A list of strings that should be used as hints.

This method tries to optimize the time needed to register and setup all of the subcommands by looking at a list of hints in search for the (likely) command that will be executed.

Things that look like options are ignored. The first element of hint_list that matches a known command name, as provided by meth:get_command_collection(), is used as a sign that that command will be executed and all other commands don’t have to be loaded or initialized. If no hints are found (e.g. when running tool --help) the slower fallback mode is used and all subcommands are added.

Note

This method is customized by LazyLoadingToolMixIn and should not be overriden directly. To register your commands use get_command_collection()

add_subcommands_without_hints(subparsers: argparse._SubParsersAction, command_collection: plainbox.impl.secure.plugins.IPlugInCollection) → None[source]

Add top-level subcommands to the argument parser (fallback mode)

Parameters:
  • subparsers – A part of argparse that can be used to create additional parsers for specific subcommands.
  • command_collection – A collection of commands that was obtaioned from get_command_collection() earlier.

This method is called when hint-based optimization cannot be used and all commands need to be loaded and initialized.

Note

This method is customized by LazyLoadingToolMixIn and should not be overriden directly. To register your commands use get_command_collection()

get_command_collection() → plainbox.impl.secure.plugins.IPlugInCollection[source]

Get a (lazy) collection of all subcommands.

This method returns a IPlugInCollection that maps command name to CommandBase subclass, such as PlainBoxCommand.

The name of each plug in object must match the command name.

class plainbox.impl.clitools.SingleCommandToolMixIn[source]

Bases: object

Mix-in class for ToolBase to implement single-command dispatch.

This effectively turns the tool into a single-command tool. The only method that needs to be implemented is the get_command() method.

add_subcommands(subparsers, early_ns)[source]

Overridden version of add_subcommands()

This method does nothing. It is here because ToolBase requires it.

construct_parser(early_ns=None)[source]

Overridden version of construct_parser()

This method sets the single subcommand as default. This allows the whole tool to be started without arguments and do the right thing while still supporting optional sub-commands and true (and rich) built-in help.

customize_parser(parser)[source]
get_command()[source]

Get the command to register

The return value must be a CommandBase instance that implements the CommandBase.register_arguments() method.

class plainbox.impl.clitools.ToolBase[source]

Bases: object

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, early_ns)[source]

Add top-level subcommands to the argument parser.

Parameters:
  • subparsers – The argparse subparsers object. Use it to register additional command line syntax parsers and to add your commands there.
  • early_ns – A namespace from parsing by the special early parser. The early parser may be used to quickly guess the command that needs to be loaded, despite not really being able to parse everything the full parser can. Using this as a hint one can optimize the command loading process to skip loading commands that would not be executed.

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(early_ns=None)[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.

enable_argcomplete_if_possible(parser)[source]
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
comments powered by Disqus