]> git.notmuchmail.org Git - notmuch/blob - bindings/python-cffi/notmuch2/_query.py
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / bindings / python-cffi / notmuch2 / _query.py
1 from notmuch2 import _base as base
2 from notmuch2 import _capi as capi
3 from notmuch2 import _errors as errors
4 from notmuch2 import _message as message
5 from notmuch2 import _thread as thread
6
7
8 __all__ = []
9
10
11 class Query(base.NotmuchObject):
12     """Private, minimal query object.
13
14     This is not meant for users and is not a full implementation of
15     the query API.  It is only an intermediate used internally to
16     match libnotmuch's memory management.
17     """
18     _query_p = base.MemoryPointer()
19
20     def __init__(self, db, query_p):
21         self._db = db
22         self._query_p = query_p
23
24     @property
25     def alive(self):
26         if not self._db.alive:
27             return False
28         try:
29             self._query_p
30         except errors.ObjectDestroyedError:
31             return False
32         else:
33             return True
34
35     def __del__(self):
36         self._destroy()
37
38     def _destroy(self):
39         if self.alive:
40             capi.lib.notmuch_query_destroy(self._query_p)
41         self._query_p = None
42
43     @property
44     def query(self):
45         """The query string as seen by libnotmuch."""
46         q = capi.lib.notmuch_query_get_query_string(self._query_p)
47         return base.BinString.from_cffi(q)
48
49     def messages(self):
50         """Return an iterator over all the messages found by the query.
51
52         This executes the query and returns an iterator over the
53         :class:`Message` objects found.
54         """
55         msgs_pp = capi.ffi.new('notmuch_messages_t**')
56         ret = capi.lib.notmuch_query_search_messages(self._query_p, msgs_pp)
57         if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
58             raise errors.NotmuchError(ret)
59         return message.MessageIter(self, msgs_pp[0], db=self._db)
60
61     def count_messages(self):
62         """Return the number of messages matching this query."""
63         count_p = capi.ffi.new('unsigned int *')
64         ret = capi.lib.notmuch_query_count_messages(self._query_p, count_p)
65         if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
66             raise errors.NotmuchError(ret)
67         return count_p[0]
68
69     def threads(self):
70         """Return an iterator over all the threads found by the query."""
71         threads_pp = capi.ffi.new('notmuch_threads_t **')
72         ret = capi.lib.notmuch_query_search_threads(self._query_p, threads_pp)
73         if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
74             raise errors.NotmuchError(ret)
75         return thread.ThreadIter(self, threads_pp[0], db=self._db)
76
77     def count_threads(self):
78         """Return the number of threads matching this query."""
79         count_p = capi.ffi.new('unsigned int *')
80         ret = capi.lib.notmuch_query_count_threads(self._query_p, count_p)
81         if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
82             raise errors.NotmuchError(ret)
83         return count_p[0]