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

  • 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:

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


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.18.23.20.tar.gz (11.2 kB view hashes)

Uploaded Source

Built Distribution

shil-2023.7.18.23.20-py3-none-any.whl (11.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page