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 / grammer 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
import shil
import json
# Working with models for Invocation/InvocationResponse:
req = cmd = shil.Invocation(command='ls /tmp')
resp = cmd()
print(resp.stdout)
# More functional approach:
resp = shil.invoke('ls /tmp')
# Loading data when command-output is JSON
req = cmd = shil.Invocation(command='echo {"foo":"bar"}')
resp = cmd()
print(resp.data)
assert type(resp.data)==type({})
# Uses pydantic, so serialization is straightforward
json.loads(resp.json())
json.loads(req.json())
for k,v in req.dict().items():
assert getattr(resp,k)==v
# Pass in any loggers you want to use
import logging
logger = logging.getLogger()
shil.invoke('ls /tmp', command_logger=logger.critical, output_logger=logger.warning)
# Also works with rich-loggers
from rich.console import Console
console = Console(stderr=True)
shil.invoke('ls /tmp', command_logger=console.log)
# Rich-console output for `Invocation` and `InvocationResponse`
import rich
rich.print(req)
rich.print(resp)
# Instantiate a `Runner` to stay DRY
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.17.22.48.tar.gz
(10.3 kB
view hashes)
Built Distribution
Close
Hashes for shil-2023.7.17.22.48-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82eaf5c072b66cd5c220037e43d2093401c1758b6b624e5173e3d39b1f70a1f8 |
|
MD5 | 2b3ca3d75c6f79b40c4dabac9af9e482 |
|
BLAKE2b-256 | 51413a127456599c37ec618a5e1fbdaf6d94ebea9ca0ce04056acc899de2c0b4 |