]> git.notmuchmail.org Git - notmuch/blob - bindings/python/notmuch/filename.py
a16e717e778d80fa4c9236ad94249c10ce237ceb
[notmuch] / bindings / python / notmuch / filename.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 <http://www.gnu.org/licenses/>.
16
17 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
18 """
19 from ctypes import c_char_p
20 from notmuch.globals import nmlib, STATUS, NotmuchError
21
22
23 class Filenames(object):
24     """Represents a list of filenames as returned by notmuch
25
26     This object contains the Filenames iterator. The main function is
27     as_generator() which will return a generator so we can do a Filenamesth an
28     iterator over a list of notmuch filenames. Do note that the underlying
29     library only provides a one-time iterator (it cannot reset the iterator to
30     the start). Thus iterating over the function will "exhaust" the list of
31     tags, and a subsequent iteration attempt will raise a :exc:`NotmuchError`
32     STATUS.NOT_INITIALIZED. Also note, that any function that uses iteration
33     (nearly all) will also exhaust the tags. So both::
34
35       for name in filenames: print name
36
37     as well as::
38
39        number_of_names = len(names)
40
41     and even a simple::
42
43        #str() iterates over all tags to construct a space separated list
44        print(str(filenames))
45
46     will "exhaust" the Filenames. However, you can use
47     :meth:`Message.get_filenames` repeatedly to get fresh Filenames
48     objects to perform various actions on filenames.
49     """
50
51     #notmuch_filenames_get
52     _get = nmlib.notmuch_filenames_get
53     _get.restype = c_char_p
54
55     def __init__(self, files_p, parent):
56         """
57         :param files_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 files_p: :class:`ctypes.c_void_p`
65         :param parent: The parent object (ie :class:`Message` these
66              filenames 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         """
70         if files_p is None:
71             raise NotmuchError(STATUS.NULL_POINTER)
72
73         self._files = files_p
74         #save reference to parent object so we keep it alive
75         self._parent = parent
76
77     def as_generator(self):
78         """Return generator of Filenames
79
80         This is the main function that will usually be used by the
81         user."""
82         if self._files is None:
83             raise NotmuchError(STATUS.NOT_INITIALIZED)
84
85         if not nmlib.notmuch_filenames_valid(self._files):
86             self._files = None
87             return
88
89         file = Filenames._get(self._files)
90         nmlib.notmuch_filenames_move_to_next(self._files)
91         yield file
92
93     def __str__(self):
94         """Represent Filenames() as newline-separated list of full paths
95
96         .. note:: As this iterates over the filenames, we will not be
97                able to iterate over them again (as in retrieve them)! If
98                the tags have been exhausted already, this will raise a
99                :exc:`NotmuchError` STATUS.NOT_INITIALIZED on subsequent
100                attempts. However, you can use
101                :meth:`Message.get_filenames` repeatedly to perform
102                various actions on filenames.
103         """
104         return "\n".join(self)
105
106     def __del__(self):
107         """Close and free the notmuch filenames"""
108         if self._files is not None:
109             nmlib.notmuch_filenames_destroy(self._files)