Skip to main content

Object-oriented commandline

Project description

License PyPI CI pre-commit.ci status CodeQL codecov.io readthedocs.org python3.8 Black pylint

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.6.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

spall-0.6.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file spall-0.6.0.tar.gz.

File metadata

  • Download URL: spall-0.6.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.13 Darwin/22.2.0

File hashes

Hashes for spall-0.6.0.tar.gz
Algorithm Hash digest
SHA256 64cd0fceab9fac1fdedd63a18e765ba0433c84f8e8999ef5c249821ccb636c8a
MD5 75a80de603cceabf38d6332fc63e7e6e
BLAKE2b-256 3516504bd4149a92b896149a0a652f43571f45a7a3841da0878e0a5f1e64cdd1

See more details on using hashes here.

File details

Details for the file spall-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: spall-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.13 Darwin/22.2.0

File hashes

Hashes for spall-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e35f418be5398490bc624f0e1defa627d0f295c23b651e8be6850b318be1cb13
MD5 f6a8bcca23e6dec972d5b819043574d0
BLAKE2b-256 b8170a8f63d047bdc367b82a4c91fabdc6745e080426db0b85b30e636c85ea32

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