Skip to main content

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

Shell-formatters / pretty-printers

Subprocess Invocation

There's a lot of shell-related libraries out there, especially for invocation (see for example this list). The interface for shil is hopefully unsurprising, and something that's convenient but not fancy. It's a utility, and not a huge framework.

The main goal is to provide an API that is simple and stable, without a ton of dependencies.

>>> import shil
>>> proc = shil.invoke('echo hello world')
>>> assert proc.succeeded
>>> assert proc.stdout.strip()=='hello world'
>>>

Beyond such basics, shil includes support for rich output and uses pydantic for datastructures.

See the API docs for more detailed information.


Installation

See pypi for available releases.

$ pip install shil

Usage (CLI)

The shil library publishes a small CLI tool, mostly just for testing & demoing the API behaviour. See the CLI docs for the latest (autogenerated) help.


Usage (API)

See also:

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(command='printf hello-world\n')
>>> print(resp.stdout)
hello-world
>>>

Loading data when command-output is JSON

>>> import shil
>>> cmd = shil.Invocation(command="""echo '{"foo":"bar"}'""", load_json=True)
>>> resp = cmd()
>>> print(resp.data)
{'foo': 'bar'}
>>> assert type(resp.data) == type({})
>>> assert resp.data['foo'] == 'bar'
>>>

Serialization with Pydantic

>>> import json, shil
>>> req = cmd = shil.Invocation(command="""echo pipes-are-allowed|grep allowed""")
>>> resp = cmd()
>>> keys = resp.dict().keys()
>>> expected = 'stdout stderr failed failure success succeeded data'.split()
>>> assert all([k in keys for k in expected])
>>>

Caller determines logging

Works like this with basic logger:

>>> import logging, shil
>>> logger = logging.getLogger()
>>> resp = 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)
>>> resp = 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, rich
>>> req = cmd = shil.Invocation(command='echo {"foo":"bar"}')
>>> resp = cmd()
>>> rich.print(req)
>>> rich.print(resp)

By default, output looks roughly like this:

rich console
rich console

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.

>>> import shil
>>> from rich.console import Console
>>> console=Console(stderr=True)
>>> runner = shil.Runner(output_logger=console.log, command_logger=console.log)
>>> resp = runner('ls /tmp')
>>> assert isinstance(resp,(shil.InvocationResult,))
>>>

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

shil-2025.8.6.5.22.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

shil-2025.8.6.5.22-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file shil-2025.8.6.5.22.tar.gz.

File metadata

  • Download URL: shil-2025.8.6.5.22.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for shil-2025.8.6.5.22.tar.gz
Algorithm Hash digest
SHA256 f6a4d1dd7dc0c5de513addc6ecbacbd365b3a6f34b9173ca8f83265e0ed17d0c
MD5 412609a8daf812bcbd9d3f46b546b128
BLAKE2b-256 37335b4ca1823cff71acb92e520f608039925044df41e4b9bd897d3538929541

See more details on using hashes here.

File details

Details for the file shil-2025.8.6.5.22-py3-none-any.whl.

File metadata

  • Download URL: shil-2025.8.6.5.22-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for shil-2025.8.6.5.22-py3-none-any.whl
Algorithm Hash digest
SHA256 2eb75bf993aab96da1f1cfea89089ee96cbfe89b3faeecbf6bf1d12440e09168
MD5 ca8c5be2bae0f8403f81e3d60de7c753
BLAKE2b-256 39ba4332589d6fbfbabe56a2d41cc03ed9c7bb004dfdf8bddab51cc42547d10b

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