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