command
- Wrap complex commands in Python objects¶
Tools to wrap a Python API around interactive and non-interactive programs.
The command.Command
and interpreter.Interpreter
classes handle batch and
interactive commands respectively. They can be provided with option.Config
instances
which describe the options available to the programs being wrapped. The option.Parser
class can then be used to validate option sets and construct the corresponding command lines.
See the svn.svn
module for a concrete example.
command
- Drive batch commands with function calls¶
Non interactive command driver.
-
class
Command
(cmd, debug=False)[source]¶ Represents the command to be executed. Typically you would derive from this class and provide a different method for each alternative way of invoking the program. If the program you want to execute has many sub-commands you might provide a different method for each sub-command. You can use the
option.Config
class to declare the options supported by your command and then use theoption.Parser
class to validate your methods’ arguments and generate the resulting command line. A debug mode is available in which commands are echoed rather than run. This can be enabled globally or separately for each invocation.
error
- The command
package exception hierarchy¶
Exception classes for the nxpy.command package.
interpreter
- Wrap interactive programs in Python classes¶
Interactive program driver.
-
class
BaseInterpreter
(popen)[source]¶ Controls the execution of an interactive program in a sub-process. Provides means to send input to the controlled process and to check different conditions on its output and error streams.
-
__init__
(popen)[source]¶ Creates an interpreter instance. popen is a
Popen
-like object which must support non-blocking I/O.
-
expect
(cond=None, timeout=0, retries=0, interval=0.01, quantum=0.01, raise_on_error=True, log=None)[source]¶ Express expectations on the outcome of a command.
cond is a two argument callable which will be passed the command’s standard output and standard error, and which should return True if the expectation is satisfied. For the other arguments see the documentation for the
Timer
class.
-
expect_regexp
(regexp, where=0, **kwargs)[source]¶ Expect to find a match for the regexp regular expression within the where stream.
-
run
(cmd, log=None, **kwargs)[source]¶ Executes the command and waits for the expected outcome or an error.
-
-
class
Interpreter
(cmd)[source]¶ The actual Interpreter class.
This implementation uses a
core.async_subprocess.AsyncPopen
instance.
-
class
RegexpWaiter
(regexp, where)[source]¶ Wait for a match to a given regexp, passed either compiled or as a string.
-
class
Timer
(timeout=0, retries=0, interval=0.1, quantum=0.01)[source]¶ A collaborative timer class. Support a polling mechanism by keeping track of the amount of time to wait before the next attempt, according to different policies.
-
__init__
(timeout=0, retries=0, interval=0.1, quantum=0.01)[source]¶ Specify an overall timeout, a number of retries and/or an interval between them. The next attempt will not take place before a quantum has passed. Timings are expressed in seconds. If a timeout is specified it will take precedence over the other arguments; in that case the number of retries will take precedence over the interval. If neither a timeout nor a number of retries are specified the overall timer will never expire.
-
expired
()[source]¶ Indicate whether the current timer expired. Use as polling loop control condition.
-
option
- Describe complex command lines¶
Function argument to command line option conversion. Provides means to describe commands with complicated syntaxes, which often combine sub-commands, options and arguments. Typical examples include subversion and ftp.
-
class
Config
(prefix='--', separator=' ', bool_opts=(), value_opts=(), iterable_opts=(), format_opts={}, mapped_opts={}, opposite_opts={})[source]¶ Command option definitions. Provides a single definition point for all the options supported by a command.
-
__init__
(prefix='--', separator=' ', bool_opts=(), value_opts=(), iterable_opts=(), format_opts={}, mapped_opts={}, opposite_opts={})[source]¶ Constructor. Its arguments are used to specify all the valid options. Each option is prefixed by prefix. When an option takes multiple arguments these are separated by a separator. bool_opts must be specified on the command line when they are True. value_opts take a single argument; iterable_opts take multiple arguments; format_opts have their syntax specified by means of a format string; mapped_opts require some form of translation, usually because they are not valid Python identifiers; opposite_opts must be specified on the command line when they are False.
-
-
class
Parser
(config, command, arguments, options, **defaults)[source]¶ Constructs a complex command line from the provided command and its options and arguments. Uses a
Config
instance, config, to provide means to check conditions on the supplied options. Other constraints on how options should be used may be expressed and verified by means of the check methods.-
__init__
(config, command, arguments, options, **defaults)[source]¶ Takes an instance of
Config
, a command to execute, an iterable of arguments and a mapping of options and their actual values. The remaining keyword arguments indicate the options supported by command with their default values.
-
checkExactlyOneOption
(*options)[source]¶ Checks that one and only one in a set of mutually exclusive options has been specified.
-
checkExclusiveOptions
(*options)[source]¶ Checks that at most one in a set of mutually exclusive options has been specified.
-
checkNotBothOptsAndArgs
(*options)[source]¶ Checks that options incompatible with arguments haven’t been specified if any argument is present.
-