From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id DC6656DE1BC8 for ; Tue, 19 Jan 2016 18:53:34 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.022 X-Spam-Level: X-Spam-Status: No, score=-0.022 tagged_above=-999 required=5 tests=[AWL=-0.022] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id M8w8ci7xGJyE for ; Tue, 19 Jan 2016 18:53:31 -0800 (PST) Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108]) by arlo.cworth.org (Postfix) with ESMTP id 1C1976DE178E for ; Tue, 19 Jan 2016 18:53:17 -0800 (PST) Received: from fifthhorseman.net (unknown [38.109.115.130]) by che.mayfirst.org (Postfix) with ESMTPSA id 57D02F99C for ; Tue, 19 Jan 2016 21:53:14 -0500 (EST) Received: by fifthhorseman.net (Postfix, from userid 1000) id 0906B2092E; Tue, 19 Jan 2016 18:53:11 -0800 (PST) From: Daniel Kahn Gillmor To: Notmuch Mail Subject: [PATCH v2 13/16] add indexopts to notmuch python bindings. Date: Tue, 19 Jan 2016 21:52:46 -0500 Message-Id: <1453258369-7366-14-git-send-email-dkg@fifthhorseman.net> X-Mailer: git-send-email 2.7.0.rc3 In-Reply-To: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net> References: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jan 2016 02:53:35 -0000 Make notmuch indexing options are not available in python as the notmuch.Indexopts class. Users can do something like: import notmuch d = notmuch.Database() indexopts = notmuch.Indexopts(try_decrypt=true) d.add_message(fname, indexopts=indexopts) --- bindings/python/notmuch/__init__.py | 1 + bindings/python/notmuch/database.py | 21 +++++--- bindings/python/notmuch/globals.py | 5 ++ bindings/python/notmuch/indexopts.py | 97 ++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 bindings/python/notmuch/indexopts.py diff --git a/bindings/python/notmuch/__init__.py b/bindings/python/notmuch/__init__.py index 29416a5..fe2d886 100644 --- a/bindings/python/notmuch/__init__.py +++ b/bindings/python/notmuch/__init__.py @@ -54,6 +54,7 @@ Copyright 2010-2011 Sebastian Spaeth from .database import Database from .directory import Directory from .filenames import Filenames +from .indexopts import Indexopts from .message import Message from .messages import Messages from .query import Query diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index 93e7b7a..1b0ee1a 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -29,6 +29,7 @@ from .globals import ( NotmuchDirectoryP, NotmuchMessageP, NotmuchTagsP, + NotmuchIndexoptsP, ) from .errors import ( STATUS, @@ -383,12 +384,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, - POINTER(NotmuchMessageP)] - _add_message.restype = c_uint - - def add_message(self, filename, sync_maildir_flags=False): + _add_message_with_indexopts = nmlib.notmuch_database_add_message_with_indexopts + _add_message_with_indexopts.argtypes = [NotmuchDatabaseP, c_char_p, + NotmuchIndexoptsP, + POINTER(NotmuchMessageP)] + _add_message_with_indexopts.restype = c_uint + + def add_message(self, filename, sync_maildir_flags=False, indexopts=None): """Adds a new message to the database :param filename: should be a path relative to the path of the @@ -409,6 +411,9 @@ class Database(object): API. You might want to look into the underlying method :meth:`Message.maildir_flags_to_tags`. + :param indexopts: a nomtuch.Indexopts object indicating custom + options desired for indexing. + :returns: On success, we return 1) a :class:`Message` object that can be used for things @@ -436,10 +441,12 @@ class Database(object): :attr:`STATUS`.READ_ONLY_DATABASE Database was opened in read-only mode so no message can be added. + """ self._assert_db_is_initialized() msg_p = NotmuchMessageP() - status = self._add_message(self._db, _str(filename), byref(msg_p)) + + status = self._add_message_with_indexopts(self._db, _str(filename), indexopts._indexopts, byref(msg_p)) if not status in [STATUS.SUCCESS, STATUS.DUPLICATE_MESSAGE_ID]: raise NotmuchError(status) diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py index 6872a29..c4b9832 100644 --- a/bindings/python/notmuch/globals.py +++ b/bindings/python/notmuch/globals.py @@ -88,3 +88,8 @@ NotmuchDirectoryP = POINTER(NotmuchDirectoryS) class NotmuchFilenamesS(Structure): pass NotmuchFilenamesP = POINTER(NotmuchFilenamesS) + + +class NotmuchIndexoptsS(Structure): + pass +NotmuchIndexoptsP = POINTER(NotmuchIndexoptsS) diff --git a/bindings/python/notmuch/indexopts.py b/bindings/python/notmuch/indexopts.py new file mode 100644 index 0000000..b0d4603 --- /dev/null +++ b/bindings/python/notmuch/indexopts.py @@ -0,0 +1,97 @@ +""" +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 . + +Copyright 2015 Daniel Kahn Gillmor +""" +from ctypes import c_char_p, c_bool, c_int +from .globals import ( + nmlib, + NotmuchIndexoptsP, +) +from .errors import ( + STATUS, + NullPointerError, + NotInitializedError, +) + + +class Indexopts(object): + """Represents available options for notmuch indexing. + """ + + # create + _create = nmlib.notmuch_indexopts_create + _create.argtypes = [] + _create.restype = NotmuchIndexoptsP + + def __init__(self, try_decrypt=False, gpg_path=None): + """ + :param try_decrypt: True if notmuch should try to decrypt messages + while indexing, and index the cleartext. + + :param gpg_path: the name or path to the preferred GnuPG binary. + """ + self._indexopts = Indexopts._create() + self.gpg_path = gpg_path + self.try_decrypt = try_decrypt + + # try_decrypt + _get_try_decrypt = nmlib.notmuch_indexopts_get_try_decrypt + _get_try_decrypt.argtypes = [NotmuchIndexoptsP] + _get_try_decrypt.restype = bool + + _set_try_decrypt = nmlib.notmuch_indexopts_set_try_decrypt + _set_try_decrypt.argtypes = [NotmuchIndexoptsP, c_bool] + _set_try_decrypt.restype = c_int + + @property + def try_decrypt(self): + return Indexopts._get_try_decrypt(self._indexopts) + + @try_decrypt.setter + def try_decrypt(self, try_decrypt): + status = Indexopts._set_try_decrypt(self._indexopts, try_decrypt) + if status != STATUS.SUCCESS: + raise NotmuchError(status) + + # gpg_path + _get_gpg_path = nmlib.notmuch_indexopts_get_gpg_path + _get_gpg_path.argtypes = [NotmuchIndexoptsP] + _get_gpg_path.restype = c_char_p + + _set_gpg_path = nmlib.notmuch_indexopts_set_gpg_path + _set_gpg_path.argtypes = [NotmuchIndexoptsP, c_char_p] + _set_gpg_path.restype = c_int + + @property + def gpg_path(self): + return Indexopts._get_gpg_path(self._indexopts) + + @gpg_path.setter + def gpg_path(self, gpg_path): + status = Indexopts._set_gpg_path(self._indexopts, gpg_path) + if status != STATUS.SUCCESS: + raise NotmuchError(status) + + + _destroy = nmlib.notmuch_indexopts_destroy + _destroy.argtypes = [NotmuchIndexoptsP] + _destroy.restype = None + + def __del__(self): + """Close and free the indexopts""" + if self._indexopts: + self._destroy(self._indexopts) -- 2.7.0.rc3