Shell API¶
shell¶
shell¶
A better way to run shell commands in Python.
If you just need to quickly run a command, you can use the shell shortcut
function:
>>> from shell import shell
>>> ls = shell('ls')
>>> for file in ls.output():
... print file
'another.txt'
If you need to extend the behavior, you can also use the Shell object:
>>> from shell import Shell
>>> sh = Shell(has_input=True)
>>> cat = sh.run('cat -u')
>>> cat.write('Hello, world!')
>>> cat.output()
['Hello, world!']
-
exception
shell.MissingCommandException¶ Thrown when no command was setup.
-
class
shell.Shell(has_input=False, record_output=True, record_errors=True, strip_empty=True)¶ Handles executing commands & recording output.
Optionally accepts a
has_inputparameter, which should be a boolean. If set toTrue, the command will wait to execute until you call theShell.writemethod & send input. (Default:False)Optionally accepts a
record_outputparameter, which should be a boolean. If set toTrue, the stdout from the command will be recorded. (Default:True)Optionally accepts a
record_errorsparameter, which should be a boolean. If set toTrue, the stderr from the command will be recorded. (Default:True)Optionally accepts a
strip_emptyparameter, which should be a boolean. If set toTrue, only non-empty lines fromShell.outputorShell.errorswill be returned. (Default:True)-
errors(raw=False)¶ Returns the errors from running a command.
Optionally accepts a
rawparameter, which should be a boolean. Ifrawis set toFalse, you get an array of lines of errors. Ifrawis set toTrue, the raw string of errors is returned. (Default:False)Example:
>>> from shell import Shell >>> sh = Shell() >>> sh.run('ls /there-s-no-way-anyone/has/this/directory/please') >>> sh.errors() [ 'ls /there-s-no-way-anyone/has/this/directory/please: No such file or directory' ]
-
kill()¶ Kills a given process.
Example:
>>> from shell import Shell >>> sh = Shell() >>> sh.run('some_long_running_thing') >>> sh.kill()
-
output(raw=False)¶ Returns the output from running a command.
Optionally accepts a
rawparameter, which should be a boolean. Ifrawis set toFalse, you get an array of lines of output. Ifrawis set toTrue, the raw string of output is returned. (Default:False)Example:
>>> from shell import Shell >>> sh = Shell() >>> sh.run('ls ~') >>> sh.output() [ 'hello.txt', 'world.txt', ]
-
run(command)¶ Runs a given command.
Requires a
commandparameter should be either a string command (easier) or an array of arguments to send as the command (if you know what you’re doing).Returns the
Shellinstance.Example:
>>> from shell import Shell >>> sh = Shell() >>> sh.run('ls- alh')
-
write(the_input)¶ If you’re working with an interactive process, sends that input to the process.
This needs to be used in conjunction with the
has_input=Trueparameter.Requires a
the_inputparameter, which should be a string of the input to send to the command.Returns the
Shellinstance.Example:
>>> from shell import Shell >>> sh = Shell(has_input=True) >>> sh.run('cat -u') >>> sh.write('Hello world!')
-
-
exception
shell.ShellException¶ The base exception for all shell-related errors.
-
shell.shell(command, has_input=False, record_output=True, record_errors=True, strip_empty=True)¶ A convenient shortcut for running commands.
Requires a
commandparameter should be either a string command (easier) or an array of arguments to send as the command (if you know what you’re doing).Optionally accepts a
has_inputparameter, which should be a boolean. If set toTrue, the command will wait to execute until you call theShell.writemethod & send input. (Default:False)Optionally accepts a
record_outputparameter, which should be a boolean. If set toTrue, the stdout from the command will be recorded. (Default:True)Optionally accepts a
record_errorsparameter, which should be a boolean. If set toTrue, the stderr from the command will be recorded. (Default:True)Optionally accepts a
strip_emptyparameter, which should be a boolean. If set toTrue, only non-empty lines fromShell.outputorShell.errorswill be returned. (Default:True)Returns the
Shellinstance, which has been run with the given command.Example:
>>> from shell import shell >>> sh = shell('ls -alh *py') >>> sh.output() ['hello.py', 'world.py']