- # collect all tags (returns NULL on error)
- tags_p = Messages._collect_tags (self._msgs)
- #reset _msgs as we iterated over it and can do so only once
- self._msgs = None
-
- if tags_p == None:
- raise NotmuchError(STATUS.NULL_POINTER)
- return Tags(tags_p, self)
-
- def __iter__(self):
- """ Make Messages an iterator """
- return self
-
- def next(self):
- if self._msgs is None:
- raise NotmuchError(STATUS.NOT_INITIALIZED)
-
- if not nmlib.notmuch_messages_valid(self._msgs):
- self._msgs = None
- raise StopIteration
-
- msg = Message(Messages._get (self._msgs), self)
- nmlib.notmuch_messages_move_to_next(self._msgs)
- return msg
-
- def __len__(self):
- """len(:class:`Messages`) returns the number of contained messages
-
- .. note:: As this iterates over the messages, we will not be able to
- iterate over them again! So this will fail::
-
- #THIS FAILS
- msgs = Database().create_query('').search_message()
- if len(msgs) > 0: #this 'exhausts' msgs
- # next line raises NotmuchError(STATUS.NOT_INITIALIZED)!!!
- for msg in msgs: print msg
-
- Most of the time, using the
- :meth:`Query.count_messages` is therefore more
- appropriate (and much faster). While not guaranteeing
- that it will return the exact same number than len(),
- in my tests it effectively always did so.
- """
- if self._msgs is None:
- raise NotmuchError(STATUS.NOT_INITIALIZED)
-
- i=0
- while nmlib.notmuch_messages_valid(self._msgs):
- nmlib.notmuch_messages_move_to_next(self._msgs)
- i += 1
- 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 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 = ""
- set_sep = ""
- elif format.lower() == "json":
- set_start = "["
- set_end = "]"
- set_sep = ", "
- else:
- raise Exception
-
- first_set = True
-
- sys.stdout.write(set_start)
-
- # iterate through all toplevel messages in this thread
- for msg in self:
- # if not msg:
- # break
- if not first_set:
- sys.stdout.write(set_sep)
- first_set = False
-
- sys.stdout.write(set_start)
- match = msg.is_match()
- next_indent = indent
-
- if (match or entire_thread):
- if format.lower() == "text":
- sys.stdout.write(msg.format_message_as_text(indent))
- elif format.lower() == "json":
- sys.stdout.write(msg.format_message_as_json(indent))
- else:
- raise NotmuchError
- next_indent = indent + 1
-
- # get replies and print them also out (if there are any)
- replies = msg.get_replies()
- if not replies is None:
- sys.stdout.write(set_sep)
- replies.print_messages(format, next_indent, entire_thread)
-
- sys.stdout.write(set_end)
- sys.stdout.write(set_end)
-
-#------------------------------------------------------------------------------
-class Message(object):