1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
| | import collections.abc
import notdb._base as base
import notdb._capi as capi
import notdb._errors as errors
__all__ = ['ImmutableTagSet', 'MutableTagSet', 'TagsIter']
class ImmutableTagSet(base.NotmuchObject, collections.abc.Set):
"""The tags associated with a message thread or whole database.
Both a thread as well as the database expose the union of all tags
in messages associated with them. This exposes these as a
:class:`collections.abc.Set` object.
Note that due to the underlying notmuch API the performance of the
implementation is not the same as you would expect from normal
sets. E.g. the :meth:`__contains__` and :meth:`__len__` are O(n)
rather then O(1).
Tags are internally stored as bytestrings but normally exposed as
unicode strings using the UTF-8 encoding and the *ignore* decoder
error handler. However the :meth:`iter` method can be used to
return tags as bytestrings or using a different error handler.
Note that when doing arithmetic operations on tags, this class
will return a plain normal set as it is no longer associated with
the message.
:param parent: the parent object
:param ptr_name: the name of the attribute on the parent which will
return the memory pointer. This allows this object to
access the pointer via the parent's descriptor and thus
trigger :class:`MemoryPointer`'s memory safety.
:param cffi_fn: the callable CFFI wrapper to retrieve the tags
iter. This can be one of notmuch_database_get_all_tags,
notmuch_thread_get_tags or notmuch_message_get_tags.
"""
def __init__(self, parent, ptr_name, cffi_fn):
self._parent = parent
self._ptr = lambda: getattr(parent, ptr_name)
self._cffi_fn = cffi_fn
def __del__(self):
self._destroy()
@property
def alive(self):
return self._parent.alive
def _destroy(self):
pass
@classmethod
def _from_iterable(cls, it):
return set(it)
def __iter__(self):
"""Return an iterator over the tags.
Tags are yielded as unicode strings, decoded using the
"ignore" error handler.
:raises NullPointerError: If the iterator can not be created.
"""
return self.iter(encoding='utf-8', errors='ignore')
def iter(self, *, encoding=None, errors='strict'):
"""Aternate iterator constructor controlling string decoding.
Tags are stored as bytes in the notmuch database, in Python
it's easier to work with unicode strings and thus is what the
normal iterator returns. However this method allows you to
specify how you would like to get the tags, defaulting to the
bytestring representation instead of unicode strings.
:param encoding: Which codec to use. The default *None* does not
decode at all and will return the unmodified bytes.
Otherwise this is passed on to :func:`str.decode`.
:param errors: If using a codec, this is the error handler.
See :func:`str.decode` to which this is passed on.
:raises NullPointerError: When things do not go as planned.
"""
# self._cffi_fn should point either to
# notmuch_database_get_all_tags, notmuch_thread_get_tags or
# notmuch_message_get_tags. nothmuch.h suggests these never
# fail, let's handle NULL anyway.
tags_p = self._cffi_fn(self._ptr())
if tags_p == capi.ffi.NULL:
raise errors.NullPointerError()
tags = TagsIter(self, tags_p, encoding=encoding, errors=errors)
return tags
def __len__(self):
return sum(1 for t in self)
def __contains__(self, tag):
if isinstance(tag, str):
tag = tag.encode()
for msg_tag in self.iter():
if tag == msg_tag:
return True
else:
return False
def __eq__(self, other):
return tuple(sorted(self.iter())) == tuple(sorted(other.iter()))
def __hash__(self):
return hash(tuple(self.iter()))
def __repr__(self):
return '<{name} object at 0x{addr:x} tags={{{tags}}}>'.format(
name=self.__class__.__name__,
addr=id(self),
tags=', '.join(repr(t) for t in self))
class MutableTagSet(ImmutableTagSet, collections.abc.MutableSet):
"""The tags associated with a message.
This is a :class:`collections.abc.MutableSet` object which can be
used to manipulate the tags of a message.
Note that due to the underlying notmuch API the performance of the
implementation is not the same as you would expect from normal
sets. E.g. the ``in`` operator and variants are O(n) rather then
O(1).
Tags are bytestrings and calling ``iter()`` will return an
iterator yielding bytestrings. However the :meth:`iter` method
can be used to return tags as unicode strings, while all other
operations accept either byestrings or unicode strings. In case
unicode strings are used they will be encoded using utf-8 before
being passed to notmuch.
"""
# Since we subclass ImmutableTagSet we inherit a __hash__. But we
# are mutable, setting it to None will make the Python machinary
# recognise us as unhashable.
__hash__ = None
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 discard(self, tag):
"""Remove a tag from the message.
:param tag: The tag to remove.
:type tag: str of 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 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_remove_tag(self._ptr(), tag)
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 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 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)
class TagsIter(base.NotmuchObject, collections.abc.Iterator):
"""Iterator over tags.
This is only an interator, not a container so calling
:meth:`__iter__` does not return a new, replenished iterator but
only itself.
:param parent: The parent object to keep alive.
:param tags_p: The CFFI pointer to the C-level tags iterator.
:param encoding: Which codec to use. The default *None* does not
decode at all and will return the unmodified bytes.
Otherwise this is passed on to :func:`str.decode`.
:param errors: If using a codec, this is the error handler.
See :func:`str.decode` to which this is passed on.
:raises ObjectDestoryedError: if used after destroyed.
"""
_tags_p = base.MemoryPointer()
def __init__(self, parent, tags_p, *, encoding=None, errors='strict'):
self._parent = parent
self._tags_p = tags_p
self._encoding = encoding
self._errors = errors
def __del__(self):
self._destroy()
@property
def alive(self):
if not self._parent.alive:
return False
try:
self._tags_p
except errors.ObjectDestroyedError:
return False
else:
return True
def _destroy(self):
if self.alive:
try:
capi.lib.notmuch_tags_destroy(self._tags_p)
except errors.ObjectDestroyedError:
pass
self._tags_p = None
def __iter__(self):
"""Return the iterator itself.
Note that as this is an iterator and not a container this will
not return a new iterator. Thus any elements already consumed
will not be yielded by the :meth:`__next__` method anymore.
"""
return self
def __next__(self):
if not capi.lib.notmuch_tags_valid(self._tags_p):
self._destroy()
raise StopIteration()
tag_p = capi.lib.notmuch_tags_get(self._tags_p)
tag = capi.ffi.string(tag_p)
if self._encoding:
tag = tag.decode(encoding=self._encoding, errors=self._errors)
capi.lib.notmuch_tags_move_to_next(self._tags_p)
return tag
def __repr__(self):
try:
self._tags_p
except errors.ObjectDestroyedError:
return '<TagsIter (exhausted)>'
else:
return '<TagsIter>'
|