From 97aeb18b20f901950da0355471fdc17055f3f4a8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 26 Jun 2009 09:27:50 -0400 Subject: Added ability to handle non text/* MIME types. The main problem was the encoding/decoding that was happening to _all_ input/output. Now many I/O activities have a `binary' option to disable any encoding/decoding. The `binary' flag is set whenever the comment content-type is not a text/* type. In order to print valid XML (and make life easy on xml/be-xml-to-mbox), non text/* types are printed out as base64-encoded MIME messages, so be list --xml | be-xml-to-mbox | catmutt works as you'd expect. With the standard (non-XML) output from `be show', we just print a message telling the user that we can't reasonably display the MIME type and that they should use the XML output if they want to see it. --- libbe/comment.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'libbe/comment.py') diff --git a/libbe/comment.py b/libbe/comment.py index df5a63f..9fa4a30 100644 --- a/libbe/comment.py +++ b/libbe/comment.py @@ -16,6 +16,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA +import email.mime.base, email.encoders import os import os.path import time @@ -150,11 +151,13 @@ class Comment(Tree, settings_object.SavedSettingsObject): def _get_comment_body(self): if self.rcs != None and self.sync_with_disk == True: import rcs - return self.rcs.get_file_contents(self.get_path("body")) + binary = not self.content_type.startswith("text/") + return self.rcs.get_file_contents(self.get_path("body"), binary=binary) def _set_comment_body(self, old=None, new=None, force=False): if (self.rcs != None and self.sync_with_disk == True) or force==True: assert new != None, "Can't save empty comment" - self.rcs.set_file_contents(self.get_path("body"), new) + binary = not self.content_type.startswith("text/") + self.rcs.set_file_contents(self.get_path("body"), new, binary=binary) @Property @change_hook_property(hook=_set_comment_body) @@ -236,13 +239,21 @@ class Comment(Tree, settings_object.SavedSettingsObject): """ if shortname == None: shortname = self.uuid + if self.content_type.startswith("text/"): + body = (self.body or "").rstrip('\n') + else: + maintype,subtype = self.content_type.split('/',1) + msg = email.mime.base.MIMEBase(maintype, subtype) + msg.set_payload(self.body or "") + email.encoders.encode_base64(msg) + body = msg.as_string() info = [("uuid", self.uuid), ("short-name", shortname), ("in-reply-to", self.in_reply_to), ("from", self._setting_attr_string("From")), ("date", self.time_string), ("content-type", self.content_type), - ("body", (self.body or "").rstrip('\n'))] + ("body", body)] lines = [""] for (k,v) in info: if v not in [settings_object.EMPTY, None]: @@ -276,7 +287,10 @@ class Comment(Tree, settings_object.SavedSettingsObject): lines.append("") #lines.append(textwrap.fill(self.body or "", # width=(79-indent))) - lines.extend((self.body or "").splitlines()) + if self.content_type.startswith("text/"): + lines.extend((self.body or "").splitlines()) + else: + lines.append("Content type %s not printable. Try XML output instead" % self.content_type) # some comments shouldn't be wrapped... istring = ' '*indent -- cgit