X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=cnotmuch%2Fmessage.py;h=0e5057f42751310094626e8bd63171f9ff64dce7;hp=411d84da93b9a5a8006f13a617d5e1fda0056be3;hb=702f88ea9021afc26f4d58a3e920912d0faebb28;hpb=5c4af8ce39ee5bc5868075f4f20e9abed5102156 diff --git a/cnotmuch/message.py b/cnotmuch/message.py index 411d84da..0e5057f4 100644 --- a/cnotmuch/message.py +++ b/cnotmuch/message.py @@ -16,7 +16,7 @@ # (C) Copyright 2010 Sebastian Spaeth # Jesse Rosenthal -from ctypes import c_char_p, c_void_p, c_long, c_bool +from ctypes import c_char_p, c_void_p, c_long, c_uint from datetime import date from cnotmuch.globals import nmlib, STATUS, NotmuchError, Enum from cnotmuch.tag import Tags @@ -172,14 +172,19 @@ class Messages(object): self._msgs = None return i - - def __del__(self): """Close and free the notmuch Messages""" if self._msgs is not None: nmlib.notmuch_messages_destroy (self._msgs) - def show_messages(self, format, indent=0, entire_thread=True): + def print_messages(self, format, indent=0, entire_thread=False): + """Outputs messages as needed for 'notmuch show' to sys.stdout + + :param format: A string of either 'text' or 'json'. + :param indent: A number indicating the reply depth of these messages. + :param entire_thread: A bool, indicating whether we want to output + whole threads or only the matching messages. + """ if format.lower() == "text": set_start = "" set_end = "" @@ -195,6 +200,7 @@ class Messages(object): sys.stdout.write(set_start) + # iterate through all toplevel messages in this thread for msg in self: # if not msg: # break @@ -215,14 +221,11 @@ 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.show_messages(format, next_indent, entire_thread) - + replies.print_messages(format, next_indent, entire_thread) sys.stdout.write(set_end) sys.stdout.write(set_end) @@ -240,7 +243,7 @@ class Message(object): """notmuch_message_get_flag""" _get_flag = nmlib.notmuch_message_get_flag - _get_flag.restype = c_bool + _get_flag.restype = c_uint """notmuch_message_get_message_id (notmuch_message_t *message)""" _get_message_id = nmlib.notmuch_message_get_message_id @@ -403,7 +406,7 @@ class Message(object): :param flag: One of the :attr:`Message.FLAG` values (currently only *Message.FLAG.MATCH* - :returns: A bool, indicating whether the flag is set. + :returns: An unsigned int (0/1), indicating whether the flag is set. :exception: :exc:`NotmuchError` STATUS.NOT_INITIALIZED if the message is not initialized. """ @@ -646,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, @@ -670,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 @@ -682,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. @@ -691,9 +697,8 @@ 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() @@ -701,6 +706,7 @@ class Message(object): i == 0): part_dict["content"] = msg.get_payload() body.append(part_dict) + output["body"] = body return output @@ -716,26 +722,25 @@ class Message(object): easy to change to a new format when the format changes.""" format = self.format_message_internal() - output = "\n\fmessage{ id:%s depth:%d match:%d filename:%s" \ + output = "\fmessage{ id:%s depth:%d match:%d filename:%s" \ % (format['id'], indent, format['match'], format['filename']) output += "\n\fheader{" - #Todo: this date is supposed to be cleaned up, as in the index. - output += "\n%s (%s) (" % (format["headers"]["from"], - format["headers"]["date"]) + #Todo: this date is supposed to be prettified, as in the index. + output += "\n%s (%s) (" % (format["headers"]["From"], + format["headers"]["Date"]) output += ", ".join(format["tags"]) - output += ")\n" - - - 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 += "\nheader}\f" + 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 += "\n\fheader}" output += "\n\fbody{" @@ -744,26 +749,25 @@ class Message(object): for p in parts: if not p.has_key("filename"): output += "\n\fpart{ " - output += "ID: %d, Content-type:%s\n" % (p["id"], - p["content_type"]) + output += "ID: %d, Content-type: %s\n" % (p["id"], + 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" output += "\n\fbody}\n" - output += "\n\fmessage}\n" + output += "\n\fmessage}" return output - def __del__(self): """Close and free the notmuch Message""" if self._msg is not None: