X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=bindings%2Fpython%2Fnotmuch%2Fdatabase.py;h=1279804a1c81eab28018e663f0b543085c0ce250;hb=040c3236afcf95bead0324a48c2e0b9cd7934993;hp=e5c74cfb164557e53914559f758712b491a3d78d;hpb=54508eb78d1c5c1c67c8b220cf2bd826af1203a9;p=notmuch diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index e5c74cfb..1279804a 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -12,15 +12,17 @@ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License -along with notmuch. If not, see . +along with notmuch. If not, see . Copyright 2010 Sebastian Spaeth """ import os import codecs +import warnings 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, Enum, _str, @@ -35,10 +37,9 @@ from .errors import ( NotmuchError, NullPointerError, NotInitializedError, - ReadOnlyDatabaseError, ) -from notmuch.message import Message -from notmuch.tag import Tags +from .message import Message +from .tag import Tags from .query import Query from .directory import Directory @@ -86,6 +87,11 @@ class Database(object): _get_version.argtypes = [NotmuchDatabaseP] _get_version.restype = c_uint + """notmuch_database_get_revision""" + _get_revision = nmlib.notmuch_database_get_revision + _get_revision.argtypes = [NotmuchDatabaseP, POINTER(c_char_p)] + _get_revision.restype = c_uint + """notmuch_database_open""" _open = nmlib.notmuch_database_open _open.argtypes = [c_char_p, c_uint, POINTER(NotmuchDatabaseP)] @@ -156,11 +162,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`""" @@ -187,7 +195,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) @@ -216,7 +224,7 @@ class Database(object): _close = nmlib.notmuch_database_close _close.argtypes = [NotmuchDatabaseP] - _close.restype = None + _close.restype = c_uint def close(self): ''' @@ -230,7 +238,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): ''' @@ -257,6 +267,17 @@ class Database(object): self._assert_db_is_initialized() return Database._get_version(self._db) + def get_revision (self): + """Returns the committed database revison and UUID + + :returns: (revison, uuid) The database revision as a positive integer + and the UUID of the database. + """ + self._assert_db_is_initialized() + uuid = c_char_p () + revision = Database._get_revision(self._db, byref (uuid)) + return (revision, uuid.value.decode ('utf-8')) + _needs_upgrade = nmlib.notmuch_database_needs_upgrade _needs_upgrade.argtypes = [NotmuchDatabaseP] _needs_upgrade.restype = bool @@ -265,7 +286,7 @@ class Database(object): """Does this database need to be upgraded before writing to it? If this function returns `True` then no functions that modify the - database (:meth:`add_message`, + database (:meth:`index_file`, :meth:`Message.add_tag`, :meth:`Directory.set_mtime`, etc.) will work unless :meth:`upgrade` is called successfully first. @@ -379,12 +400,13 @@ class Database(object): # return the Directory, init it with the absolute path return Directory(abs_dirpath, dir_p, self) - _add_message = nmlib.notmuch_database_add_message - _add_message.argtypes = [NotmuchDatabaseP, c_char_p, + _index_file = nmlib.notmuch_database_index_file + _index_file.argtypes = [NotmuchDatabaseP, c_char_p, + c_void_p, POINTER(NotmuchMessageP)] - _add_message.restype = c_uint + _index_file.restype = c_uint - def add_message(self, filename, sync_maildir_flags=False): + def index_file(self, filename, sync_maildir_flags=False): """Adds a new message to the database :param filename: should be a path relative to the path of the @@ -435,7 +457,7 @@ class Database(object): """ self._assert_db_is_initialized() msg_p = NotmuchMessageP() - status = self._add_message(self._db, _str(filename), byref(msg_p)) + status = self._index_file(self._db, _str(filename), c_void_p(None), byref(msg_p)) if not status in [STATUS.SUCCESS, STATUS.DUPLICATE_MESSAGE_ID]: raise NotmuchError(status) @@ -447,6 +469,14 @@ class Database(object): msg.maildir_flags_to_tags() return (msg, status) + def add_message(self, filename, sync_maildir_flags=False): + """Deprecated alias for :meth:`index_file` + """ + warnings.warn( + "This function is deprecated and will be removed in the future, use index_file.", DeprecationWarning) + + return self.index_file(filename, sync_maildir_flags=sync_maildir_flags) + _remove_message = nmlib.notmuch_database_remove_message _remove_message.argtypes = [NotmuchDatabaseP, c_char_p] _remove_message.restype = c_uint @@ -479,7 +509,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 @@ -546,7 +579,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) @@ -570,6 +603,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() + "'" @@ -577,13 +626,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')) @@ -592,12 +634,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