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='ls /tmp')
resp = cmd()
print(resp.stdout)
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.7.19.2.41.tar.gz
(11.2 kB
view hashes)
Built Distribution
Close
Hashes for shil-2023.7.19.2.41-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6aa44c9cbcb55a902af6a92c5647c5c6ac6f1f3c2cc946ef4fbf3d82c22c353b |
|
MD5 | ca9cc5e17933c2fe3ea3d06f3700ee46 |
|
BLAKE2b-256 | aff9befcc1fb1789f0c7eb6d483bf31cca21aace10ea3192f026f47a98faa8d0 |