Skip to main content

shell pipes and redirecting for python

Project description

initially was named after a friend from school, Pavel Hurgcheev - `pash`. then
`psh`. until free name was found - `pipeish`.

it's like pipes in shell but not exactly. hence `pipe-ish`.

[picture-of-python-looking-like-a-shell]

it is still raw and WIP but mostly behaves like you running commands in shell.

How to use
-----
for installed package:
```
from pipeish import Shell
```

for development (replace `os.getcwd()` with path to pipeish sources)
```
import os; import sys; sys.path.insert(0, os.getcwd())
from pipeish import Shell
```

Examples
-----

simple as it is:

```
_()('ls')
_()('ls -la | head -3') # single string with '|' in shell-like way
```

it uses `str.split('|')` under the hood, so if your arguments contain `|`s you can feed comma separated list instead.

```
_()('gzcat stagedb.sql.gz', 'psql -U usermane pentagon_db')
```

if you need fancy redirections provide tuple (or list) with redirection options instead of string.
options are always two characters, first for `stdout` and second for `stderr`.
for example, 'nt' means "redirect stdout to /dev/null and don't redirect stderr".

there is a special variant for each stream, 'd', which means 'default behaviour'.
by default `pipeish` tryies to do what you expect from the same command when it runs in shell.


```
_()(
['ls -la', 'nt'],
('grep Pip', 'dd')
)
```

but none of these will cope with exit codes.
for this to work use `Shell` as context manager:

```
with Shell() as _:
_('true | false')
```

now your python script will exits with the same exit code as bash script with same commands. but it will exit right after exiting `with` scope. nothing below context manager will be executed. to disable this behaviour there is `check` argument:

```
with Shell(check=False) as _:
_('true | false')

print (_.last_retcode)
```

this way you can run different `Shell`s in one script which allows more complicated scenarios.


to capture outout of command you have to set 'i' option for stdout of last command:
```
with Shell() as _:
out, err, exit_code = _(['uname -a', 'id'])
```

this looks a bit inconvenient, but this is best replacement for POSIX shell redirection syntax I came up so far.
in this example:
1. output will be captured into `out` and will not be printed in terminal.
similiar to when you do `out = $(uname -a)` in bash, but here you can capture all two streams along with exit code in one call.
2. errors will be printed into terminal and will not be captured. which is happening when you forget to set redirect `2>&1` or `2>/dev/null` in bash script.
you can totally do it by the way in `pipeish` with respective options `r` or `n` for stderr ('ir' or 'in' for this example).
3. `exit_code` will contain exit code of _last_ command. which is you kinda expect when running chain of commands.

more are comming!

feel free to open issues with common use cases, I'll try to prioritize
what seems important and integrate.


Links
-----

"Bash is the JavaScript of systems programming". Strongly agree.
https://github.com/progrium/bashstyle

"shell=True: Security Considerations"
https://docs.python.org/3/library/subprocess.html#security-considerations


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pipeish-0.3.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pipeish-0.3.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file pipeish-0.3.0.tar.gz.

File metadata

  • Download URL: pipeish-0.3.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for pipeish-0.3.0.tar.gz
Algorithm Hash digest
SHA256 48caec29b97ae4d5fc91c680993ceccd1d16d57154e90e968a8af972ef7d7bd2
MD5 15d3fddd1921102bfafed2c2eef9191e
BLAKE2b-256 34c3b402f91a5f30e1c0b8dbddd5b4f341bca525b0ac06bda6a625b6a3147c8d

See more details on using hashes here.

File details

Details for the file pipeish-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pipeish-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for pipeish-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f5b104fef2b30401ea0d2e5bf4073c5eb670b16b7d4bff841e30c7d1a57099e
MD5 9652e27210aed3bd0009fac56b407833
BLAKE2b-256 b812ffe92442b994f14fef67e2299d3f0a2f998e19fd99e1ed680cef2143b12f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page