From d4c67504b31d8a4c6a89f02425f50c28970fcd43 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Tue, 30 Aug 2011 13:13:54 +0100 Subject: unifies pipe to cmd in send_mail and print. issue #39 --- alot/account.py | 20 ++++++-------------- alot/command.py | 18 +++++------------- alot/helper.py | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/alot/account.py b/alot/account.py index 0447d0a8..dcf971b4 100644 --- a/alot/account.py +++ b/alot/account.py @@ -29,6 +29,7 @@ from ConfigParser import SafeConfigParser from urlparse import urlparse from helper import cmd_output +import helper class Account: @@ -151,20 +152,11 @@ class SendmailAccount(Account): def send_mail(self, mail): mail['Date'] = email.utils.formatdate(time.time(), True) - # no unicode in shlex on 2.x - args = shlex.split(self.cmd.encode('ascii')) - try: - proc = subprocess.Popen(args, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, err = proc.communicate(mail.as_string()) - except OSError, e: - return str(e) + '. sendmail_cmd set to: %s' % self.cmd - if proc.poll(): # returncode is not 0 - return err.strip() - else: - self.store_sent_mail(mail) - return None + out, err = helper.pipe_to_command(self.cmd, mail.as_string()) + if err: + return err + '. sendmail_cmd set to: %s' % self.cmd + self.store_sent_mail(mail) + return None class AccountManager: diff --git a/alot/command.py b/alot/command.py index 67912f0a..612bf0bf 100644 --- a/alot/command.py +++ b/alot/command.py @@ -668,7 +668,6 @@ class PrintCommand(Command): 'set "print_cmd" in the global section.', priority='error') return - args = shlex.split(cmd.encode('ascii')) # get messages to print and set up notification strings if self.all: @@ -692,19 +691,12 @@ class PrintCommand(Command): if not self.separately: mailstrings = ['\n\n'.join(mailstrings)] - # print - try: - for mail in mailstrings: - proc = subprocess.Popen(args, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, err = proc.communicate(mail) - if proc.poll(): # returncode is not 0 - raise OSError(err) - except OSError, e: # handle errors - ui.notify(str(e), priority='error') - return + for mail in mailstrings: + out, err = helper.pipe_to_command(cmd, mail) + if err: + ui.notify(err, priority='error') + return # display 'done' message ui.notify(ok_msg) diff --git a/alot/helper.py b/alot/helper.py index 72c9cb63..3516d77e 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -61,6 +61,22 @@ 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 + return '', err.strip() + 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: -- cgit v1.2.3