rbtools.commands.base.commands¶
Base classes for commands.
Added in version 5.0.
Classes
|
Base class for RBTools commands. |
|
Abstract base class for commands which offer subcommands. |
|
Abstract base class for a subcommand. |
|
Filters log messages of a given level. |
|
Smartly formats help text, preserving paragraphs. |
- class rbtools.commands.base.commands.BaseCommand(transport_cls: type[Transport] = <class 'rbtools.api.transport.sync.SyncTransport'>, stdout: TextIO = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, stderr: TextIO = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin: TextIO = <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)[source]¶
Bases:
objectBase class for RBTools commands.
This class will handle retrieving the configuration, and parsing command line options.
usageis a list of usage strings each showing a use case. These should not include the main rbt command or the command name; they will be added automatically.Changed in version 5.0: This moved from
rbtools.commandstorbtools.commands.base.commands.- __firstlineno__ = 143¶
- __init__(transport_cls: type[Transport] = <class 'rbtools.api.transport.sync.SyncTransport'>, stdout: TextIO = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, stderr: TextIO = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin: TextIO = <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>) None[source]¶
Initialize the base functionality for the command.
- Parameters:
transport_cls (
rbtools.api.transport.Transport, optional) – The transport class used for all API communication. By default, this uses the transport defined indefault_transport_cls.stdout (
io.TextIOWrapper, optional) –The standard output stream. This can be used to capture output programmatically.
Added in version 3.1.
stderr (
io.TextIOWrapper, optional) –The standard error stream. This can be used to capture errors programmatically.
Added in version 3.1.
stdin (
io.TextIOWrapper, optional) –The standard input stream. This can be used to provide input programmatically.
Added in version 3.1.
- __static_attributes__ = ('api_client', 'api_root', 'capabilities', 'config', 'json', 'log', 'needs_api', 'needs_scm_client', 'options', 'repository', 'repository_info', 'server_url', 'stderr', 'stderr_bytes', 'stderr_is_atty', 'stdin', 'stdin_is_atty', 'stdout', 'stdout_bytes', 'stdout_is_atty', 'tool', 'transport_cls')¶
- api_client: RBClient | None¶
The client used to connect to the API.
This will be set when the command is run if
needs_apiisTrue. Otherwise it will beNone.
- api_root: RootResource | None¶
The root of the API tree.
This will be set when the command is run if
needs_apiisTrue. Otherwise it will beNone.
- args: ClassVar[str] = ''¶
Usage text for what arguments the command takes.
Arguments for the command are anything passed in other than defined options (for example, revisions passed to rbt post).
- Type:
- capabilities: Capabilities | None¶
Capabilities set by the API.
This will be set when the command is run if
needs_apiisTrue. Otherwise it will beNone.
- config: RBToolsConfig | None¶
The loaded configuration for RBTools.
Changed in version 5.0: This is now a
RBToolsConfiginstance, instead of a plain dictionary.
- create_arg_parser(argv: list[str], *, color: bool = True) argparse.ArgumentParser[source]¶
Create and return the argument parser.
Changed in version 6.0: Added the
colorargument.- Parameters:
- Returns:
Argument parser for commandline arguments
- Return type:
- create_parser(config: RBToolsConfig, argv: list[str] | None = None, *, color: bool = True) argparse.ArgumentParser[source]¶
Return a new argument parser for this command.
Changed in version 6.0: Added the
colorargument.- Parameters:
- Returns:
The new argument parser for the command.
- Return type:
- credentials_prompt(realm: str, uri: str, username: str | None = None, password: str | None = None, *args, **kwargs) tuple[str, str][source]¶
Prompt the user for credentials using the command line.
This will prompt the user, and then return the provided username and password. This is used as a callback in the API when the user requires authorization.
- Parameters:
realm (
str) – The HTTP realm.uri (
str) – The URI of the endpoint requiring authentication.username (
str, optional) – The default username for authentication.password (
str, optional) – The default password for authentication.*args (
tuple, unused) – Unused additional positional arguments.**kwargs (
dict, unused) – Unused additional keyword arguments.
- Returns:
A 2-tuple of:
- Return type:
- Raises:
rbtools.commands.base.errors.CommandError – HTTP authentication failed.
- default_transport_cls[source]¶
alias of
SyncTransport
- description: ClassVar[str] = ''¶
A short description of the command, suitable for display in usage text.
- Type:
- get_api(server_url: str) tuple[RBClient, RootResource][source]¶
Return an RBClient instance and the associated root resource.
Commands should use this method to gain access to the API, instead of instantiating their own client. This will set the
api_clientattribute.Changed in version 6.0: This now sets the
api_clientattribute as soon as the client is instantiated.- Parameters:
server_url (
str) – The URL to the Review Board server.- Returns:
A 2-tuple of:
- Tuple:
0 (
rbtools.api.client.RBClient) – The new API client.1 (
rbtools.api.resource.RootResource) – The root resource for the API.
- Return type:
- get_capabilities(api_root: RootResource) Capabilities[source]¶
Retrieve capabilities from the server and return them.
- Parameters:
api_root (
rbtools.api.resource.RootResource) – The root resource- Returns:
The server capabilities.
- Return type:
- get_options_flat() Sequence[Option][source]¶
Return a list of all options for the command.
This returns a flat list of options, pulling out the options that are inside any option groups set on the command.
Added in version 6.0.
- Returns:
The list of options for the command.
- Return type:
- initialize() None[source]¶
Initialize the command.
This will set up various prerequisites for commands. Individual command subclasses can control what gets done by setting the various
needs_*attributes (as documented in this class).- Raises:
rbtools.commands.base.errors.CommandError – An error occurred while initializing the command.
rbtools.commands.base.errors.NeedsReinitialize – The initialization process needs to be restarted (due to loading additional config).
- initialize_scm_tool(client_name: str | None = None, *, tool_required: bool = True) tuple[RepositoryInfo | None, BaseSCMClient | None][source]¶
Initialize the SCM tool for the current working directory.
Changed in version 5.0.3: Added the
tool_requiredargument.Changed in version 5.0: Removed deprecated
require_repository_infoargument.- Parameters:
- Returns:
A 2-tuple:
- Tuple:
0 (
rbtools.clients.base.repository.RepositoryInfo) – The repository information.1 (
rbtools.clients.base.scmclient.BaseSCMClient) – The SCMTool client instance.
- Return type:
- json: JSONOutput¶
An output buffer for JSON results.
Commands can set this to return data used when a command is passed
--json.
- log: logging.Logger¶
A logger for the command.
- main(*args) int[source]¶
Run the main logic of the command.
This method should be overridden to implement the commands functionality.
- needs_api: ClassVar[bool] = False¶
Whether the command needs the API client.
If this is set, the initialization of the command will set
api_clientandapi_root.Added in version 3.0.
- Type:
- needs_diffs: ClassVar[bool] = False¶
Whether the command needs to generate diffs.
If this is set, the initialization of the command will check for the presence of a diff tool compatible with the chosen type of repository.
This depends on
needs_repositoryandneeds_scm_clientboth being set toTrue.Added in version 4.0.
- Type:
- needs_repository: ClassVar[bool] = False¶
Whether the command needs the remote repository object.
If this is set, the initialization of the command will set
repository.Setting this will imply setting both
needs_apiandneeds_scm_clienttoTrue.Added in version 3.0.
- Type:
- needs_scm_client: ClassVar[bool] = False¶
Whether the command needs the SCM client.
If this is set, the initialization of the command will set
repository_infoandtool.Added in version 3.0.
- Type:
- option_list: ClassVar[list[Option | OptionGroup]] = []¶
Command-line options for this command.
- Type:
listofOptionorOptionGroup
- options: argparse.Namespace¶
Options parsed for the command.
- otp_token_prompt(uri: str, token_method: str, *args, **kwargs) str[source]¶
Prompt the user for a one-time password token.
Their account is configured with two-factor authentication. The server will have sent a token to their configured mobile device or application. The user will be prompted for this token.
- post_process_options() None[source]¶
Post-process options for the command.
This can validate and update options before the command is invoked.
- Raises:
rbtools.commands.base.errors.CommandError – There was an error found with an option.
- repository: RepositoryItemResource | None¶
The resource for the matching repository.
This will be set when the command is run if both
needs_apiandneeds_repositoryareTrue.
- repository_info: RepositoryInfo | None¶
Information on the local repository.
This will be set when the command is run if
needs_scm_clientis run. Otherwise it will beNone.
- run_from_argv(argv: list[str]) None[source]¶
Execute the command using the provided arguments.
The options and commandline arguments will be parsed from
argvand the commandsmainmethod will be called.
- server_url: str | None¶
The URL to the Review Board server.
This will be set when the command is run if
needs_apiisTrue.
- stderr: OutputWrapper[str]¶
The stream for writing error output as Unicode strings.
Commands should write error text using this instead of
print()orsys.stderr().
- stderr_bytes: OutputWrapper[bytes]¶
The stream for writing error output as byte strings.
Commands should write error text using this instead of
print()orsys.stderr().
- stderr_is_atty: bool¶
Whether the stderr stream is from an interactive session.
This applies to
stderr.Added in version 3.1.
- stdin: TextIO¶
The stream for reading standard input.
Commands should read input from here instead of using
sys.stdin().Added in version 3.1.
- stdin_is_atty: bool¶
Whether the stdin stream is from an interactive session.
This applies to
stdin.Added in version 3.1.
- stdout: OutputWrapper[str]¶
The stream for writing standard output as Unicode strings.
Commands should write text using this instead of
print()orsys.stdout().
- stdout_bytes: OutputWrapper[bytes]¶
The stream for writing standard output as byte strings.
Commands should write text using this instead of
print()orsys.stdout().
- stdout_is_atty: bool¶
Whether the stdout stream is from an interactive session.
This applies to
stdout.Added in version 3.1.
- tool: BaseSCMClient | None¶
The client/tool used to communicate with the repository.
This will be set when the command is run if
needs_scm_clientis run. Otherwise it will beNone.
- class rbtools.commands.base.commands.BaseMultiCommand(transport_cls: type[Transport] = <class 'rbtools.api.transport.sync.SyncTransport'>, stdout: TextIO = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, stderr: TextIO = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin: TextIO = <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)[source]¶
Bases:
BaseCommandAbstract base class for commands which offer subcommands.
Some commands (such as rbt review) want to offer many subcommands.
Added in version 3.0.
- __firstlineno__ = 1835¶
- __static_attributes__ = ('subcommand', 'subcommand_parsers')¶
- common_subcommand_option_list: list[Option | OptionGroup] = []¶
Options common to all subcommands.
- Type:
- create_parser(config: RBToolsConfig, argv: list[str] | None = None, *, color: bool = True) argparse.ArgumentParser[source]¶
Create and return the argument parser for this command.
Changed in version 6.0: Added the
colorargument.- Parameters:
- Returns:
The argument parser.
- Return type:
- get_options_flat() Sequence[Option][source]¶
Return a list of all options for the command.
This returns a flat list of options, pulling out the options that are inside any option groups set on the command, and in any subcommands.
Added in version 6.0.
- Returns:
The list of options for the command.
- Return type:
- subcommand: BaseSubCommand¶
The currently-running subcommand.
- subcommand_parsers: dict[str, argparse.ArgumentParser]¶
A mapping of subcommand names to argument parsers.
- subcommands: list[type[BaseSubCommand]] = []¶
The available subcommands.
This is a list of BaseSubCommand subclasses.
- Type:
- class rbtools.commands.base.commands.BaseSubCommand(options: Namespace, config: RBToolsConfig, *args, **kwargs)[source]¶
Bases:
BaseCommandAbstract base class for a subcommand.
- __firstlineno__ = 1799¶
- __init__(options: Namespace, config: RBToolsConfig, *args, **kwargs) None[source]¶
Initialize the subcommand.
- Parameters:
options (
argparse.Namespace) – The parsed options.config (
rbtools.config.RBToolsConfigg) – The loaded RBTools configuration.*args (
list) – Positional arguments to pass to the Command class.**kwargs (
dict) – Keyword arguments to pass to the Command class.
- __static_attributes__ = ('config', 'options')¶
- class rbtools.commands.base.commands.LogLevelFilter(level: int)[source]¶
Bases:
FilterFilters log messages of a given level.
Only log messages that have the specified level will be allowed by this filter. This prevents propagation of higher level types to lower log handlers.
- __annotations_cache__ = {}¶
- __firstlineno__ = 63¶
- __init__(level: int) None[source]¶
Initialize the filter.
- Parameters:
level (
int) – The log level to filter for.
- __static_attributes__ = ('level',)¶
- class rbtools.commands.base.commands.SmartHelpFormatter(prog, indent_increment=2, max_help_position=24, width=None, color=True)[source]¶
Bases:
HelpFormatterSmartly formats help text, preserving paragraphs.
Changed in version 5.0: This moved from
rbtools.commandstorbtools.commands.base.commands.- __annotations_cache__ = {}¶
- __firstlineno__ = 100¶
- __static_attributes__ = ()¶