Object-oriented commandline
Project description
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
Built Distribution
File details
Details for the file spall-0.6.1.tar.gz
.
File metadata
- Download URL: spall-0.6.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.8.13 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83002b2b60a7fd49c2a9108938d099fbf975048a4e0349f06c1a3c589de2bfcb |
|
MD5 | ec9e724ead3d2dfc1486b5b38321102f |
|
BLAKE2b-256 | 1d6bdc82a52870f09ef73b0b8ed1b47e5fb6cb57aae7c75bd7864ab19a42671f |
File details
Details for the file spall-0.6.1-py3-none-any.whl
.
File metadata
- Download URL: spall-0.6.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.8.13 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0174d6c8fdc13d7198c6c981c3a4eeb79898e4794b6ba3c35d4272ece0e1bb1 |
|
MD5 | 3f2da8925b18399a090ace629af0734a |
|
BLAKE2b-256 | 4f522ea47d92752782bbf397b77437126db52193e47ef69c83cd91ab0f7e6cd2 |