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()
[]

Redirect stdout to /dev/null with the devnull keyword argument

>>> echo.call("Hello, world", devnull=True)
0
>>> 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)

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.3.0.tar.gz (6.2 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.3.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spall-0.3.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.11+ Linux/5.16.18-200.fc35.x86_64

File hashes

Hashes for spall-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e3761e5f6506ed950218c613ac79f2bf4515805f3aea90c2b09919f55500a7f0
MD5 48a2385d189d1bdfe903a6cdec064e5c
BLAKE2b-256 7618678e925c813915a7aece537108d010c3d6a81558f7852d6599a0fb732d70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: spall-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.11+ Linux/5.16.18-200.fc35.x86_64

File hashes

Hashes for spall-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab813db19255a32681cf1dda77fe4b58c97b9c08451f36bd330a9b9639c755c6
MD5 d48d4430d9a41c9495a701b9270cfee0
BLAKE2b-256 ee78fe3da1a49eed768fe3410b214ce9d5476a2a6a1853a2967c25868898d420

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