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 DD0EF6DE02AC for ; Fri, 8 Jul 2016 03:13:33 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: 0.108 X-Spam-Level: X-Spam-Status: No, score=0.108 tagged_above=-999 required=5 tests=[AWL=0.108] 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 eo15abrp6g-e for ; Fri, 8 Jul 2016 03:13:24 -0700 (PDT) Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118]) by arlo.cworth.org (Postfix) with ESMTP id 94C316DE02AF for ; Fri, 8 Jul 2016 03:13:09 -0700 (PDT) Received: from fifthhorseman.net (unknown [88.128.80.54]) by che.mayfirst.org (Postfix) with ESMTPSA id EFA0DF99D for ; Fri, 8 Jul 2016 06:13:07 -0400 (EDT) Received: by fifthhorseman.net (Postfix, from userid 1000) id C12D4215CD; Fri, 8 Jul 2016 11:27:34 +0200 (CEST) From: Daniel Kahn Gillmor To: Notmuch Mail Subject: [PATCH v4 13/16] add indexopts to notmuch python bindings. Date: Fri, 8 Jul 2016 11:27:24 +0200 Message-Id: <1467970047-8013-14-git-send-email-dkg@fifthhorseman.net> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1467970047-8013-1-git-send-email-dkg@fifthhorseman.net> References: <1467970047-8013-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: Fri, 08 Jul 2016 10:13:34 -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 | 24 ++++++--- bindings/python/notmuch/globals.py | 5 ++ bindings/python/notmuch/indexopts.py | 97 ++++++++++++++++++++++++++++++++++++ 4 files changed, 120 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 cf627ff..5c19532 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 67fb1c4..a13b9bb 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,15 @@ 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)) + + io = None + if indexopts is not None: + io = indexopts._indexopts + status = self._add_message_with_indexopts(self._db, _str(filename), io, 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 b1eec2c..71426c8 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.8.1