]> git.notmuchmail.org Git - notmuch/blobdiff - bindings/python/notmuch/database.py
notmuch.Database.remove_message should raise exception on failure
[notmuch] / bindings / python / notmuch / database.py
index 800264b7bd157f0e890714e21fdf7741b63f33d5..f30453345e0dabd6a1cb4fa81596c50bdac65143 100644 (file)
@@ -14,20 +14,15 @@ for more details.
 You should have received a copy of the GNU General Public License
 along with notmuch.  If not, see <http://www.gnu.org/licenses/>.
 
-Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
+Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>
 """
 
 import os
 import codecs
 from ctypes import c_char_p, c_void_p, c_uint, byref, POINTER
-from notmuch.globals import (
+from .compat import SafeConfigParser
+from .globals import (
     nmlib,
-    STATUS,
-    FileError,
-    NotmuchError,
-    NullPointerError,
-    NotInitializedError,
-    ReadOnlyDatabaseError,
     Enum,
     _str,
     NotmuchDatabaseP,
@@ -35,8 +30,15 @@ from notmuch.globals import (
     NotmuchMessageP,
     NotmuchTagsP,
 )
-from notmuch.message import Message
-from notmuch.tag import Tags
+from .errors import (
+    STATUS,
+    FileError,
+    NotmuchError,
+    NullPointerError,
+    NotInitializedError,
+)
+from .message import Message
+from .tag import Tags
 from .query import Query
 from .directory import Directory
 
@@ -54,21 +56,14 @@ class Database(object):
 
     :class:`Database` objects implement the context manager protocol
     so you can use the :keyword:`with` statement to ensure that the
-    database is properly closed.
+    database is properly closed. See :meth:`close` for more
+    information.
 
     .. note::
 
         Any function in this class can and will throw an
         :exc:`NotInitializedError` if the database was not intitialized
         properly.
-
-    .. note::
-
-        Do remember that as soon as we tear down (e.g. via `del db`) this
-        object, all underlying derived objects such as queries, threads,
-        messages, tags etc will be freed by the underlying library as well.
-        Accessing these objects will lead to segfaults and other unexpected
-        behavior. See above for more details.
     """
     _std_db_path = None
     """Class attribute to cache user's default database"""
@@ -78,8 +73,8 @@ class Database(object):
 
     """notmuch_database_get_directory"""
     _get_directory = nmlib.notmuch_database_get_directory
-    _get_directory.argtypes = [NotmuchDatabaseP, c_char_p]
-    _get_directory.restype = NotmuchDirectoryP
+    _get_directory.argtypes = [NotmuchDatabaseP, c_char_p, POINTER(NotmuchDirectoryP)]
+    _get_directory.restype = c_uint
 
     """notmuch_database_get_path"""
     _get_path = nmlib.notmuch_database_get_path
@@ -93,8 +88,8 @@ class Database(object):
 
     """notmuch_database_open"""
     _open = nmlib.notmuch_database_open
-    _open.argtypes = [c_char_p, c_uint]
-    _open.restype = NotmuchDatabaseP
+    _open.argtypes = [c_char_p, c_uint, POINTER(NotmuchDatabaseP)]
+    _open.restype = c_uint
 
     """notmuch_database_upgrade"""
     _upgrade = nmlib.notmuch_database_upgrade
@@ -120,8 +115,8 @@ class Database(object):
 
     """notmuch_database_create"""
     _create = nmlib.notmuch_database_create
-    _create.argtypes = [c_char_p]
-    _create.restype = NotmuchDatabaseP
+    _create.argtypes = [c_char_p, POINTER(NotmuchDatabaseP)]
+    _create.restype = c_uint
 
     def __init__(self, path = None, create = False,
                  mode = MODE.READ_ONLY):
@@ -159,8 +154,15 @@ class Database(object):
         else:
             self.create(path)
 
+    _destroy = nmlib.notmuch_database_destroy
+    _destroy.argtypes = [NotmuchDatabaseP]
+    _destroy.restype = c_uint
+
     def __del__(self):
-        self.close()
+        if self._db:
+            status = self._destroy(self._db)
+            if status != STATUS.SUCCESS:
+                raise NotmuchError(status)
 
     def _assert_db_is_initialized(self):
         """Raises :exc:`NotInitializedError` if self._db is `None`"""
@@ -182,16 +184,17 @@ class Database(object):
         :raises: :exc:`NotmuchError` in case of any failure
                     (possibly after printing an error message on stderr).
         """
-        if self._db is not None:
+        if self._db:
             raise NotmuchError(message="Cannot create db, this Database() "
                                        "already has an open one.")
 
-        res = Database._create(_str(path), Database.MODE.READ_WRITE)
+        db = NotmuchDatabaseP()
+        status = Database._create(_str(path), byref(db))
 
-        if not res:
-            raise NotmuchError(
-                message="Could not create the specified database")
-        self._db = res
+        if status != STATUS.SUCCESS:
+            raise NotmuchError(status)
+        self._db = db
+        return status
 
     def open(self, path, mode=0):
         """Opens an existing database
@@ -205,21 +208,33 @@ class Database(object):
         :raises: Raises :exc:`NotmuchError` in case of any failure
                     (possibly after printing an error message on stderr).
         """
-        res = Database._open(_str(path), mode)
+        db = NotmuchDatabaseP()
+        status = Database._open(_str(path), mode, byref(db))
 
-        if not res:
-            raise NotmuchError(message="Could not open the specified database")
-        self._db = res
+        if status != STATUS.SUCCESS:
+            raise NotmuchError(status)
+        self._db = db
+        return status
 
     _close = nmlib.notmuch_database_close
     _close.argtypes = [NotmuchDatabaseP]
-    _close.restype = None
+    _close.restype = c_uint
 
     def close(self):
-        """Close and free the notmuch database if needed"""
-        if self._db is not None:
-            self._close(self._db)
-            self._db = None
+        '''
+        Closes the notmuch database.
+
+        .. warning::
+
+            This function closes the notmuch database. From that point
+            on every method invoked on any object ever derived from
+            the closed database may cease to function and raise a
+            NotmuchError.
+        '''
+        if self._db:
+            status = self._close(self._db)
+            if status != STATUS.SUCCESS:
+                raise NotmuchError(status)
 
     def __enter__(self):
         '''
@@ -335,7 +350,6 @@ class Database(object):
 
     def get_directory(self, path):
         """Returns a :class:`Directory` of path,
-        (creating it if it does not exist(?))
 
         :param path: An unicode string containing the path relative to the path
               of database (see :meth:`get_path`), or else should be an absolute
@@ -343,18 +357,9 @@ class Database(object):
         :returns: :class:`Directory` or raises an exception.
         :raises: :exc:`FileError` if path is not relative database or absolute
                  with initial components same as database.
-        :raises: :exc:`ReadOnlyDatabaseError` if the database has not been
-                 opened in read-write mode
         """
         self._assert_db_is_initialized()
 
-        # work around libnotmuch calling exit(3), see
-        # id:20120221002921.8534.57091@thinkbox.jade-hamburg.de
-        # TODO: remove once this issue is resolved
-        if self.mode != Database.MODE.READ_WRITE:
-            raise ReadOnlyDatabaseError('The database has to be opened in '
-                                        'read-write mode for get_directory')
-
         # sanity checking if path is valid, and make path absolute
         if path and path[0] == os.sep:
             # we got an absolute path
@@ -367,7 +372,13 @@ class Database(object):
             #we got a relative path, make it absolute
             abs_dirpath = os.path.abspath(os.path.join(self.get_path(), path))
 
-        dir_p = Database._get_directory(self._db, _str(path))
+        dir_p = NotmuchDirectoryP()
+        status = Database._get_directory(self._db, _str(path), byref(dir_p))
+
+        if status != STATUS.SUCCESS:
+            raise NotmuchError(status)
+        if not dir_p:
+            return None
 
         # return the Directory, init it with the absolute path
         return Directory(abs_dirpath, dir_p, self)
@@ -472,7 +483,10 @@ class Database(object):
                removed.
         """
         self._assert_db_is_initialized()
-        return self._remove_message(self._db, _str(filename))
+        status = self._remove_message(self._db, _str(filename))
+        if status not in [STATUS.SUCCESS, STATUS.DUPLICATE_MESSAGE_ID]:
+            raise NotmuchError(status)
+        return status
 
     def find_message(self, msgid):
         """Returns a :class:`Message` as identified by its message ID
@@ -519,19 +533,10 @@ class Database(object):
                  retry.
         :raises: :exc:`NotInitializedError` if the database was not
                  intitialized.
-        :raises: :exc:`ReadOnlyDatabaseError` if the database has not been
-                 opened in read-write mode
 
         *Added in notmuch 0.9*"""
         self._assert_db_is_initialized()
 
-        # work around libnotmuch calling exit(3), see
-        # id:20120221002921.8534.57091@thinkbox.jade-hamburg.de
-        # TODO: remove once this issue is resolved
-        if self.mode != Database.MODE.READ_WRITE:
-            raise ReadOnlyDatabaseError('The database has to be opened in '
-                                        'read-write mode for get_directory')
-
         msg_p = NotmuchMessageP()
         status = Database._find_message_by_filename(self._db, _str(filename),
                                                     byref(msg_p))
@@ -548,7 +553,7 @@ class Database(object):
         """
         self._assert_db_is_initialized()
         tags_p = Database._get_all_tags(self._db)
-        if tags_p == None:
+        if not tags_p:
             raise NullPointerError()
         return Tags(tags_p, self)
 
@@ -572,6 +577,22 @@ class Database(object):
         """
         return Query(self, querystring)
 
+    """notmuch_database_status_string"""
+    _status_string = nmlib.notmuch_database_status_string
+    _status_string.argtypes = [NotmuchDatabaseP]
+    _status_string.restype = c_char_p
+
+    def status_string(self):
+        """Returns the status string of the database
+
+        This is sometimes used for additional error reporting
+        """
+        self._assert_db_is_initialized()
+        s = Database._status_string(self._db)
+        if s:
+            return s.decode('utf-8', 'ignore')
+        return s
+
     def __repr__(self):
         return "'Notmuch DB " + self.get_path() + "'"
 
@@ -579,13 +600,6 @@ class Database(object):
         """ Reads a user's notmuch config and returns his db location
 
         Throws a NotmuchError if it cannot find it"""
-        try:
-            # python3.x
-            from configparser import SafeConfigParser
-        except ImportError:
-            # python2.x
-            from ConfigParser import SafeConfigParser
-
         config = SafeConfigParser()
         conf_f = os.getenv('NOTMUCH_CONFIG',
                            os.path.expanduser('~/.notmuch-config'))
@@ -594,12 +608,3 @@ class Database(object):
             raise NotmuchError(message="No DB path specified"
                                        " and no user default found")
         return config.get('database', 'path')
-
-    @property
-    def db_p(self):
-        """Property returning a pointer to `notmuch_database_t` or `None`
-
-        This should normally not be needed by a user (and is not yet
-        guaranteed to remain stable in future versions).
-        """
-        return self._db