]> git.notmuchmail.org Git - notmuch/blob - bindings/python/notmuch/filenames.py
Merge branch 'release'
[notmuch] / bindings / python / notmuch / filenames.py
1 """
2 This file is part of notmuch.
3
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.
8
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
12 for more details.
13
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/>.
16
17 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>
18 """
19 from ctypes import c_char_p
20 from .globals import (
21     nmlib,
22     NotmuchFilenamesP,
23     Python3StringMixIn,
24 )
25 from .errors import (
26     NullPointerError,
27     NotInitializedError,
28 )
29
30
31 class Filenames(Python3StringMixIn):
32     """Represents a list of filenames as returned by notmuch
33
34     Objects of this class implement the iterator protocol.
35
36     .. note::
37
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
44         both::
45
46            for name in filenames: print name
47
48         as well as::
49
50            list_of_names = list(names)
51
52         and even a simple::
53
54            #str() iterates over all tags to construct a space separated list
55            print(str(filenames))
56
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.
60     """
61
62     #notmuch_filenames_get
63     _get = nmlib.notmuch_filenames_get
64     _get.argtypes = [NotmuchFilenamesP]
65     _get.restype = c_char_p
66
67     def __init__(self, files_p, parent):
68         """
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`
75              if it is `None`.
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.
81         """
82         if not files_p:
83             raise NullPointerError()
84
85         self._files_p = files_p
86         #save reference to parent object so we keep it alive
87         self._parent = parent
88
89     def __iter__(self):
90         """ Make Filenames an iterator """
91         return self
92
93     _valid = nmlib.notmuch_filenames_valid
94     _valid.argtypes = [NotmuchFilenamesP]
95     _valid.restype = bool
96
97     _move_to_next = nmlib.notmuch_filenames_move_to_next
98     _move_to_next.argtypes = [NotmuchFilenamesP]
99     _move_to_next.restype = None
100
101     def __next__(self):
102         if not self._files_p:
103             raise NotInitializedError()
104
105         if not self._valid(self._files_p):
106             self._files_p = None
107             raise StopIteration
108
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
113
114     def __unicode__(self):
115         """Represent Filenames() as newline-separated list of full paths
116
117         .. note::
118
119             This method exhausts the iterator object, so you will not be able to
120             iterate over them again.
121         """
122         return "\n".join(self)
123
124     _destroy = nmlib.notmuch_filenames_destroy
125     _destroy.argtypes = [NotmuchFilenamesP]
126     _destroy.restype = None
127
128     def __del__(self):
129         """Close and free the notmuch filenames"""
130         if self._files_p:
131             self._destroy(self._files_p)