def __delitem__(self, key): """Remove all properties with this key.""" if isinstance(key, str): key = key.encode('utf-8') ret = capi.lib.notmuch_message_remove_all_properties(self._ptr(), key) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)
def threads(self): """Return an iterator over all the threads found by the query.""" threads_pp = capi.ffi.new('notmuch_threads_t **') ret = capi.lib.notmuch_query_search_threads(self._query_p, threads_pp) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) return thread.ThreadIter(self, threads_pp[0], db=self._db)
def count_threads(self): """Return the number of threads matching this query.""" count_p = capi.ffi.new('unsigned int *') ret = capi.lib.notmuch_query_count_threads(self._query_p, count_p) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) return count_p[0]
def to_maildir_flags(self): """Update the message's maildir flags based on the notmuch tags. If the message's filename is in a maildir directory, that is a directory named ``new`` or ``cur``, and has a valid maildir filename then the flags will be added as such: 'D' if the message has the "draft" tag 'F' if the message has the "flagged" tag 'P' if the message has the "passed" tag 'R' if the message has the "replied" tag 'S' if the message does not have the "unread" tag Any existing flags unmentioned in the list above will be preserved in the renaming. Also, if this filename is in a directory named "new", rename it to be within the neighboring directory named "cur". In case there are multiple files associated with the message all filenames will get the same logic applied. """ ret = capi.lib.notmuch_message_tags_to_maildir_flags(self._ptr()) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)
def from_maildir_flags(self): """Update the tags based on the state in the message's maildir flags. This function examines the filenames of 'message' for maildir flags, and adds or removes tags on 'message' as follows when these flags are present: Flag Action if present ---- ----------------- 'D' Adds the "draft" tag to the message 'F' Adds the "flagged" tag to the message 'P' Adds the "passed" tag to the message 'R' Adds the "replied" tag to the message 'S' Removes the "unread" tag from the message For each flag that is not present, the opposite action (add/remove) is performed for the corresponding tags. Flags are identified as trailing components of the filename after a sequence of ":2,". If there are multiple filenames associated with this message, the flag is considered present if it appears in one or more filenames. (That is, the flags from the multiple filenames are combined with the logical OR operator.) """ ret = capi.lib.notmuch_message_maildir_flags_to_tags(self._ptr()) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)
def add(self, tag): """Add a tag to the message. :param tag: The tag to add. :type tag: str or bytes. A str will be encoded using UTF-8. :param sync_flags: Whether to sync the maildir flags with the new set of tags. Leaving this as *None* respects the configuration set in the database, while *True* will always sync and *False* will never sync. :param sync_flags: NoneType or bool :raises TypeError: If the tag is not a valid type. :raises TagTooLongError: If the added tag exceeds the maximum lenght, see ``notmuch_cffi.NOTMUCH_TAG_MAX``. :raises ReadOnlyDatabaseError: If the database is opened in read-only mode. """ if isinstance(tag, str): tag = tag.encode() if not isinstance(tag, bytes): raise TypeError('Not a valid type for a tag: {}'.format(type(tag))) ret = capi.lib.notmuch_message_add_tag(self._ptr(), tag) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)
def remove(self, key, value): """Remove a key-value pair from the properties.""" if isinstance(key, str): key = key.encode('utf-8') if isinstance(value, str): value = value.encode('utf-8') ret = capi.lib.notmuch_message_remove_property(self._ptr(), key, value) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)
def clear(self): """Remove all tags from the message. :raises ReadOnlyDatabaseError: If the database is opened in read-only mode. """ ret = capi.lib.notmuch_message_remove_all_tags(self._ptr()) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)
def frozen(self): """Context manager to freeze the message state. This allows you to perform atomic tag updates:: with msg.frozen(): msg.tags.clear() msg.tags.add('foo') Using This would ensure the message never ends up with no tags applied at all. It is safe to nest calls to this context manager. :raises ReadOnlyDatabaseError: if the database is opened in read-only mode. :raises UnbalancedFreezeThawError: if you somehow managed to call __exit__ of this context manager more than once. Why did you do that? :raises ObjectDestroyedError: if used after destoryed. """ ret = capi.lib.notmuch_message_freeze(self._msg_p) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) self._frozen = True try: yield except Exception: # Only way to "rollback" these changes is to destroy # ourselves and re-create. Behold. msgid = self.messageid self._destroy() with contextlib.suppress(Exception): new = self._db.find(msgid) self._msg_p = new._msg_p new._msg_p = None del new raise else: ret = capi.lib.notmuch_message_thaw(self._msg_p) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) self._frozen = False
def __getitem__(self, key): """Return **the first** peroperty associated with a key.""" if isinstance(key, str): key = key.encode('utf-8') value_pp = capi.ffi.new('char**') ret = capi.lib.notmuch_message_get_property(self._ptr(), key, value_pp) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) if value_pp[0] == capi.ffi.NULL: raise KeyError return base.BinString.from_cffi(value_pp[0])
def messages(self): """Return an iterator over all the messages found by the query. This executes the query and returns an iterator over the :class:`Message` objects found. """ msgs_pp = capi.ffi.new('notmuch_messages_t**') ret = capi.lib.notmuch_query_search_messages(self._query_p, msgs_pp) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret) return message.MessageIter(self, msgs_pp[0], db=self._db)
def clear(self): ret = capi.lib.notmuch_message_remove_all_properties( self._ptr(), capi.ffi.NULL) if ret != capi.lib.NOTMUCH_STATUS_SUCCESS: raise errors.NotmuchError(ret)