4 from notmuch2 import _base as base
5 from notmuch2 import _capi as capi
6 from notmuch2 import _errors as errors
7 from notmuch2 import _message as message
8 from notmuch2 import _tags as tags
14 class Thread(base.NotmuchObject, collections.abc.Iterable):
15 _thread_p = base.MemoryPointer()
17 def __init__(self, parent, thread_p, *, db):
19 self._thread_p = thread_p
24 if not self._parent.alive:
28 except errors.ObjectDestroyedError:
38 capi.lib.notmuch_thread_destroy(self._thread_p)
43 """The thread ID as a :class:`BinString`.
45 :raises ObjectDestroyedError: if used after destroyed.
47 ret = capi.lib.notmuch_thread_get_thread_id(self._thread_p)
48 return base.BinString.from_cffi(ret)
51 """Return the number of messages in the thread.
53 :raises ObjectDestroyedError: if used after destroyed.
55 return capi.lib.notmuch_thread_get_total_messages(self._thread_p)
58 """Return an iterator of the toplevel messages.
60 :returns: An iterator yielding :class:`Message` instances.
62 :raises ObjectDestroyedError: if used after destroyed.
64 msgs_p = capi.lib.notmuch_thread_get_toplevel_messages(self._thread_p)
65 return message.MessageIter(self, msgs_p,
67 msg_cls=message.OwnedMessage)
70 """Return an iterator over all the messages in the thread.
72 :returns: An iterator yielding :class:`Message` instances.
74 :raises ObjectDestroyedError: if used after destroyed.
76 msgs_p = capi.lib.notmuch_thread_get_messages(self._thread_p)
77 return message.MessageIter(self, msgs_p,
79 msg_cls=message.OwnedMessage)
83 """The number of messages in this thread which matched the query.
85 Of the messages in the thread this gives the count of messages
86 which did directly match the search query which this thread
89 :raises ObjectDestroyedError: if used after destroyed.
91 return capi.lib.notmuch_thread_get_matched_messages(self._thread_p)
95 """A comma-separated string of all authors in the thread.
97 Authors of messages which matched the query the thread was
98 retrieved from will be at the head of the string, ordered by
99 date of their messages. Following this will be the authors of
100 the other messages in the thread, also ordered by date of
101 their messages. Both groups of authors are separated by the
104 :returns: The stringified list of authors.
107 :raises ObjectDestroyedError: if used after destroyed.
109 ret = capi.lib.notmuch_thread_get_authors(self._thread_p)
110 return base.BinString.from_cffi(ret)
114 """The subject of the thread, taken from the first message.
116 The thread's subject is taken to be the subject of the first
117 message according to query sort order.
119 :returns: The thread's subject.
122 :raises ObjectDestroyedError: if used after destroyed.
124 ret = capi.lib.notmuch_thread_get_subject(self._thread_p)
125 return base.BinString.from_cffi(ret)
129 """Return the date of the oldest message in the thread.
131 The time the first message was sent as an integer number of
132 seconds since the *epoch*, 1 Jan 1970.
134 :raises ObjectDestroyedError: if used after destroyed.
136 return capi.lib.notmuch_thread_get_oldest_date(self._thread_p)
140 """Return the date of the newest message in the thread.
142 The time the last message was sent as an integer number of
143 seconds since the *epoch*, 1 Jan 1970.
145 :raises ObjectDestroyedError: if used after destroyed.
147 return capi.lib.notmuch_thread_get_newest_date(self._thread_p)
151 """Return an immutable set with all tags used in this thread.
153 This returns an immutable set-like object implementing the
154 collections.abc.Set Abstract Base Class. Due to the
155 underlying libnotmuch implementation some operations have
156 different performance characteristics then plain set objects.
157 Mainly any lookup operation is O(n) rather then O(1).
159 Normal usage treats tags as UTF-8 encoded unicode strings so
160 they are exposed to Python as normal unicode string objects.
161 If you need to handle tags stored in libnotmuch which are not
162 valid unicode do check the :class:`ImmutableTagSet` docs for
165 :rtype: ImmutableTagSet
167 :raises ObjectDestroyedError: if used after destroyed.
170 ref = self._cached_tagset
171 except AttributeError:
176 tagset = tags.ImmutableTagSet(
177 self, '_thread_p', capi.lib.notmuch_thread_get_tags)
178 self._cached_tagset = weakref.ref(tagset)
182 class ThreadIter(base.NotmuchIter):
184 def __init__(self, parent, threads_p, *, db):
186 super().__init__(parent, threads_p,
187 fn_destroy=capi.lib.notmuch_threads_destroy,
188 fn_valid=capi.lib.notmuch_threads_valid,
189 fn_get=capi.lib.notmuch_threads_get,
190 fn_next=capi.lib.notmuch_threads_move_to_next)
193 thread_p = super().__next__()
194 return Thread(self, thread_p, db=self._db)