2 This file is part of notmuch.
4 Notmuch is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation, either version 3 of the License, or (at your
7 option) any later version.
9 Notmuch is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with notmuch. If not, see <http://www.gnu.org/licenses/>.
17 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
19 from ctypes import c_char_p
20 from notmuch.globals import nmlib, STATUS, NotmuchError
24 """Represents a list of notmuch tags
26 This object provides an iterator over a list of notmuch tags (which
27 are unicode instances).
29 Do note that the underlying library only provides a one-time
30 iterator (it cannot reset the iterator to the start). Thus iterating
31 over the function will "exhaust" the list of tags, and a subsequent
32 iteration attempt will raise a :exc:`NotmuchError`
33 STATUS.NOT_INITIALIZED. Also note, that any function that uses
34 iteration (nearly all) will also exhaust the tags. So both::
36 for tag in tags: print tag
40 number_of_tags = len(tags)
44 #str() iterates over all tags to construct a space separated list
47 will "exhaust" the Tags. If you need to re-iterate over a list of
48 tags you will need to retrieve a new :class:`Tags` object.
52 _get = nmlib.notmuch_tags_get
53 _get.restype = c_char_p
55 def __init__(self, tags_p, parent=None):
57 :param tags_p: A pointer to an underlying *notmuch_tags_t*
58 structure. These are not publically exposed, so a user
59 will almost never instantiate a :class:`Tags` object
60 herself. They are usually handed back as a result,
61 e.g. in :meth:`Database.get_all_tags`. *tags_p* must be
62 valid, we will raise an :exc:`NotmuchError`
63 (STATUS.NULL_POINTER) if it is `None`.
64 :type tags_p: :class:`ctypes.c_void_p`
65 :param parent: The parent object (ie :class:`Database` or
66 :class:`Message` these tags are derived from, and saves a
67 reference to it, so we can automatically delete the db object
68 once all derived objects are dead.
69 :TODO: Make the iterator optionally work more than once by
70 cache the tags in the Python object(?)
73 NotmuchError(STATUS.NULL_POINTER)
76 #save reference to parent object so we keep it alive
80 """ Make Tags an iterator """
84 if self._tags is None:
85 raise NotmuchError(STATUS.NOT_INITIALIZED)
86 if not nmlib.notmuch_tags_valid(self._tags):
89 tag = Tags._get(self._tags).decode('UTF-8')
90 nmlib.notmuch_tags_move_to_next(self._tags)
93 def __nonzero__(self):
94 """Implement bool(Tags) check that can be repeatedly used
96 If __nonzero__ is not implemented, "if Tags()"
97 will implicitly call __len__, using up our iterator, so it is
98 important that this function is defined.
100 :returns: True if the Tags() iterator has at least one more Tag
102 return nmlib.notmuch_tags_valid(self._tags) > 0
105 """The str() representation of Tags() is a space separated list of tags
107 .. note:: As this iterates over the tags, we will not be able
108 to iterate over them again (as in retrieve them)! If
109 the tags have been exhausted already, this will raise a
110 :exc:`NotmuchError` STATUS.NOT_INITIALIZED on
113 return " ".join(self)
116 """Close and free the notmuch tags"""
117 if self._tags is not None:
118 nmlib.notmuch_tags_destroy(self._tags)