- self._msgs = msgs_p
- #store parent, so we keep them alive as long as self is alive
- self._parent = parent
- logging.debug("Inited Messages derived from %s" %(str(parent)))
-
- def collect_tags(self):
- """Return the unique :class:`Tags` in the contained messages
-
- :returns: :class:`Tags`
- :exceptions: :exc:`NotmuchError` STATUS.NOT_INITIALIZED if not inited
-
- .. note:: :meth:`collect_tags` will iterate over the messages and
- therefore will not allow further iterations.
- """
- if self._msgs is None:
- raise NotmuchError(STATUS.NOT_INITIALIZED)
-
- # 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 (as in retrieve them)!
- """
- 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:
- logging.debug("Freeing the Messages now")
- nmlib.notmuch_messages_destroy (self._msgs)
-
-
-#------------------------------------------------------------------------------
-class Message(object):
- """Represents a single Email message
-
- Technically, this wraps the underlying *notmuch_message_t* structure.
- """
-
- """notmuch_message_get_filename (notmuch_message_t *message)"""
- _get_filename = nmlib.notmuch_message_get_filename
- _get_filename.restype = c_char_p
- """notmuch_message_get_message_id (notmuch_message_t *message)"""
- _get_message_id = nmlib.notmuch_message_get_message_id
- _get_message_id.restype = c_char_p
-
- """notmuch_message_get_tags (notmuch_message_t *message)"""
- _get_tags = nmlib.notmuch_message_get_tags
- _get_tags.restype = c_void_p
-
- _get_date = nmlib.notmuch_message_get_date
- _get_date.restype = c_uint64
-
- _get_header = nmlib.notmuch_message_get_header
- _get_header.restype = c_char_p
-
- def __init__(self, msg_p, parent=None):
- """
- :param msg_p: A pointer to an internal notmuch_message_t
- Structure. If it is `None`, we will raise an :exc:`NotmuchError`
- STATUS.NULL_POINTER.
- :param parent: A 'parent' object is passed which this message is
- derived from. We save a reference to it, so we can
- automatically delete the parent object once all derived
- objects are dead.
- """
- if msg_p is None:
- NotmuchError(STATUS.NULL_POINTER)
- self._msg = msg_p
- #keep reference to parent, so we keep it alive
- self._parent = parent
- logging.debug("Inited Message derived from %s" %(str(parent)))
-
-
- def get_message_id(self):
- """Return the message ID
-
- :returns: String with a message ID
- :exception: :exc:`NotmuchError` STATUS.NOT_INITIALIZED if the message
- is not initialized.
- """
- if self._msg is None:
- raise NotmuchError(STATUS.NOT_INITIALIZED)
- return Message._get_message_id(self._msg)
-
- def get_date(self):
- """Returns time_t of the message date
-
- For the original textual representation of the Date header from the
- message call notmuch_message_get_header() with a header value of
- "date".
-
- :returns: a time_t timestamp
- :rtype: c_unit64
- :exception: :exc:`NotmuchError` STATUS.NOT_INITIALIZED if the message
- is not initialized.
- """
- if self._msg is None:
- raise NotmuchError(STATUS.NOT_INITIALIZED)
- return Message._get_date(self._msg)
-
- def get_header(self, header):
- """Returns a message header
-
- This returns any message header that is stored in the notmuch database.
- This is only a selected subset of headers, which is currently:
-
- TODO: add stored headers
-
- :param header: The name of the header to be retrieved.
- It is not case-sensitive (TODO: confirm).
- :type header: str
- :returns: The header value as string
- :exception: :exc:`NotmuchError`
-
- * STATUS.NOT_INITIALIZED if the message
- is not initialized.
- * STATUS.NULL_POINTER, if no header was found
- """
- if self._msg is None:
- raise NotmuchError(STATUS.NOT_INITIALIZED)