]> git.notmuchmail.org Git - notmuch/blob - bindings/python/notmuch/database.py
python: improve the docstring of Database.find_message_by_filename
[notmuch] / bindings / python / notmuch / database.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
20 import os
21 import codecs
22 from ctypes import c_char_p, c_void_p, c_uint, c_long, byref, POINTER
23 from notmuch.globals import (
24     nmlib,
25     STATUS,
26     FileError,
27     NotmuchError,
28     NullPointerError,
29     NotInitializedError,
30     ReadOnlyDatabaseError,
31     Enum,
32     _str,
33     NotmuchDatabaseP,
34     NotmuchDirectoryP,
35     NotmuchMessageP,
36     NotmuchTagsP,
37     NotmuchFilenamesP
38 )
39 from notmuch.message import Message
40 from notmuch.tag import Tags
41 from .query import Query
42
43 class Database(object):
44     """The :class:`Database` is the highest-level object that notmuch
45     provides. It references a notmuch database, and can be opened in
46     read-only or read-write mode. A :class:`Query` can be derived from
47     or be applied to a specific database to find messages. Also adding
48     and removing messages to the database happens via this
49     object. Modifications to the database are not atmic by default (see
50     :meth:`begin_atomic`) and once a database has been modified, all
51     other database objects pointing to the same data-base will throw an
52     :exc:`XapianError` as the underlying database has been
53     modified. Close and reopen the database to continue working with it.
54
55     :class:`Database` objects implement the context manager protocol
56     so you can use the :keyword:`with` statement to ensure that the
57     database is properly closed.
58
59     .. note::
60
61         Any function in this class can and will throw an
62         :exc:`NotInitializedError` if the database was not intitialized
63         properly.
64
65     .. note::
66
67         Do remember that as soon as we tear down (e.g. via `del db`) this
68         object, all underlying derived objects such as queries, threads,
69         messages, tags etc will be freed by the underlying library as well.
70         Accessing these objects will lead to segfaults and other unexpected
71         behavior. See above for more details.
72     """
73     _std_db_path = None
74     """Class attribute to cache user's default database"""
75
76     MODE = Enum(['READ_ONLY', 'READ_WRITE'])
77     """Constants: Mode in which to open the database"""
78
79     """notmuch_database_get_directory"""
80     _get_directory = nmlib.notmuch_database_get_directory
81     _get_directory.argtypes = [NotmuchDatabaseP, c_char_p]
82     _get_directory.restype = NotmuchDirectoryP
83
84     """notmuch_database_get_path"""
85     _get_path = nmlib.notmuch_database_get_path
86     _get_path.argtypes = [NotmuchDatabaseP]
87     _get_path.restype = c_char_p
88
89     """notmuch_database_get_version"""
90     _get_version = nmlib.notmuch_database_get_version
91     _get_version.argtypes = [NotmuchDatabaseP]
92     _get_version.restype = c_uint
93
94     """notmuch_database_open"""
95     _open = nmlib.notmuch_database_open
96     _open.argtypes = [c_char_p, c_uint]
97     _open.restype = NotmuchDatabaseP
98
99     """notmuch_database_upgrade"""
100     _upgrade = nmlib.notmuch_database_upgrade
101     _upgrade.argtypes = [NotmuchDatabaseP, c_void_p, c_void_p]
102     _upgrade.restype = c_uint
103
104     """ notmuch_database_find_message"""
105     _find_message = nmlib.notmuch_database_find_message
106     _find_message.argtypes = [NotmuchDatabaseP, c_char_p,
107                               POINTER(NotmuchMessageP)]
108     _find_message.restype = c_uint
109
110     """notmuch_database_find_message_by_filename"""
111     _find_message_by_filename = nmlib.notmuch_database_find_message_by_filename
112     _find_message_by_filename.argtypes = [NotmuchDatabaseP, c_char_p,
113                                           POINTER(NotmuchMessageP)]
114     _find_message_by_filename.restype = c_uint
115
116     """notmuch_database_get_all_tags"""
117     _get_all_tags = nmlib.notmuch_database_get_all_tags
118     _get_all_tags.argtypes = [NotmuchDatabaseP]
119     _get_all_tags.restype = NotmuchTagsP
120
121     """notmuch_database_create"""
122     _create = nmlib.notmuch_database_create
123     _create.argtypes = [c_char_p]
124     _create.restype = NotmuchDatabaseP
125
126     def __init__(self, path = None, create = False,
127                  mode = MODE.READ_ONLY):
128         """If *path* is `None`, we will try to read a users notmuch
129         configuration and use his configured database. The location of the
130         configuration file can be specified through the environment variable
131         *NOTMUCH_CONFIG*, falling back to the default `~/.notmuch-config`.
132
133         If *create* is `True`, the database will always be created in
134         :attr:`MODE`.READ_WRITE mode. Default mode for opening is READ_ONLY.
135
136         :param path:   Directory to open/create the database in (see
137                        above for behavior if `None`)
138         :type path:    `str` or `None`
139         :param create: Pass `False` to open an existing, `True` to create a new
140                        database.
141         :type create:  bool
142         :param mode:   Mode to open a database in. Is always
143                        :attr:`MODE`.READ_WRITE when creating a new one.
144         :type mode:    :attr:`MODE`
145         :raises: :exc:`NotmuchError` or derived exception in case of
146             failure.
147         """
148         self._db = None
149         self.mode = mode
150         if path is None:
151             # no path specified. use a user's default database
152             if Database._std_db_path is None:
153                 #the following line throws a NotmuchError if it fails
154                 Database._std_db_path = self._get_user_default_db()
155             path = Database._std_db_path
156
157         if create == False:
158             self.open(path, mode)
159         else:
160             self.create(path)
161
162     def __del__(self):
163         self.close()
164
165     def _assert_db_is_initialized(self):
166         """Raises :exc:`NotInitializedError` if self._db is `None`"""
167         if not self._db:
168             raise NotInitializedError()
169
170     def create(self, path):
171         """Creates a new notmuch database
172
173         This function is used by __init__() and usually does not need
174         to be called directly. It wraps the underlying
175         *notmuch_database_create* function and creates a new notmuch
176         database at *path*. It will always return a database in :attr:`MODE`
177         .READ_WRITE mode as creating an empty database for
178         reading only does not make a great deal of sense.
179
180         :param path: A directory in which we should create the database.
181         :type path: str
182         :raises: :exc:`NotmuchError` in case of any failure
183                     (possibly after printing an error message on stderr).
184         """
185         if self._db is not None:
186             raise NotmuchError(message="Cannot create db, this Database() "
187                                        "already has an open one.")
188
189         res = Database._create(_str(path), Database.MODE.READ_WRITE)
190
191         if not res:
192             raise NotmuchError(
193                 message="Could not create the specified database")
194         self._db = res
195
196     def open(self, path, mode=0):
197         """Opens an existing database
198
199         This function is used by __init__() and usually does not need
200         to be called directly. It wraps the underlying
201         *notmuch_database_open* function.
202
203         :param status: Open the database in read-only or read-write mode
204         :type status:  :attr:`MODE`
205         :raises: Raises :exc:`NotmuchError` in case of any failure
206                     (possibly after printing an error message on stderr).
207         """
208         res = Database._open(_str(path), mode)
209
210         if not res:
211             raise NotmuchError(message="Could not open the specified database")
212         self._db = res
213
214     _close = nmlib.notmuch_database_close
215     _close.argtypes = [NotmuchDatabaseP]
216     _close.restype = None
217
218     def close(self):
219         """Close and free the notmuch database if needed"""
220         if self._db is not None:
221             self._close(self._db)
222             self._db = None
223
224     def __enter__(self):
225         '''
226         Implements the context manager protocol.
227         '''
228         return self
229
230     def __exit__(self, exc_type, exc_value, traceback):
231         '''
232         Implements the context manager protocol.
233         '''
234         self.close()
235
236     def get_path(self):
237         """Returns the file path of an open database"""
238         self._assert_db_is_initialized()
239         return Database._get_path(self._db).decode('utf-8')
240
241     def get_version(self):
242         """Returns the database format version
243
244         :returns: The database version as positive integer
245         """
246         self._assert_db_is_initialized()
247         return Database._get_version(self._db)
248
249     _needs_upgrade = nmlib.notmuch_database_needs_upgrade
250     _needs_upgrade.argtypes = [NotmuchDatabaseP]
251     _needs_upgrade.restype = bool
252
253     def needs_upgrade(self):
254         """Does this database need to be upgraded before writing to it?
255
256         If this function returns `True` then no functions that modify the
257         database (:meth:`add_message`,
258         :meth:`Message.add_tag`, :meth:`Directory.set_mtime`,
259         etc.) will work unless :meth:`upgrade` is called successfully first.
260
261         :returns: `True` or `False`
262         """
263         self._assert_db_is_initialized()
264         return self._needs_upgrade(self._db)
265
266     def upgrade(self):
267         """Upgrades the current database
268
269         After opening a database in read-write mode, the client should
270         check if an upgrade is needed (notmuch_database_needs_upgrade) and
271         if so, upgrade with this function before making any modifications.
272
273         NOT IMPLEMENTED: The optional progress_notify callback can be
274         used by the caller to provide progress indication to the
275         user. If non-NULL it will be called periodically with
276         'progress' as a floating-point value in the range of [0.0..1.0]
277         indicating the progress made so far in the upgrade process.
278
279         :TODO: catch exceptions, document return values and etc...
280         """
281         self._assert_db_is_initialized()
282         status = Database._upgrade(self._db, None, None)
283         #TODO: catch exceptions, document return values and etc
284         return status
285
286     _begin_atomic = nmlib.notmuch_database_begin_atomic
287     _begin_atomic.argtypes = [NotmuchDatabaseP]
288     _begin_atomic.restype = c_uint
289
290     def begin_atomic(self):
291         """Begin an atomic database operation
292
293         Any modifications performed between a successful
294         :meth:`begin_atomic` and a :meth:`end_atomic` will be applied to
295         the database atomically.  Note that, unlike a typical database
296         transaction, this only ensures atomicity, not durability;
297         neither begin nor end necessarily flush modifications to disk.
298
299         :returns: :attr:`STATUS`.SUCCESS or raises
300         :raises: :exc:`NotmuchError`: :attr:`STATUS`.XAPIAN_EXCEPTION
301                     Xapian exception occurred; atomic section not entered.
302
303         *Added in notmuch 0.9*"""
304         self._assert_db_is_initialized()
305         status = self._begin_atomic(self._db)
306         if status != STATUS.SUCCESS:
307             raise NotmuchError(status)
308         return status
309
310     _end_atomic = nmlib.notmuch_database_end_atomic
311     _end_atomic.argtypes = [NotmuchDatabaseP]
312     _end_atomic.restype = c_uint
313
314     def end_atomic(self):
315         """Indicate the end of an atomic database operation
316
317         See :meth:`begin_atomic` for details.
318
319         :returns: :attr:`STATUS`.SUCCESS or raises
320
321         :raises:
322             :exc:`NotmuchError`:
323                 :attr:`STATUS`.XAPIAN_EXCEPTION
324                     A Xapian exception occurred; atomic section not
325                     ended.
326                 :attr:`STATUS`.UNBALANCED_ATOMIC:
327                     end_atomic has been called more times than begin_atomic.
328
329         *Added in notmuch 0.9*"""
330         self._assert_db_is_initialized()
331         status = self._end_atomic(self._db)
332         if status != STATUS.SUCCESS:
333             raise NotmuchError(status)
334         return status
335
336     def get_directory(self, path):
337         """Returns a :class:`Directory` of path,
338         (creating it if it does not exist(?))
339
340         :param path: An unicode string containing the path relative to the path
341               of database (see :meth:`get_path`), or else should be an absolute
342               path with initial components that match the path of 'database'.
343         :returns: :class:`Directory` or raises an exception.
344         :raises: :exc:`FileError` if path is not relative database or absolute
345                  with initial components same as database.
346         :raises: :exc:`ReadOnlyDatabaseError` if the database has not been
347                  opened in read-write mode
348         """
349         self._assert_db_is_initialized()
350
351         # work around libnotmuch calling exit(3), see
352         # id:20120221002921.8534.57091@thinkbox.jade-hamburg.de
353         # TODO: remove once this issue is resolved
354         if self.mode != Database.MODE.READ_WRITE:
355             raise ReadOnlyDatabaseError('The database has to be opened in '
356                                         'read-write mode for get_directory')
357
358         # sanity checking if path is valid, and make path absolute
359         if path and path[0] == os.sep:
360             # we got an absolute path
361             if not path.startswith(self.get_path()):
362                 # but its initial components are not equal to the db path
363                 raise FileError('Database().get_directory() called '
364                                 'with a wrong absolute path')
365             abs_dirpath = path
366         else:
367             #we got a relative path, make it absolute
368             abs_dirpath = os.path.abspath(os.path.join(self.get_path(), path))
369
370         dir_p = Database._get_directory(self._db, _str(path))
371
372         # return the Directory, init it with the absolute path
373         return Directory(abs_dirpath, dir_p, self)
374
375     _add_message = nmlib.notmuch_database_add_message
376     _add_message.argtypes = [NotmuchDatabaseP, c_char_p,
377                              POINTER(NotmuchMessageP)]
378     _add_message.restype = c_uint
379
380     def add_message(self, filename, sync_maildir_flags=False):
381         """Adds a new message to the database
382
383         :param filename: should be a path relative to the path of the
384             open database (see :meth:`get_path`), or else should be an
385             absolute filename with initial components that match the
386             path of the database.
387
388             The file should be a single mail message (not a
389             multi-message mbox) that is expected to remain at its
390             current location, since the notmuch database will reference
391             the filename, and will not copy the entire contents of the
392             file.
393
394         :param sync_maildir_flags: If the message contains Maildir
395             flags, we will -depending on the notmuch configuration- sync
396             those tags to initial notmuch tags, if set to `True`. It is
397             `False` by default to remain consistent with the libnotmuch
398             API. You might want to look into the underlying method
399             :meth:`Message.maildir_flags_to_tags`.
400
401         :returns: On success, we return
402
403            1) a :class:`Message` object that can be used for things
404               such as adding tags to the just-added message.
405            2) one of the following :attr:`STATUS` values:
406
407               :attr:`STATUS`.SUCCESS
408                   Message successfully added to database.
409               :attr:`STATUS`.DUPLICATE_MESSAGE_ID
410                   Message has the same message ID as another message already
411                   in the database. The new filename was successfully added
412                   to the list of the filenames for the existing message.
413
414         :rtype:   2-tuple(:class:`Message`, :attr:`STATUS`)
415
416         :raises: Raises a :exc:`NotmuchError` with the following meaning.
417               If such an exception occurs, nothing was added to the database.
418
419               :attr:`STATUS`.FILE_ERROR
420                       An error occurred trying to open the file, (such as
421                       permission denied, or file not found, etc.).
422               :attr:`STATUS`.FILE_NOT_EMAIL
423                       The contents of filename don't look like an email
424                       message.
425               :attr:`STATUS`.READ_ONLY_DATABASE
426                       Database was opened in read-only mode so no message can
427                       be added.
428         """
429         self._assert_db_is_initialized()
430         msg_p = NotmuchMessageP()
431         status = self._add_message(self._db, _str(filename), byref(msg_p))
432
433         if not status in [STATUS.SUCCESS, STATUS.DUPLICATE_MESSAGE_ID]:
434             raise NotmuchError(status)
435
436         #construct Message() and return
437         msg = Message(msg_p, self)
438         #automatic sync initial tags from Maildir flags
439         if sync_maildir_flags:
440             msg.maildir_flags_to_tags()
441         return (msg, status)
442
443     _remove_message = nmlib.notmuch_database_remove_message
444     _remove_message.argtypes = [NotmuchDatabaseP, c_char_p]
445     _remove_message.restype = c_uint
446
447     def remove_message(self, filename):
448         """Removes a message (filename) from the given notmuch database
449
450         Note that only this particular filename association is removed from
451         the database. If the same message (as determined by the message ID)
452         is still available via other filenames, then the message will
453         persist in the database for those filenames. When the last filename
454         is removed for a particular message, the database content for that
455         message will be entirely removed.
456
457         :returns: A :attr:`STATUS` value with the following meaning:
458
459              :attr:`STATUS`.SUCCESS
460                The last filename was removed and the message was removed
461                from the database.
462              :attr:`STATUS`.DUPLICATE_MESSAGE_ID
463                This filename was removed but the message persists in the
464                database with at least one other filename.
465
466         :raises: Raises a :exc:`NotmuchError` with the following meaning.
467              If such an exception occurs, nothing was removed from the
468              database.
469
470              :attr:`STATUS`.READ_ONLY_DATABASE
471                Database was opened in read-only mode so no message can be
472                removed.
473         """
474         self._assert_db_is_initialized()
475         return self._remove_message(self._db, _str(filename))
476
477     def find_message(self, msgid):
478         """Returns a :class:`Message` as identified by its message ID
479
480         Wraps the underlying *notmuch_database_find_message* function.
481
482         :param msgid: The message ID
483         :type msgid: unicode or str
484         :returns: :class:`Message` or `None` if no message is found.
485         :raises:
486             :exc:`OutOfMemoryError`
487                   If an Out-of-memory occured while constructing the message.
488             :exc:`XapianError`
489                   In case of a Xapian Exception. These exceptions
490                   include "Database modified" situations, e.g. when the
491                   notmuch database has been modified by another program
492                   in the meantime. In this case, you should close and
493                   reopen the database and retry.
494             :exc:`NotInitializedError` if
495                     the database was not intitialized.
496         """
497         self._assert_db_is_initialized()
498         msg_p = NotmuchMessageP()
499         status = Database._find_message(self._db, _str(msgid), byref(msg_p))
500         if status != STATUS.SUCCESS:
501             raise NotmuchError(status)
502         return msg_p and Message(msg_p, self) or None
503
504     def find_message_by_filename(self, filename):
505         """Find a message with the given filename
506
507         .. warning::
508
509             This call needs a writeable database in
510             :attr:`Database.MODE`.READ_WRITE mode. The underlying library will
511             exit the program if this method is used on a read-only database!
512
513         :returns: If the database contains a message with the given
514             filename, then a class:`Message:` is returned.  This
515             function returns None if no message is found with the given
516             filename.
517
518         :raises: :exc:`OutOfMemoryError` if an Out-of-memory occured while
519                  constructing the message.
520         :raises: :exc:`XapianError` in case of a Xapian Exception.
521                  These exceptions include "Database modified"
522                  situations, e.g. when the notmuch database has been
523                  modified by another program in the meantime. In this
524                  case, you should close and reopen the database and
525                  retry.
526         :raises: :exc:`NotInitializedError` if the database was not
527                  intitialized.
528
529         *Added in notmuch 0.9*"""
530         self._assert_db_is_initialized()
531         msg_p = NotmuchMessageP()
532         status = Database._find_message_by_filename(self._db, _str(filename),
533                                                     byref(msg_p))
534         if status != STATUS.SUCCESS:
535             raise NotmuchError(status)
536         return msg_p and Message(msg_p, self) or None
537
538     def get_all_tags(self):
539         """Returns :class:`Tags` with a list of all tags found in the database
540
541         :returns: :class:`Tags`
542         :execption: :exc:`NotmuchError` with :attr:`STATUS`.NULL_POINTER
543                     on error
544         """
545         self._assert_db_is_initialized()
546         tags_p = Database._get_all_tags(self._db)
547         if tags_p == None:
548             raise NullPointerError()
549         return Tags(tags_p, self)
550
551     def create_query(self, querystring):
552         """Returns a :class:`Query` derived from this database
553
554         This is a shorthand method for doing::
555
556           # short version
557           # Automatically frees the Database() when 'q' is deleted
558
559           q  = Database(dbpath).create_query('from:"Biene Maja"')
560
561           # long version, which is functionally equivalent but will keep the
562           # Database in the 'db' variable around after we delete 'q':
563
564           db = Database(dbpath)
565           q  = Query(db,'from:"Biene Maja"')
566
567         This function is a python extension and not in the underlying C API.
568         """
569         return Query(self, querystring)
570
571     def __repr__(self):
572         return "'Notmuch DB " + self.get_path() + "'"
573
574     def _get_user_default_db(self):
575         """ Reads a user's notmuch config and returns his db location
576
577         Throws a NotmuchError if it cannot find it"""
578         try:
579             # python3.x
580             from configparser import SafeConfigParser
581         except ImportError:
582             # python2.x
583             from ConfigParser import SafeConfigParser
584
585         config = SafeConfigParser()
586         conf_f = os.getenv('NOTMUCH_CONFIG',
587                            os.path.expanduser('~/.notmuch-config'))
588         config.readfp(codecs.open(conf_f, 'r', 'utf-8'))
589         if not config.has_option('database', 'path'):
590             raise NotmuchError(message="No DB path specified"
591                                        " and no user default found")
592         return config.get('database', 'path')
593
594     @property
595     def db_p(self):
596         """Property returning a pointer to `notmuch_database_t` or `None`
597
598         This should normally not be needed by a user (and is not yet
599         guaranteed to remain stable in future versions).
600         """
601         return self._db
602
603
604 class Directory(object):
605     """Represents a directory entry in the notmuch directory
606
607     Modifying attributes of this object will modify the
608     database, not the real directory attributes.
609
610     The Directory object is usually derived from another object
611     e.g. via :meth:`Database.get_directory`, and will automatically be
612     become invalid whenever that parent is deleted. You should
613     therefore initialized this object handing it a reference to the
614     parent, preventing the parent from automatically being garbage
615     collected.
616     """
617
618     """notmuch_directory_get_mtime"""
619     _get_mtime = nmlib.notmuch_directory_get_mtime
620     _get_mtime.argtypes = [NotmuchDirectoryP]
621     _get_mtime.restype = c_long
622
623     """notmuch_directory_set_mtime"""
624     _set_mtime = nmlib.notmuch_directory_set_mtime
625     _set_mtime.argtypes = [NotmuchDirectoryP, c_long]
626     _set_mtime.restype = c_uint
627
628     """notmuch_directory_get_child_files"""
629     _get_child_files = nmlib.notmuch_directory_get_child_files
630     _get_child_files.argtypes = [NotmuchDirectoryP]
631     _get_child_files.restype = NotmuchFilenamesP
632
633     """notmuch_directory_get_child_directories"""
634     _get_child_directories = nmlib.notmuch_directory_get_child_directories
635     _get_child_directories.argtypes = [NotmuchDirectoryP]
636     _get_child_directories.restype = NotmuchFilenamesP
637
638     def _assert_dir_is_initialized(self):
639         """Raises a NotmuchError(:attr:`STATUS`.NOT_INITIALIZED)
640         if dir_p is None"""
641         if not self._dir_p:
642             raise NotInitializedError()
643
644     def __init__(self, path, dir_p, parent):
645         """
646         :param path:   The absolute path of the directory object.
647         :param dir_p:  The pointer to an internal notmuch_directory_t object.
648         :param parent: The object this Directory is derived from
649                        (usually a :class:`Database`). We do not directly use
650                        this, but store a reference to it as long as
651                        this Directory object lives. This keeps the
652                        parent object alive.
653         """
654         self._path = path
655         self._dir_p = dir_p
656         self._parent = parent
657
658     def set_mtime(self, mtime):
659         """Sets the mtime value of this directory in the database
660
661         The intention is for the caller to use the mtime to allow efficient
662         identification of new messages to be added to the database. The
663         recommended usage is as follows:
664
665         * Read the mtime of a directory from the filesystem
666
667         * Call :meth:`Database.add_message` for all mail files in
668           the directory
669
670         * Call notmuch_directory_set_mtime with the mtime read from the
671           filesystem.  Then, when wanting to check for updates to the
672           directory in the future, the client can call :meth:`get_mtime`
673           and know that it only needs to add files if the mtime of the
674           directory and files are newer than the stored timestamp.
675
676           .. note::
677
678                 :meth:`get_mtime` function does not allow the caller to
679                 distinguish a timestamp of 0 from a non-existent timestamp. So
680                 don't store a timestamp of 0 unless you are comfortable with
681                 that.
682
683         :param mtime: A (time_t) timestamp
684         :raises: :exc:`XapianError` a Xapian exception occurred, mtime
685                  not stored
686         :raises: :exc:`ReadOnlyDatabaseError` the database was opened
687                  in read-only mode so directory mtime cannot be modified
688         :raises: :exc:`NotInitializedError` the directory object has not
689                  been initialized
690         """
691         self._assert_dir_is_initialized()
692         status = Directory._set_mtime(self._dir_p, mtime)
693
694         if status != STATUS.SUCCESS:
695             raise NotmuchError(status)
696
697     def get_mtime(self):
698         """Gets the mtime value of this directory in the database
699
700         Retrieves a previously stored mtime for this directory.
701
702         :param mtime: A (time_t) timestamp
703         :raises: :exc:`NotmuchError`:
704
705                         :attr:`STATUS`.NOT_INITIALIZED
706                           The directory has not been initialized
707         """
708         self._assert_dir_is_initialized()
709         return Directory._get_mtime(self._dir_p)
710
711     # Make mtime attribute a property of Directory()
712     mtime = property(get_mtime, set_mtime, doc="""Property that allows getting
713                      and setting of the Directory *mtime* (read-write)
714
715                      See :meth:`get_mtime` and :meth:`set_mtime` for usage and
716                      possible exceptions.""")
717
718     def get_child_files(self):
719         """Gets a Filenames iterator listing all the filenames of
720         messages in the database within the given directory.
721
722         The returned filenames will be the basename-entries only (not
723         complete paths.
724         """
725         self._assert_dir_is_initialized()
726         files_p = Directory._get_child_files(self._dir_p)
727         return Filenames(files_p, self)
728
729     def get_child_directories(self):
730         """Gets a :class:`Filenames` iterator listing all the filenames of
731         sub-directories in the database within the given directory
732
733         The returned filenames will be the basename-entries only (not
734         complete paths.
735         """
736         self._assert_dir_is_initialized()
737         files_p = Directory._get_child_directories(self._dir_p)
738         return Filenames(files_p, self)
739
740     @property
741     def path(self):
742         """Returns the absolute path of this Directory (read-only)"""
743         return self._path
744
745     def __repr__(self):
746         """Object representation"""
747         return "<notmuch Directory object '%s'>" % self._path
748
749     _destroy = nmlib.notmuch_directory_destroy
750     _destroy.argtypes = [NotmuchDirectoryP]
751     _destroy.argtypes = None
752
753     def __del__(self):
754         """Close and free the Directory"""
755         if self._dir_p is not None:
756             self._destroy(self._dir_p)
757
758
759 class Filenames(object):
760     """An iterator over File- or Directory names stored in the database"""
761
762     #notmuch_filenames_get
763     _get = nmlib.notmuch_filenames_get
764     _get.argtypes = [NotmuchFilenamesP]
765     _get.restype = c_char_p
766
767     def __init__(self, files_p, parent):
768         """
769         :param files_p: The pointer to an internal notmuch_filenames_t object.
770         :param parent: The object this Directory is derived from
771                        (usually a Directory()). We do not directly use
772                        this, but store a reference to it as long as
773                        this Directory object lives. This keeps the
774                        parent object alive.
775         """
776         self._files_p = files_p
777         self._parent = parent
778
779     def __iter__(self):
780         """ Make Filenames an iterator """
781         return self
782
783     _valid = nmlib.notmuch_filenames_valid
784     _valid.argtypes = [NotmuchFilenamesP]
785     _valid.restype = bool
786
787     _move_to_next = nmlib.notmuch_filenames_move_to_next
788     _move_to_next.argtypes = [NotmuchFilenamesP]
789     _move_to_next.restype = None
790
791     def __next__(self):
792         if not self._files_p:
793             raise NotInitializedError()
794
795         if not self._valid(self._files_p):
796             self._files_p = None
797             raise StopIteration
798
799         file_ = Filenames._get(self._files_p)
800         self._move_to_next(self._files_p)
801         return file_.decode('utf-8', 'ignore')
802     next = __next__ # python2.x iterator protocol compatibility
803
804     def __len__(self):
805         """len(:class:`Filenames`) returns the number of contained files
806
807         .. note::
808
809             As this iterates over the files, we will not be able to
810             iterate over them again! So this will fail::
811
812                  #THIS FAILS
813                  files = Database().get_directory('').get_child_files()
814                  if len(files) > 0:  # this 'exhausts' msgs
815                      # next line raises
816                      # NotmuchError(:attr:`STATUS`.NOT_INITIALIZED)
817                      for file in files: print file
818         """
819         if not self._files_p:
820             raise NotInitializedError()
821
822         i = 0
823         while self._valid(self._files_p):
824             self._move_to_next(self._files_p)
825             i += 1
826         self._files_p = None
827         return i
828
829     _destroy = nmlib.notmuch_filenames_destroy
830     _destroy.argtypes = [NotmuchFilenamesP]
831     _destroy.restype = None
832
833     def __del__(self):
834         """Close and free Filenames"""
835         if self._files_p is not None:
836             self._destroy(self._files_p)