Skip to main content

Object-oriented commandline

Project description

License pypi CI CodeQL codecov.io readthedocs.org python3.8 black

Object-oriented commandline

Install

$ pip install spall

Development

$ pip install spall

Usage

Import Subprocess from spall

>>> from spall import Subprocess

Instantiate individual executables

>>> cat = Subprocess("cat")
>>> echo = Subprocess("echo")
>>> fails = Subprocess("false")

Default is to return returncode and print stdout and stderr to console

>>> returncode = echo.call("Hello, world")
Hello, world
>>> returncode
0

Capture stdout with the capture keyword argument

>>> echo.call("Hello, world", capture=True)
0

Stdout is consumed by calling stdout() which returns a list

>>> echo.stdout()
['Hello, world']
>>> echo.stdout()
[]

Stdout is accrued until stdout() is called

>>> echo.call("Hello, world", capture=True)
0
>>> echo.call("Goodbye, world", capture=True)
0
>>> echo.stdout()
['Hello, world', 'Goodbye, world']
>>> echo.stdout()
[]

Pipe stdout to file with the file keyword argument

>>> import os
>>> import tempfile
>>>
>>> tmp = tempfile.NamedTemporaryFile(delete=False)
>>> echo.call("Hello, world", file=tmp.name)
0
>>> returncode = cat.call(tmp.name)
Hello, world
>>> returncode
0
>>> os.remove(tmp.name)

# redirect to /dev/null
>>> echo.call("Hello, world", file=os.devnull)
0

Failing command will raise a subprocess.CalledProcessError

>>> import contextlib
>>> from subprocess import CalledProcessError
>>>
>>> with contextlib.redirect_stderr(None):
...     try:
...         returncode = fails.call()
...     except CalledProcessError as err:
...         str(err)
"Command 'false' returned non-zero exit status 1."
>>> returncode
0

This, however, will not

>>> with contextlib.redirect_stderr(None):
...     fails.call(suppress=True)
1

All the keyword arguments above can be set as the default for the instantiated object

>>> echo = Subprocess("echo", capture=True)
>>> echo.call("Hello, world")
0
>>> echo.stdout()
['Hello, world']

Which can then be overridden

>>> returncode = echo.call("Hello, world", capture=False)
Hello, world
>>> returncode
0
>>> echo.stdout()
[]

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

spall-0.5.0.tar.gz (6.1 kB view hashes)

Uploaded source

Built Distribution

spall-0.5.0-py3-none-any.whl (5.8 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page