Extending Python syntax to implement shell commands as pipes using subprocess
Project description
Python Shell Pipes
A simple way to access your shell commands in Python
The shpipes
package uses subprocess.Popen
to run commands in a shell easily from native Python.
>>> from shpipes import Pipe
# python --version
>>> Pipe('python')('--version').getvalue()
'Python 3.8.6\n'
Chaining pipes together
You can chain together your commands, passing output from one to the input of another, similar to shell pipes. You can use the bitwise inclusive or operator to chain Pipe
instances together.
# echo 1+1 | bc
>>> (Pipe('echo')('1+1') | Pipe('bc')).getvalue()
'2\n'
# This also works
>>> pipe = Pipe('echo')('1+1')
>>> pipe |= Pipe('bc'))
>>> pipe.getvalue()
'2\n'
Loading commands from your PATH
Shell Pipes can also collect all executables from your PATH
variable and gather them into a Commands
instance so you can use the lib just like your native shell.
>>> from shpipes import Commands
>>> shell = Commands()
# find . -type f | grep .py$ | wc -l
>>> (shell.find('.', '-type', 'f') | shell.grep('.py$') | shell.wc('-l')).getvalue()
'9\n'
# this also works
>>> pipe = shell.find('.', '-type', 'f')
>>> pipe |= shell.grep('.py$')
>>> pipe |= shell.wc('-l')
>>> pipe.getvalue()
'9\n'
Handling pipe arguments
When a Pipe
is called, its arguments are directly passed to Popen
so beware that strings must be quoted properly.
You can use getvalue()
to get the output from one pipe and then pass it as a command argument instead of input text.
Pipes are only evaluated when getvalue()
is called
>>> license = shell.find('.', '-name', '"LICENSE"').getvalue()
>>> shell.wc(license).getvalue()
' 21 169 1069 ./LICENSE\n'
Shell by default
Since pipes run in a shell by default, environment variables evaluate automatically
>>> pipe = shell.ps('-u $USER')
>>> pipe |= shell.grep('python')
>>> pipe |= shell.head('-1')
Configuration options
If you need to run a different shell or disable shell entirely, then you can pass options via environment variables or kwargs to Pipe
Overriding options in Pipe
You can override all options to Popen
(except stdin
/stdout
) by passing arguments to Pipe
By default Popen
is run in shell mode with your default shell.
>>> Pipe('ls', executable='/bin/zsh', cwd='/var', shell=False)
Set env SHPIPES_NO_SHELL
=true
Sets shell=False
in the call to Popen
Set env SHPIPES_SHELL
=/bin/zsh
Changes the executable shell run by Popen
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file shell-pipes-0.1.0.tar.gz
.
File metadata
- Download URL: shell-pipes-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58af2c90074fc8ec76a95f026c0d59f570a6226c2a551e7c4fe1fceaf2ad1198 |
|
MD5 | 7acaed47af40095bf1f17c51d7fc9023 |
|
BLAKE2b-256 | 2f3327e00c1fce243ee5ff623a4a15521b57e4d83952af668e8b434afc52979f |