Hello and sorry for the delay,

On 12/26/2014 10:33 PM, Antoine Beaupré wrote:
> 
> Thanks for the patch, it's a great idea!
> 
> Can you redo the patch with the "with" construct instead of manual
> exception handling?

i'm not very adept in python and/or coding, but i tried. attached are
two patches, both using the 'with' construct,
monkeysphere-fileoutput2.diff is without exception handling, but there
could be an exception if the file is not writeable.
monkeysphere-fileoutput3.diff is with exception handling to catch this case.
(i wasn't sure what you meant with 'manual' exception handling...)

> Also, I'd suggest the message "Could not write to %s: file exists" as an
> error message.
fixed ;)

however, i stumbled over another problem: thunderbird sets the
content-type for messages, so the mail then looks a bit confusing if i
just copy and paste the output...
if i find the time i'll think about a solution (maybe don't set content
type and just save two files, content and attachment...)

cheers,
muri
diff --git a/monkeysign/ui.py b/monkeysign/ui.py
index c9b6a30..d88d63d 100644
--- a/monkeysign/ui.py
+++ b/monkeysign/ui.py
@@ -93,6 +93,7 @@ class MonkeysignUi(object):
                           help=_('do not send email at all (default: use sendmail)'))
         parser.add_option('-t', '--to', dest='to', 
                           help=_('override destination email for testing (default: send individually encrypted email to each uid chosen)'))
+	parser.add_option('-o', '--output', dest='output', help=_('save the email to a file'))
         return parser
 
     def parse_args(self, args):
@@ -381,6 +382,13 @@ expects an EmailFactory email, but will not mail if nomail is set"""
 not sending email to %s, as requested, here's the email message:
 
 %s""") % (msg.mailto, msg.create_mail_from_block()))
+            if self.options.output is not None:
+                if not os.path.exists(self.options.output):
+                    with open(self.options.output, 'w') as f:
+                        f.write(str(msg.create_mail_from_block()))
+                        self.warn(_('Mail has been written to %s') % self.options.output)
+                else:
+                    self.warn(_('Could not write to %s: file exists') % self.options.output)
 
 
 class EmailFactory:
diff --git a/monkeysign/ui.py b/monkeysign/ui.py
index c9b6a30..860e241 100644
--- a/monkeysign/ui.py
+++ b/monkeysign/ui.py
@@ -93,6 +93,7 @@ class MonkeysignUi(object):
                           help=_('do not send email at all (default: use sendmail)'))
         parser.add_option('-t', '--to', dest='to', 
                           help=_('override destination email for testing (default: send individually encrypted email to each uid chosen)'))
+	parser.add_option('-o', '--output', dest='output', help=_('save the email to a file'))
         return parser
 
     def parse_args(self, args):
@@ -381,6 +382,16 @@ expects an EmailFactory email, but will not mail if nomail is set"""
 not sending email to %s, as requested, here's the email message:
 
 %s""") % (msg.mailto, msg.create_mail_from_block()))
+            if self.options.output is not None:
+                if not os.path.exists(self.options.output):
+                    try:
+                        with open(self.options.output, 'w') as f:
+                            f.write(str(msg.create_mail_from_block()))
+                            self.warn(_('Mail has been written to %s') % self.options.output)
+                    except IOError:
+                        self.warn(_('Could not write to file %s') % self.options.output)
+                else:
+                    self.warn(_('Could not write to %s: file exists') % self.options.output)
 
 
 class EmailFactory:

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to