예제 #1
0
    def find(self, msgid):
        """Return the message matching the given message ID.

        If a message with the given message ID is found a
        :class:`Message` instance is returned.  Otherwise a
        :exc:`LookupError` is raised.

        :param msgid: The message ID to look for.
        :type msgid: str

        :returns: The message instance.
        :rtype: Message

        :raises LookupError: If no message was found.
        :raises OutOfMemoryError: When there is no memory to allocate
           the message instance.
        :raises XapianError: A Xapian exception occurred.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        msg_pp = capi.ffi.new('notmuch_message_t **')
        ret = capi.lib.notmuch_database_find_message(self._db_p,
                                                     msgid.encode(), msg_pp)
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        msg_p = msg_pp[0]
        if msg_p == capi.ffi.NULL:
            raise LookupError
        msg = message.Message(self, msg_p, db=self)
        return msg
예제 #2
0
    def get(self, filename):
        """Return the :class:`Message` given a pathname.

        If a message with the given pathname exists in the database
        return the :class:`Message` instance for the message.
        Otherwise raise a :exc:`LookupError` exception.

        :param filename: The pathname of the message.
        :type filename: str, bytes, os.PathLike or pathlib.Path

        :returns: The message instance.
        :rtype: Message

        :raises LookupError: If no message was found.  This is also
           a subclass of :exc:`KeyError`.
        :raises OutOfMemoryError: When there is no memory to allocate
           the message instance.
        :raises XapianError: A Xapian exception occurred.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        if not hasattr(os, 'PathLike') and isinstance(filename, pathlib.Path):
            filename = bytes(filename)
        msg_pp = capi.ffi.new('notmuch_message_t **')
        ret = capi.lib.notmuch_database_find_message_by_filename(
            self._db_p, os.fsencode(filename), msg_pp)
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        msg_p = msg_pp[0]
        if msg_p == capi.ffi.NULL:
            raise LookupError
        msg = message.Message(self, msg_p, db=self)
        return msg
예제 #3
0
    def add(self, filename, *, sync_flags=False, indexopts=None):
        """Add a message to the database.

        Add a new message to the notmuch database.  The message is
        referred to by the pathname of the maildir file.  If the
        message ID of the new message already exists in the database,
        this adds ``pathname`` to the list of list of files for the
        existing message.

        :param filename: The path of the file containing the message.
        :type filename: str, bytes, os.PathLike or pathlib.Path.
        :param sync_flags: Whether to sync the known maildir flags to
           notmuch tags.  See :meth:`Message.flags_to_tags` for
           details.
        :type sync_flags: bool
        :param indexopts: The indexing options, see
           :meth:`default_indexopts`.  Leave as `None` to use the
           default options configured in the database.
        :type indexopts: :class:`IndexOptions` or `None`

        :returns: A tuple where the first item is the newly inserted
           messages as a :class:`Message` instance, and the second
           item is a boolean indicating if the message inserted was a
           duplicate.  This is the namedtuple ``AddedMessage(msg,
           dup)``.
        :rtype: Database.AddedMessage

        If an exception is raised, no message was added.

        :raises XapianError: A Xapian exception occurred.
        :raises FileError: The file referred to by ``pathname`` could
           not be opened.
        :raises FileNotEmailError: The file referreed to by
           ``pathname`` is not recognised as an email message.
        :raises ReadOnlyDatabaseError: The database is opened in
           READ_ONLY mode.
        :raises UpgradeRequiredError: The database must be upgraded
           first.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        if not hasattr(os, 'PathLike') and isinstance(filename, pathlib.Path):
            filename = bytes(filename)
        msg_pp = capi.ffi.new('notmuch_message_t **')
        opts_p = indexopts._opts_p if indexopts else capi.ffi.NULL
        ret = capi.lib.notmuch_database_index_file(self._db_p,
                                                   os.fsencode(filename),
                                                   opts_p, msg_pp)
        ok = [
            capi.lib.NOTMUCH_STATUS_SUCCESS,
            capi.lib.NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID
        ]
        if ret not in ok:
            raise errors.NotmuchError(ret)
        msg = message.Message(self, msg_pp[0], db=self)
        if sync_flags:
            msg.tags.from_maildir_flags()
        return self.AddedMessage(
            msg, ret == capi.lib.NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)