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 <https://www.gnu.org/licenses/>.
17 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>
19 from ctypes import c_char_p
20 from .globals import (
31 class Filenames(Python3StringMixIn):
32 """Represents a list of filenames as returned by notmuch
34 Objects of this class implement the iterator protocol.
38 The underlying library only provides a one-time iterator (it
39 cannot reset the iterator to the start). Thus iterating over
40 the function will "exhaust" the list of tags, and a subsequent
41 iteration attempt will raise a
42 :exc:`NotInitializedError`. Also note, that any function that
43 uses iteration (nearly all) will also exhaust the tags. So
46 for name in filenames: print name
50 list_of_names = list(names)
54 #str() iterates over all tags to construct a space separated list
57 will "exhaust" the Filenames. However, you can use
58 :meth:`Message.get_filenames` repeatedly to get fresh
59 Filenames objects to perform various actions on filenames.
62 #notmuch_filenames_get
63 _get = nmlib.notmuch_filenames_get
64 _get.argtypes = [NotmuchFilenamesP]
65 _get.restype = c_char_p
67 def __init__(self, files_p, parent):
69 :param files_p: A pointer to an underlying *notmuch_tags_t*
70 structure. These are not publically exposed, so a user
71 will almost never instantiate a :class:`Tags` object
72 herself. They are usually handed back as a result,
73 e.g. in :meth:`Database.get_all_tags`. *tags_p* must be
74 valid, we will raise an :exc:`NullPointerError`
76 :type files_p: :class:`ctypes.c_void_p`
77 :param parent: The parent object (ie :class:`Message` these
78 filenames are derived from, and saves a
79 reference to it, so we can automatically delete the db object
80 once all derived objects are dead.
83 raise NullPointerError()
85 self._files_p = files_p
86 #save reference to parent object so we keep it alive
90 """ Make Filenames an iterator """
93 _valid = nmlib.notmuch_filenames_valid
94 _valid.argtypes = [NotmuchFilenamesP]
97 _move_to_next = nmlib.notmuch_filenames_move_to_next
98 _move_to_next.argtypes = [NotmuchFilenamesP]
99 _move_to_next.restype = None
102 if not self._files_p:
103 raise NotInitializedError()
105 if not self._valid(self._files_p):
109 file_ = Filenames._get(self._files_p)
110 self._move_to_next(self._files_p)
111 return file_.decode('utf-8', 'ignore')
112 next = __next__ # python2.x iterator protocol compatibility
114 def __unicode__(self):
115 """Represent Filenames() as newline-separated list of full paths
119 This method exhausts the iterator object, so you will not be able to
120 iterate over them again.
122 return "\n".join(self)
124 _destroy = nmlib.notmuch_filenames_destroy
125 _destroy.argtypes = [NotmuchFilenamesP]
126 _destroy.restype = None
129 """Close and free the notmuch filenames"""
131 self._destroy(self._files_p)