From: Ethan Glasser-Camp <ethan@betacantrips.com> Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com> --- bindings/python/notmuch/database.py | 31 +++++++++++++++++--------- bindings/python/notmuch/globals.py | 3 ++ bindings/python/notmuch/mailstore.py | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 bindings/python/notmuch/mailstore.py diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index 36b65ec..638ade3 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -23,7 +23,9 @@ from ctypes import c_char_p, c_void_p, c_uint, c_long, byref, POINTER from notmuch.globals import (nmlib, STATUS, NotmuchError, NotInitializedError, NullPointerError, Enum, _str, NotmuchDatabaseP, NotmuchDirectoryP, NotmuchMessageP, NotmuchTagsP, - NotmuchQueryP, NotmuchMessagesP, NotmuchThreadsP, NotmuchFilenamesP) + NotmuchQueryP, NotmuchMessagesP, NotmuchThreadsP, NotmuchFilenamesP, + NotmuchMailstoreP) +from notmuch.mailstore import Mailstore from notmuch.thread import Threads from notmuch.message import Messages, Message from notmuch.tag import Tags @@ -78,7 +80,7 @@ class Database(object): """notmuch_database_open""" _open = nmlib.notmuch_database_open - _open.argtypes = [c_char_p, c_uint] + _open.argtypes = [NotmuchMailstoreP, c_char_p, c_uint] _open.restype = NotmuchDatabaseP """notmuch_database_upgrade""" @@ -105,11 +107,13 @@ class Database(object): """notmuch_database_create""" _create = nmlib.notmuch_database_create - _create.argtypes = [c_char_p] + _create.argtypes = [NotmuchMailstoreP, c_char_p] _create.restype = NotmuchDatabaseP - def __init__(self, path=None, create=False, mode=0): - """If *path* is `None`, we will try to read a users notmuch + def __init__(self, mailstore=None, path=None, create=False, mode=0): + """If *mailstore* is `None`, we will just use 'maildir'. + + If *path* is `None`, we will try to read a users notmuch configuration and use his configured database. The location of the configuration file can be specified through the environment variable *NOTMUCH_CONFIG*, falling back to the default `~/.notmuch-config`. @@ -130,6 +134,9 @@ class Database(object): failure. """ self._db = None + if mailstore == None: + mailstore = Mailstore('maildir') + if path is None: # no path specified. use a user's default database if Database._std_db_path is None: @@ -138,16 +145,16 @@ class Database(object): path = Database._std_db_path if create == False: - self.open(path, mode) + self.open(mailstore, path, mode) else: - self.create(path) + self.create(mailstore, path) def _assert_db_is_initialized(self): """Raises :exc:`NotInitializedError` if self._db is `None`""" if self._db is None: raise NotInitializedError() - def create(self, path): + def create(self, mailstore, path): """Creates a new notmuch database This function is used by __init__() and usually does not need @@ -167,14 +174,15 @@ class Database(object): raise NotmuchError(message="Cannot create db, this Database() " "already has an open one.") - res = Database._create(_str(path), Database.MODE.READ_WRITE) + mailstore = mailstore._mailstore + res = Database._create(mailstore, _str(path), Database.MODE.READ_WRITE) if not res: raise NotmuchError( message="Could not create the specified database") self._db = res - def open(self, path, mode=0): + def open(self, mailstore, path, mode=0): """Opens an existing database This function is used by __init__() and usually does not need @@ -187,7 +195,8 @@ class Database(object): :exception: Raises :exc:`NotmuchError` in case of any failure (possibly after printing an error message on stderr). """ - res = Database._open(_str(path), mode) + mailstore = mailstore._mailstore # unwrap mailstore + res = Database._open(mailstore, _str(path), mode) if not res: raise NotmuchError(message="Could not open the specified database") diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py index 4138460..5f01dce 100644 --- a/bindings/python/notmuch/globals.py +++ b/bindings/python/notmuch/globals.py @@ -228,6 +228,9 @@ class NotmuchDatabaseS(Structure): pass NotmuchDatabaseP = POINTER(NotmuchDatabaseS) +class NotmuchMailstoreS(Structure): + pass +NotmuchMailstoreP = POINTER(NotmuchMailstoreS) class NotmuchQueryS(Structure): pass diff --git a/bindings/python/notmuch/mailstore.py b/bindings/python/notmuch/mailstore.py new file mode 100644 index 0000000..315ea70 --- /dev/null +++ b/bindings/python/notmuch/mailstore.py @@ -0,0 +1,38 @@ +""" +This file is part of notmuch. + +Notmuch is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation, either version 3 of the License, or (at your +option) any later version. + +Notmuch is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +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 <http://www.gnu.org/licenses/>. + +Copyright 2012 Ethan Glasser-Camp <ethan@betacantrips.com>' +""" + +# This is all kind of cargo-culted from database.py. I hope someone +# else takes a good look at this! + +import os +from ctypes import c_char_p, c_void_p, c_uint, c_long, byref, POINTER +from notmuch.globals import (nmlib, STATUS, NotmuchError, NotInitializedError, + NullPointerError, Enum, _str, + NotmuchDatabaseP, NotmuchDirectoryP, NotmuchMessageP, NotmuchTagsP, + NotmuchQueryP, NotmuchMessagesP, NotmuchThreadsP, NotmuchFilenamesP, + NotmuchMailstoreP,) + +class Mailstore(object): + """The :class:`Mailstore` represents "where the mail lives".""" + _get_by_name = nmlib.notmuch_mailstore_get_by_name + _get_by_name.argtypes = [c_char_p] + _get_by_name.restype = NotmuchMailstoreP + + def __init__(self, type=None, path=None): + self._mailstore = self._get_by_name(type) -- 1.7.5.4