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:

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


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)

Uploaded Source

Built Distribution

shil-2023.7.17.22.48-py3-none-any.whl (10.6 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