GithubHelp home page GithubHelp logo

spall's Introduction

Status: Archived

This repository has been archived and is no longer maintained

Use subprocess.run instead

spall

Status Inactive License PyPI Build CodeQL pre-commit.ci status codecov.io python3.8 Black isort docformatter pylint Security Status Known Vulnerabilities spall

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

spall's People

Contributors

dependabot[bot] avatar jshwi avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.