txsh 0.2

txsh is a dynamic wrapper around Twisted ProcessProtocol and spawnProcess that allows you to call any program as if it were a function and return a deferred with its exit code and output. If you wanna know what it does, check out my first release post.

Version 0.2 introduces a few new features and bug fixes.

– Support to subcommands. It looks nice to be able to do “git.branch(“-v”)” instead of  “git(“branch”, “-v”)”

– Redirection of stderr and stdout to any file-like object, deferred, deffered queue or callable. If a string is passed, we will assume it’s a filename we can open and write to it.

– Added a few unit tests.

– Fixed a bug where the subcommand was not being properly passed on .bake call.

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

The new release is on PyPI, so you can install it with pip. Check out the project page! Hope it’s useful for some.

Use Facebook to Comment on this Post

txsh – A Twisted Wrapper to Processes

txsh is a project *largely* inspired by sh.

txsh is a dynamic wrapper around Twisted ProcessProtocol and spawnProcess that allows you to call any program as if it were a function and return a deferred with its exit code and output. A basic example:

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

txsh does not implement “ls”. The lookup is dynamic. You can import curl, wc, whatever – well, provided you have an application called “whatever”.

Twisted API for processes is great but not necessarily practical and I believe this abstracts a lot of use cases. txsh, so far, supports arguments, named arguments, piping and baking. But there is still a lot to be done.

Some more examples:

from txsh import ls, curl, wc
 
# 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 bake
ll = ls.bake("-l", "-h")
d = ll()  # Now ll will always output ls -l -h

Using pip ?

pip install txsh

If someone has any suggestions or critiques, please leave them in the comments. Even better, open an issue in the project.

Use Facebook to Comment on this Post