summaryrefslogtreecommitdiff
path: root/alot/helper.py
diff options
context:
space:
mode:
Diffstat (limited to 'alot/helper.py')
-rw-r--r--alot/helper.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/alot/helper.py b/alot/helper.py
index eb0d4978..91da0811 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -51,7 +51,7 @@ def pretty_datetime(d):
def cmd_output(command_line):
- args = shlex.split(command_line)
+ args = shlex.split(command_line.encode('ascii', errors='ignore'))
try:
output = subprocess.check_output(args)
except subprocess.CalledProcessError:
@@ -61,6 +61,25 @@ def cmd_output(command_line):
return output
+def pipe_to_command(cmd, stdin):
+ # no unicode in shlex on 2.x
+ args = shlex.split(cmd.encode('ascii'))
+ try:
+ proc = subprocess.Popen(args, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ out, err = proc.communicate(stdin)
+ except OSError, e:
+ return '', str(e)
+ if proc.poll(): # returncode is not 0
+ e = 'return value != 0'
+ if err.strip():
+ e = e + ': %s' % err
+ return '', e
+ else:
+ return out, err
+
+
def attach(path, mail, filename=None):
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
@@ -94,3 +113,13 @@ def attach(path, mail, filename=None):
part.add_header('Content-Disposition', 'attachment',
filename=filename)
mail.attach(part)
+
+
+def shell_quote(text):
+ r'''
+ >>> print(shell_quote("hello"))
+ 'hello'
+ >>> print(shell_quote("hello'there"))
+ 'hello'"'"'there'
+ '''
+ return "'%s'" % text.replace("'", """'"'"'""")