cement.utils.shell
¶
Common Shell Utilities.
-
class
cement.utils.shell.
Prompt
(text=None, *args, **kw)[source]¶ Bases:
cement.core.meta.MetaMixin
A wrapper around
input
whose purpose is to limit the redundent tasks of gather usr input. Can be used in several ways depending on the use case (simple input, options, and numbered selection).Parameters: text (str) – The text displayed at the input prompt. Example
Simple prompt to halt operations and wait for user to hit enter:
p = shell.Prompt("Press Enter To Continue", default='ENTER')
$ python myapp.py Press Enter To Continue $
Provide a numbered list for longer selections:
p = Prompt("Where do you live?", options=[ 'San Antonio, TX', 'Austin, TX', 'Dallas, TX', 'Houston, TX', ], numbered = True, )
Where do you live? 1: San Antonio, TX 2: Austin, TX 3: Dallas, TX 4: Houston, TX Enter the number for your selection:
Create a more complex prompt, and process the input from the user:
class MyPrompt(Prompt): class Meta: text = "Do you agree to the terms?" options = ['Yes', 'no', 'maybe-so'] options_separator = '|' default = 'no' clear = True max_attempts = 99 def process_input(self): if self.input.lower() == 'yes': # do something crazy pass else: # don't do anything... maybe exit? print("User doesn't agree! I'm outa here") sys.exit(1) MyPrompt()
$ python myapp.py [TERMINAL CLEAR] Do you agree to the terms? [Yes|no|maybe-so] no User doesn't agree! I'm outa here $ echo $? $ 1
-
class
Meta
[source]¶ Bases:
object
Optional meta-data (can also be passed as keyword arguments to the parent class).
-
auto
= True¶ Whether or not to automatically prompt() the user once the class is instantiated.
-
case_insensitive
= True¶ Whether to treat user input as case insensitive (only used to compare user input with available options).
-
clear
= False¶ Whether or not to clear the terminal when prompting the user.
-
clear_command
= 'clear'¶ Command to issue when clearing the terminal.
-
default
= None¶ A default value to use if the user doesn’t provide any input
-
max_attempts
= 10¶ Max attempts to get proper input from the user before giving up.
-
max_attempts_exception
= True¶ Raise an exception when max_attempts is hit? If not, Prompt passes the input through as
None
.
-
numbered
= False¶ Display options in a numbered list, where the user can enter a number. Useful for long selections.
-
options
= None¶ Options to provide to the user. If set, the input must match one of the items in the options selection.
-
options_separator
= ','¶ Separator to use within the option selection (non-numbered)
-
selection_text
= 'Enter the number for your selection:'¶ The text to display along with the numbered selection for user input.
-
suppress
= False¶ Suppress user input (use
getpass.getpass
instead ofbuiltins.input
. Default:False
.
-
text
= 'Tell me someting interesting:'¶ The text that is displayed to prompt the user
-
-
class
-
cement.utils.shell.
cmd
(command, capture=True, *args, **kwargs)[source]¶ Wrapper around
exec_cmd
andexec_cmd2
depending on whether capturing output is desired. Defaults to setting the Popenshell
keyword argument toTrue
(string command rather than list of command and arguments).Parameters: Other Parameters: - args – Additional arguments are passed to
Popen()
. - kwargs – Additional keyword arguments are passed to
Popen()
.
Returns: - When
capture==True
, returns the ``(stdout, stderror, return_code)`` of the command.
- int: When
capture==False
, returns only theexitcode
of the command.
Return type: Example
from cement.utils import shell # execute a command and capture output stdout, stderr, exitcode = shell.cmd('echo helloworld') # execute a command but do not capture output exitcode = shell.cmd('echo helloworld', capture=False)
- args – Additional arguments are passed to
-
cement.utils.shell.
exec_cmd
(cmd_args, *args, **kwargs)[source]¶ Execute a shell call using Subprocess. All additional
*args
and**kwargs
are passed directly tosubprocess.Popen
. See Subprocess for more information on the features ofPopen()
.Parameters: cmd_args (list) – List of command line arguments.
Other Parameters: - args – Additional arguments are passed to
Popen()
. - kwargs – Additional keyword arguments are passed to
Popen()
.
Returns: The
(stdout, stderror, return_code)
of the command.Return type: Example
from cement.utils import shell stdout, stderr, exitcode = shell.exec_cmd(['echo', 'helloworld'])
- args – Additional arguments are passed to
-
cement.utils.shell.
exec_cmd2
(cmd_args, *args, **kwargs)[source]¶ Similar to
exec_cmd
, however does not capture stdout, stderr (therefore allowing it to print to console). All additional*args
and**kwargs
are passed directly tosubprocess.Popen
. See Subprocess for more information on the features ofPopen()
.Parameters: cmd_args (list) – List of command line arguments
Other Parameters: - args – Additional arguments are passed to
Popen()
- kwargs – Additional keyword arguments are passed to
Popen()
Returns: The integer return code of the command.
Return type: Example
from cement.utils import shell exitcode = shell.exec_cmd2(['echo', 'helloworld'])
- args – Additional arguments are passed to
-
cement.utils.shell.
spawn
(target, start=True, join=False, thread=False, *args, **kwargs)[source]¶ Wrapper around
spawn_process
andspawn_thread
depending on desired execution model.Parameters: target (function) – The target function to execute in the sub-process.
Keyword Arguments: Other Parameters: - args – Additional arguments are passed to
Process()
- kwargs – Additional keyword arguments are passed to
Process()
.
Returns: The process object returned by Process().
Return type: Example
from cement.utils import shell def add(a, b): print(a + b) p = shell.spawn(add, args=(12, 27)) p.join()
- args – Additional arguments are passed to
-
cement.utils.shell.
spawn_process
(target, start=True, join=False, *args, **kwargs)[source]¶ A quick wrapper around
multiprocessing.Process()
. By default thestart()
function will be called before the spawned process object is returned. See MultiProcessing for more information on the features ofProcess()
.Parameters: target (function) – The target function to execute in the sub-process.
Keyword Arguments: Other Parameters: - args – Additional arguments are passed to
Process()
- kwargs – Additional keyword arguments are passed to
Process()
.
Returns: The process object returned by Process().
Return type: Example
from cement.utils import shell def add(a, b): print(a + b) p = shell.spawn_process(add, args=(12, 27)) p.join()
- args – Additional arguments are passed to
-
cement.utils.shell.
spawn_thread
(target, start=True, join=False, *args, **kwargs)[source]¶ A quick wrapper around
threading.Thread()
. By default thestart()
function will be called before the spawned thread object is returned See Threading for more information on the features ofThread()
.Parameters: target (function) – The target function to execute in the thread.
Keyword Arguments: Other Parameters: - args – Additional arguments are passed to
Thread()
. - kwargs – Additional keyword arguments are passed to
Thread()
.
Returns: The thread object returned by
Thread()
.Return type: Example
from cement.utils import shell def add(a, b): print(a + b) t = shell.spawn_thread(add, args=(12, 27)) t.join()
- args – Additional arguments are passed to