]> 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 ee0366cd0c846ce848be42c1a349e38be68d7b81..f30453345e0dabd6a1cb4fa81596c50bdac65143 100644 (file)
@@ -36,7 +36,6 @@ from .errors import (
     NotmuchError,
     NullPointerError,
     NotInitializedError,
-    ReadOnlyDatabaseError,
 )
 from .message import Message
 from .tag import Tags
@@ -157,11 +156,13 @@ class Database(object):
 
     _destroy = nmlib.notmuch_database_destroy
     _destroy.argtypes = [NotmuchDatabaseP]
-    _destroy.restype = None
+    _destroy.restype = c_uint
 
     def __del__(self):
         if self._db:
-            self._destroy(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`"""
@@ -188,7 +189,7 @@ class Database(object):
                                        "already has an open one.")
 
         db = NotmuchDatabaseP()
-        status = Database._create(_str(path), Database.MODE.READ_WRITE, byref(db))
+        status = Database._create(_str(path), byref(db))
 
         if status != STATUS.SUCCESS:
             raise NotmuchError(status)
@@ -217,7 +218,7 @@ class Database(object):
 
     _close = nmlib.notmuch_database_close
     _close.argtypes = [NotmuchDatabaseP]
-    _close.restype = None
+    _close.restype = c_uint
 
     def close(self):
         '''
@@ -231,7 +232,9 @@ class Database(object):
             NotmuchError.
         '''
         if self._db:
-            self._close(self._db)
+            status = self._close(self._db)
+            if status != STATUS.SUCCESS:
+                raise NotmuchError(status)
 
     def __enter__(self):
         '''
@@ -347,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
@@ -355,8 +357,6 @@ 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()
 
@@ -483,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
@@ -530,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))
@@ -583,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() + "'"
 
@@ -598,14 +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`
-
-        .. deprecated:: 0.14
-                        If you really need a pointer to the notmuch
-                        database object use the `_pointer` field. This
-                        alias will be removed in notmuch 0.15.
-        """
-        return self._db