X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=cnotmuch%2Fmessage.py;h=7980f921a792880b3be0a12ce04fb2ba25a2e6b3;hp=6a68c92d3ca1b1e8686131e9b580a9ea080b8df5;hb=3d6590be0207d7f0babfda3d26cf9b7c76451fab;hpb=61a547bd3e2760f1f835762f92c83cff2c1c71e5 diff --git a/cnotmuch/message.py b/cnotmuch/message.py index 6a68c92d..7980f921 100644 --- a/cnotmuch/message.py +++ b/cnotmuch/message.py @@ -221,10 +221,8 @@ class Messages(object): raise NotmuchError next_indent = indent + 1 - + # get replies and print them also out (if there are any) replies = msg.get_replies() - # if isinstance(replies, types.NoneType): - # break if not replies is None: sys.stdout.write(set_sep) replies.print_messages(format, next_indent, entire_thread) @@ -651,17 +649,20 @@ class Message(object): email_msg = email.message_from_file(fp) fp.close() - # A subfunction to recursively unpack the message parts into a - # list. - def msg_unpacker_gen(msg): + out = [] + for msg in email_msg.walk(): if not msg.is_multipart(): - yield msg - else: - for part in msg.get_payload(): - for subpart in msg_unpacker_gen(part): - yield subpart + out.append(msg) + return out - return list(msg_unpacker_gen(email_msg)) + def get_part(self, num): + """Returns the nth message body part""" + parts = self.get_message_parts() + if (num <= 0 or num > len(parts)): + return "" + else: + out_part = parts[(num - 1)] + return out_part.get_payload(decode=True) def format_message_internal(self): """Create an internal representation of the message parts, @@ -675,7 +676,7 @@ class Message(object): output["tags"] = list(self.get_tags()) headers = {} - for h in ["subject", "from", "to", "cc", "bcc", "date"]: + for h in ["Subject", "From", "To", "Cc", "Bcc", "Date"]: headers[h] = self.get_header(h) output["headers"] = headers @@ -687,7 +688,7 @@ class Message(object): part_dict["id"] = i + 1 # We'll be using this is a lot, so let's just get it once. cont_type = msg.get_content_type() - part_dict["content_type"] = cont_type + part_dict["content-type"] = cont_type # NOTE: # Now we emulate the current behaviour, where it ignores # the html if there's a text representation. @@ -696,16 +697,16 @@ class Message(object): # here in the future than to end up with another # incompatible solution. disposition = msg["Content-Disposition"] - if disposition: - if disposition.lower().startswith("attachment"): - part_dict["filename"] = msg.get_filename() + if disposition and disposition.lower().startswith("attachment"): + part_dict["filename"] = msg.get_filename() else: if cont_type.lower() == "text/plain": part_dict["content"] = msg.get_payload() elif (cont_type.lower() == "text/html" and i == 0): part_dict["content"] = msg.get_payload() - body.append(part_dict) + body.append(part_dict) + output["body"] = body return output @@ -726,19 +727,19 @@ class Message(object): output += "\n\fheader{" #Todo: this date is supposed to be prettified, as in the index. - output += "\n%s (%s) (" % (format["headers"]["from"], - format["headers"]["date"]) + output += "\n%s (%s) (" % (format["headers"]["From"], + format["headers"]["Date"]) output += ", ".join(format["tags"]) output += ")" - output += "\nSubject: %s" % format["headers"]["subject"] - output += "\nFrom: %s" % format["headers"]["from"] - output += "\nTo: %s" % format["headers"]["to"] - if format["headers"]["cc"]: - output += "\nCc: %s" % format["headers"]["cc"] - if format["headers"]["bcc"]: - output += "\nBcc: %s" % format["headers"]["bcc"] - output += "\nDate: %s" % format["headers"]["date"] + output += "\nSubject: %s" % format["headers"]["Subject"] + output += "\nFrom: %s" % format["headers"]["From"] + output += "\nTo: %s" % format["headers"]["To"] + if format["headers"]["Cc"]: + output += "\nCc: %s" % format["headers"]["Cc"] + if format["headers"]["Bcc"]: + output += "\nBcc: %s" % format["headers"]["Bcc"] + output += "\nDate: %s" % format["headers"]["Date"] output += "\n\fheader}" output += "\n\fbody{" @@ -749,16 +750,16 @@ class Message(object): if not p.has_key("filename"): output += "\n\fpart{ " output += "ID: %d, Content-type: %s\n" % (p["id"], - p["content_type"]) + p["content-type"]) if p.has_key("content"): output += "\n%s\n" % p["content"] else: - output += "Non-text part: %s\n" % p["content_type"] + output += "Non-text part: %s\n" % p["content-type"] output += "\n\fpart}" else: output += "\n\fattachment{ " output += "ID: %d, Content-type:%s\n" % (p["id"], - p["content_type"]) + p["content-type"]) output += "Attachment: %s\n" % p["filename"] output += "\n\fattachment}\n"