Shell helper utilities for python
Project description
shil | |
Shell-util library for python. Includes helpers for subprocess invocation, shell-formatters / pretty-printers, and more. |
Overview
The shil
library provides various shell-utilities for python.
Features
- Helpers for subprocess invocation
- Shell-formatters / pretty-printers
- A somewhat usable parser / grammar for bash
- Console support for rich & rich protocols
Installation
See pypi for available releases.
pip install shil
Usage
See also:
- the unit-tests for some examples of library usage
- the smoke-tests for example usage of stand-alone scripts
OOP-style Dispatch
This uses shil.Invocation
and returns shil.InvocationResponse
.
>>> import shil
>>> req = cmd = shil.Invocation(command='printf hello-world\n')
>>> resp = cmd()
>>> print(resp.stdout)
hello-world
>>>
Functional approach to dispatch
Use shil.invoke
, get back shil.InvocationResponse
import shil
resp = shil.invoke('ls /tmp')
Loading data when command-output is JSON
import shil
req = cmd = shil.Invocation(command='echo {"foo":"bar"}')
resp = cmd()
print(resp.data)
assert type(resp.data) == type({})
assert resp.data['foo']=='bar'
Serialization with Pydantic
import shil
import json
req = cmd = shil.Invocation(command='echo {"foo":"bar"}')
resp = cmd()
json.loads(resp.json())
json.loads(req.json())
for k,v in req.dict().items():
assert getattr(resp,k)==v
Caller determines logging
Works like this with basic logger:
import shil
import logging
logger = logging.getLogger()
shil.invoke('ls /tmp', command_logger=logger.critical, output_logger=logger.warning)
Supports using rich-logger too:
import shil
from rich.console import Console
console = Console(stderr=True)
shil.invoke('ls /tmp', command_logger=console.log)
Rich-console Support
Besides using rich-logger as above, you can use the rich-protocol more directly. Printing works the way you'd expect for Invocation
and InvocationResponse
.
import shil
req = cmd = shil.Invocation(command='echo {"foo":"bar"}')
resp = cmd()
import rich
rich.print(req)
rich.print(resp)
Stay DRY with Runners
Runner's are basically just partials on shil.invoke
. It's simple but this can help reduce copying around repetitive configuration.
from rich.console import Console
console=Console(stderr=True)
runner = shil.Runner(
output_logger=console.log,
command_logger=console.log)
runner('ls /tmp')
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
shil-2023.8.21.19.59.tar.gz
(11.5 kB
view hashes)
Built Distribution
Close
Hashes for shil-2023.8.21.19.59-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65c3a41b5fb6523d78dcb1cd8ffa38318489ea869c564afb40683d43dea55068 |
|
MD5 | 422d3e487ffe94cefb5c1bf0fb6ee873 |
|
BLAKE2b-256 | 5692ff29e711a81ecc4c9b319a5287d725257f48b546732ff0cc7110116a6a6e |