summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2012-01-29 21:54:03 +0000
committerPatrick Totzke <patricktotzke@gmail.com>2012-02-01 19:38:14 +0000
commitcf2b7fa00596ecad58a665f0b5bac3c58087ff04 (patch)
tree74b0bbef96e9c48a06b3187c1a6b9503c178a8a5
parentb9b6644ae32d3d489dfe8ca61854dc26bcc75822 (diff)
add helper call_cmd_async,
that does as call_cmd before but non-blocking: it returns a deferred that calls back with the requested triple.
-rw-r--r--alot/helper.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/alot/helper.py b/alot/helper.py
index e2c85f05..b55188e3 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -250,6 +250,48 @@ def call_cmd(cmdlist, stdin=None):
return out, err, ret
+def call_cmd_async(cmdlist, stdin=None):
+ """
+ get a shell commands output, error message and return value as a deferred.
+
+ :type cmdlist: list of str
+ :param stdin: string to pipe to the process
+ :type stdin: str
+ :return: deferred that calls back with triple of stdout, stderr and
+ return value of the shell command
+ :rtype: `twisted.internet.defer.Deferred`
+ """
+
+ from twisted.internet import process
+
+ class _EverythingGetter(protocol.ProcessProtocol):
+ def __init__(self, deferred):
+ self.deferred = deferred
+ self.outBuf = StringIO.StringIO()
+ self.errBuf = StringIO.StringIO()
+ self.outReceived = self.outBuf.write
+ self.errReceived = self.errBuf.write
+
+ def processEnded(self, reason):
+ out = self.outBuf.getvalue()
+ err = self.errBuf.getvalue()
+ out = string_decode(out, urwid.util.detected_encoding)
+ err = string_decode(err, urwid.util.detected_encoding)
+ e = reason.value
+ code = e.exitCode
+ if e.signal:
+ self.deferred.callback((out, err, e.signal))
+ else:
+ self.deferred.callback((out, err, code))
+
+ d = defer.Deferred()
+ proc = process.Process(executable=cmdlist[0], args=cmdlist[1:],
+ _EverythingGetter(d))
+ if stdin:
+ proc.write(stdin)
+ return d
+
+
def guess_mimetype(blob):
"""
uses file magic to determine the mime-type of the given data blob.