Giter VIP home page Giter VIP logo

txsh's Introduction

txsh

txsh is a project largely inspired by [sh] (https://github.com/amoffat/sh). txsh is a dynamic wrapper around [Twisted] (http://twistedmatrix.com) [ProcessProtocol] (https://twistedmatrix.com/documents/current/api/twisted.internet.protocol.ProcessProtocol.html) and [spawnProcess] (http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IReactorProcess.spawnProcess.html) that allows you to call any program as if it were a function and return a deferred with its exit code and output:

from twisted.internet import reactor
from txsh import ls

def my_callback(exc_info):
    print 'Exit Code:', exc_info.status
    print 'Output:', exc_info.stdout
    print 'Errors:', exc_info.stderr
    reactor.stop()

d = ls()
d.addCallback(my_callback)

reactor.run()

Examples

from txsh import ls, curl, wc, git, sudo

# arguments should go separated
d = ls("-l", "-h") # ls -l -h

# Keyword arguments are also supported
d = ls(help=True)  # ls --help

# Underscores will be replaced by dashes
d = curl(connect_timeout=10, url="http://something")
# curl --connect-timeout 10 --url http:/something

# You can pipe
d = wc(ls())

# You can have subcommands
d = git.branch()  # Same as git("branch")
d = sudo.ls("-h")  # Same as sudo("ls", "-h")

# You can bake
ll = ls.bake("-l", "-h")
d = ll()  # Now ll will always output ls -l -h

# You can redirect stderr or stdout to a file using special args _out and _err
d = ls("-l", _out=open('output.log', 'wb'))

# In fact, you can use any file-like object like a StringIO.

# A callabble.
def alert(error):
    pass  # Do something

d = ls("-l", _err=alert) # Will redirect stderr to alert function.

# If you pass a string, we will simply assume it's a filename.
d = ls("-l", _out="output.log", _err="error.log")

# You can also pass a DeferredQueue or a simple Deferred.
queue = DeferredQueue()
my_defer = Deferred()
d = ls("-l", _out=queue, _err=my_defer)
# When stdout is ready, it will call queue.put
# When stderr is ready, it will call my_defer.callback

txsh is not a collection of system commands implemented in Twisted.

Installation

$> pip install txsh

To-Do

- Proper documentation / tutorials.
- Tests
- Passing of any object, Queue, or any iterable (list, set, dictionary, etc) to stdin
- Custom success exit codes
- Raising Failures if exit_code is not successful so user can add errbacks to deal with them.
- Glob Expansion
- Advanced piping
- usePTY
- Python 3

txsh's People

Stargazers

Jakub Pavlík avatar Kozo Nishida avatar Martín Gaitán avatar Dávid Fazekas avatar Adam Drakeford avatar Fabio Cerqueira avatar

Watchers

Kozo Nishida avatar Nicholas Amorim avatar

Forkers

minskmaz

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.