Small tool to interact with shell pipes
Project description
Chut is a small tool to help you to interact with shell pipes.
Basically it will help to write some shell script in python
This is more like a toy than a real tool but… it may be usefull sometimes.
Installation
$ pip install chut
Usage
Import the shell:
>>> from chut import ch
Then run what you want:
>>> print(ch.cat('/etc/passwd') | ch.grep('root') | ch.cut("-d: -f1")) root
When I said what you want it’s mean that ch.whatyouwant will call a binary named whatyouwant
Let’s check if an error occured with whatyouwant:
>>> str(ch.whatyouwant()) # doctest: +ELLIPSIS Traceback (most recent call last): ... OSError: whatyouwant
But an error can also occured if the binary exist:
>>> cmd = ch.cat('whatyouwant') >>> output = str(cmd) >>> print(cmd.returncodes) [1] >>> print(cmd.stderr) cat: whatyouwant: No such file or directory
The pipe context manager
A context manager can help you to check for some errors:
>>> with ch.pipe(ch.cat('fff') | ch.grep('fff')) as p: # doctest: +ELLIPSIS ... print(p) Traceback (most recent call last): ... OSError: cat: fff: No such file or directory
Use python !!
Finally you can use some python code ad the end of the pipe (and only at the end):
>>> @ch.wraps ... def check_root(stdin): ... for line in stdin: ... if line.startswith(b'root'): ... yield b'User ' + line.split(b':', 1)[0] + b' exist\n' >>> with ch.pipe(ch.cat('/etc/passwd') | check_root) as cmd: ... for line in cmd: ... print(line) User root exist <BLANKLINE>
Output
You can get the output as string:
>>> output = str(ch.cat('/etc/passwd') | check_root)
As an iterator (iter over each lines of the output):
>>> output = ch.cat('/etc/passwd') | check_root
And can use some redirection:
>>> ch.cat('/etc/passwd') | check_root > 'users.txt' >>> print(ch.cat('users.txt')) User root exist >>> (ch.cat('/etc/passwd') | check_root) >> 'users.txt' >>> print(ch.cat('users.txt')) User root exist User root exist
Parentheses are needed with >> only (due to the way the python operator work)
>>> with ch.pipe(ch.rm(' -f users.txt')) as cmd: ... output = str(cmd)
Cheers.
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 chut-0.1.tar.gz
.
File metadata
- Download URL: chut-0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfc02be7d8e0096f5698cbf593fd3b7c53f719ce84843a42e117e7801d1e6dba |
|
MD5 | c60601cc185e7c0bf6d1129c9956a7d2 |
|
BLAKE2b-256 | a2b00f78eacae8d2502bb0da84435f86a2eda78a822aa99d499364839c097972 |