rbtools.utils.console¶
Utilities for working with console interactions.
Functions
|
Interactively prompt for a Yes/No answer. |
|
Interactively prompt for a specific answer from a list of options. |
|
Run a user-configured editor to edit an existing file. |
|
Run a user-configured editor to prompt for text. |
|
Ask the user for input. |
|
Ask the user for a password. |
- rbtools.utils.console.get_input(prompt, require=False, stderr=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)[source]¶
Ask the user for input.
Changed in version 3.1:
stdout
andstderr
streams are now supported. This can now be used with a non-TTY input stream.If
stdin
is not a TTY, this will read lines of input until it receives a valid answer.- Parameters:
prompt (
unicode
) – The text to prompt the user with.require (
bool
, optional) – Whether to require a result. IfTrue
, this will keep prompting until a non-empty value is entered.stderr (
io.TextIOWrapper
orfile
, optional) –The error stream to use for the prompt.
New in version 3.1.
stdin (
io.TextIOWrapper
orfile
, optional) –The input stream to use.
New in version 3.1.
- Returns:
The entered user data.
- Return type:
- rbtools.utils.console.get_pass(prompt, require=False, stderr=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)[source]¶
Ask the user for a password.
Changed in version 3.1:
stdout
andstderr
streams are now supported. This can now be used with a non-TTY input stream.If
stdin
is not a TTY, this will read lines of input until it receives a valid answer.- Parameters:
prompt (
unicode
) – The text to prompt the user with.require (
bool
, optional) – Whether to require a result. IfTrue
, this will keep prompting until a non-empty value is entered.stderr (
io.TextIOWrapper
orfile
, optional) –The error stream to use for the prompt.
New in version 3.1.
stdin (
io.TextIOWrapper
orfile
, optional) –The input stream to use.
New in version 3.1.
- Returns:
The entered password.
- Return type:
- rbtools.utils.console.confirm(question, stderr=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)[source]¶
Interactively prompt for a Yes/No answer.
This requires a Yes or a No answer. These are case-insensitive.
Valid Yes answers include:
y
,yes
,t
,true
,on
,1
.Valid No answers include:
n
,no
,f
,false
,off
,0
.If
stdin
is not a TTY, this will read lines of input until it receives a valid answer.Changed in version 3.1:
stdout
andstderr
streams are now supported. This can now be used with a non-TTY input stream.- Parameters:
question (
unicode
) – The question to ask.stderr (
io.TextIOWrapper
orfile
, optional) –The error stream to use for the prompt.
New in version 3.1.
stdin (
io.TextIOWrapper
orfile
, optional) –The input stream to use.
New in version 3.1.
- Returns:
The confirmed value.
- Return type:
- rbtools.utils.console.confirm_select(question, options_length, stderr=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>, stdin=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>)[source]¶
Interactively prompt for a specific answer from a list of options.
Accepted answers are integers starting from 1 until an integer n representing the nth of element within options.
If
stdin
is not a TTY, this will read lines of input until it receives a valid answer.Changed in version 3.1:
stdout
andstderr
streams are now supported. This can now be used with a non-TTY input stream.- Parameters:
question (
unicode
) – The prompt to be displayed.options_length (
int
) – The number of available options that the user can choose a response from.stderr (
io.TextIOWrapper
orfile
, optional) –The error stream to use for the prompt.
New in version 3.1.
stdin (
io.TextIOWrapper
orfile
, optional) –The input stream to use.
New in version 3.1.
- Returns:
The user’s chosen response. If the user decides to cancel the prompt, None is returned.
- Return type:
- rbtools.utils.console.edit_file(filename)[source]¶
Run a user-configured editor to edit an existing file.
This will run a configured text editor (trying the
VISUAL
orEDITOR
environment variables, falling back on vi) to request text for use in a commit message or some other purpose.
- rbtools.utils.console.edit_text(content='', filename=None)[source]¶
Run a user-configured editor to prompt for text.
This will run a configured text editor (trying the
VISUAL
orEDITOR
environment variables, falling back on vi) to request text for use in a commit message or some other purpose.- Parameters:
- Returns:
The resulting content.
- Return type:
- Raises:
rbcommons.utils.errors.EditorError – The configured editor could not be run, or it failed with an error.