unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 92bfdef2a767e2464a6249ca2699555a714cb605 31234 bytes (raw)
name: bindings/python-cffi/notmuch2/_database.py 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
 
import collections
import configparser
import enum
import functools
import os
import pathlib
import weakref

import notmuch2._base as base
import notmuch2._config as config
import notmuch2._capi as capi
import notmuch2._errors as errors
import notmuch2._message as message
import notmuch2._query as querymod
import notmuch2._tags as tags


__all__ = ['Database', 'AtomicContext', 'DbRevision']


def _config_pathname():
    """Return the path of the configuration file.

    :rtype: pathlib.Path
    """
    cfgfname = os.getenv('NOTMUCH_CONFIG', '~/.notmuch-config')
    return pathlib.Path(os.path.expanduser(cfgfname))


class Mode(enum.Enum):
    READ_ONLY = capi.lib.NOTMUCH_DATABASE_MODE_READ_ONLY
    READ_WRITE = capi.lib.NOTMUCH_DATABASE_MODE_READ_WRITE

class ConfigFile(enum.Enum):
    EMPTY = b''
    SEARCH = capi.ffi.NULL

class QuerySortOrder(enum.Enum):
    OLDEST_FIRST = capi.lib.NOTMUCH_SORT_OLDEST_FIRST
    NEWEST_FIRST = capi.lib.NOTMUCH_SORT_NEWEST_FIRST
    MESSAGE_ID = capi.lib.NOTMUCH_SORT_MESSAGE_ID
    UNSORTED = capi.lib.NOTMUCH_SORT_UNSORTED


class QueryExclude(enum.Enum):
    TRUE = capi.lib.NOTMUCH_EXCLUDE_TRUE
    FLAG = capi.lib.NOTMUCH_EXCLUDE_FLAG
    FALSE = capi.lib.NOTMUCH_EXCLUDE_FALSE
    ALL = capi.lib.NOTMUCH_EXCLUDE_ALL


class DecryptionPolicy(enum.Enum):
    FALSE = capi.lib.NOTMUCH_DECRYPT_FALSE
    TRUE = capi.lib.NOTMUCH_DECRYPT_TRUE
    AUTO = capi.lib.NOTMUCH_DECRYPT_AUTO
    NOSTASH = capi.lib.NOTMUCH_DECRYPT_NOSTASH


class Database(base.NotmuchObject):
    """Toplevel access to notmuch.

    A :class:`Database` can be opened read-only or read-write.
    Modifications are not atomic by default, use :meth:`begin_atomic`
    for atomic updates.  If the underlying database has been modified
    outside of this class a :exc:`XapianError` will be raised and the
    instance must be closed and a new one created.

    You can use an instance of this class as a context-manager.

    :cvar MODE: The mode a database can be opened with, an enumeration
       of ``READ_ONLY`` and ``READ_WRITE``
    :cvar SORT: The sort order for search results, ``OLDEST_FIRST``,
       ``NEWEST_FIRST``, ``MESSAGE_ID`` or ``UNSORTED``.
    :cvar EXCLUDE: Which messages to exclude from queries, ``TRUE``,
       ``FLAG``, ``FALSE`` or ``ALL``.  See the query documentation
       for details.
    :cvar CONFIG: Control loading of config file. Enumeration of
       ``EMPTY`` (don't load a config file), and ``SEARCH`` (search as
       in :ref:`config_search`)
    :cvar AddedMessage: A namedtuple ``(msg, dup)`` used by
       :meth:`add` as return value.
    :cvar STR_MODE_MAP: A map mapping strings to :attr:`MODE` items.
       This is used to implement the ``ro`` and ``rw`` string
       variants.

    :ivar closed: Boolean indicating if the database is closed or
       still open.

    :param path: The directory of where the database is stored.  If
       ``None`` the location will be searched according to
       :ref:`database`
    :type path: str, bytes, os.PathLike or pathlib.Path
    :param mode: The mode to open the database in.  One of
       :attr:`MODE.READ_ONLY` OR :attr:`MODE.READ_WRITE`.  For
       convenience you can also use the strings ``ro`` for
       :attr:`MODE.READ_ONLY` and ``rw`` for :attr:`MODE.READ_WRITE`.
    :type mode: :attr:`MODE` or str.

    :param config: Where to load the configuration from, if any.
    :type config: :attr:`CONFIG.EMPTY`, :attr:`CONFIG.SEARCH`, str, bytes, os.PathLike, pathlib.Path

    :raises KeyError: if an unknown mode string is used.
    :raises OSError: or subclasses if the configuration file can not
       be opened.
    :raises configparser.Error: or subclasses if the configuration
       file can not be parsed.
    :raises NotmuchError: or subclasses for other failures.

    """

    MODE = Mode
    SORT = QuerySortOrder
    EXCLUDE = QueryExclude
    CONFIG = ConfigFile
    AddedMessage = collections.namedtuple('AddedMessage', ['msg', 'dup'])
    _db_p = base.MemoryPointer()
    STR_MODE_MAP = {
        'ro': MODE.READ_ONLY,
        'rw': MODE.READ_WRITE,
    }

    @staticmethod
    def _cfg_path_encode(path):
        if isinstance(path,ConfigFile):
            path = path.value
        elif path is None:
            path = capi.ffi.NULL
        elif not hasattr(os, 'PathLike') and isinstance(path, pathlib.Path):
            path = bytes(path)
        else:
            path = os.fsencode(path)
        return path

    @staticmethod
    def _db_path_encode(path):
        if path is None:
            path = capi.ffi.NULL
        elif not hasattr(os, 'PathLike') and isinstance(path, pathlib.Path):
            path = bytes(path)
        else:
            path = os.fsencode(path)
        return path

    def __init__(self, path=None, mode=MODE.READ_ONLY, config=CONFIG.EMPTY):
        if isinstance(mode, str):
            mode = self.STR_MODE_MAP[mode]
        self.mode = mode

        db_pp = capi.ffi.new('notmuch_database_t **')
        cmsg = capi.ffi.new('char**')
        ret = capi.lib.notmuch_database_open_with_config(self._db_path_encode(path),
                                                         mode.value,
                                                         self._cfg_path_encode(config),
                                                         capi.ffi.NULL,
                                                         db_pp, cmsg)
        if cmsg[0]:
            msg = capi.ffi.string(cmsg[0]).decode(errors='replace')
            capi.lib.free(cmsg[0])
        else:
            msg = None
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret, msg)
        self._db_p = db_pp[0]
        self.closed = False

    @classmethod
    def create(cls, path=None, config=ConfigFile.EMPTY):
        """Create and open database in READ_WRITE mode.

        This is creates a new notmuch database and returns an opened
        instance in :attr:`MODE.READ_WRITE` mode.

        :param path: The directory of where the database is stored.
           If ``None`` the location will be read searched by the
           notmuch library (see notmuch(3)::notmuch_open_with_config).
        :type path: str, bytes or os.PathLike

        :param config: The pathname of the notmuch configuration file.
        :type config: :attr:`CONFIG.EMPTY`, :attr:`CONFIG.SEARCH`, str, bytes, os.PathLike, pathlib.Path

        :raises OSError: or subclasses if the configuration file can not
           be opened.
        :raises configparser.Error: or subclasses if the configuration
           file can not be parsed.
        :raises NotmuchError: if the config file does not have the
           database.path setting.
        :raises FileError: if the database already exists.

        :returns: The newly created instance.

        """

        db_pp = capi.ffi.new('notmuch_database_t **')
        cmsg = capi.ffi.new('char**')
        ret = capi.lib.notmuch_database_create_with_config(cls._db_path_encode(path),
                                                           cls._cfg_path_encode(config),
                                                           capi.ffi.NULL,
                                                           db_pp, cmsg)
        if cmsg[0]:
            msg = capi.ffi.string(cmsg[0]).decode(errors='replace')
            capi.lib.free(cmsg[0])
        else:
            msg = None
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret, msg)

        # Now close the db and let __init__ open it.  Inefficient but
        # creating is not a hot loop while this allows us to have a
        # clean API.
        ret = capi.lib.notmuch_database_destroy(db_pp[0])
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        return cls(path, cls.MODE.READ_WRITE, config=config)

    @staticmethod
    def default_path(cfg_path=None):
        """Return the path of the user's default database.

        This reads the user's configuration file and returns the
        default path of the database.

        :param cfg_path: The pathname of the notmuch configuration file.
           If not specified tries to use the pathname provided in the
           :envvar:`NOTMUCH_CONFIG` environment variable and falls back
           to :file:`~/.notmuch-config`.
        :type cfg_path: str, bytes, os.PathLike or pathlib.Path.

        :returns: The path of the database, which does not necessarily
           exists.
        :rtype: pathlib.Path
        :raises OSError: or subclasses if the configuration file can not
           be opened.
        :raises configparser.Error: or subclasses if the configuration
           file can not be parsed.
        :raises NotmuchError: if the config file does not have the
           database.path setting.

        .. deprecated:: 0.35
           Use the ``cfg_path`` parameter instead.
        """
        if not cfg_path:
            cfg_path = _config_pathname()
        if not hasattr(os, 'PathLike') and isinstance(cfg_path, pathlib.Path):
            cfg_path = bytes(cfg_path)
        parser = configparser.ConfigParser()
        with open(cfg_path) as fp:
            parser.read_file(fp)
        try:
            return pathlib.Path(parser.get('database', 'path'))
        except configparser.Error:
            raise errors.NotmuchError(
                'No database.path setting in {}'.format(cfg_path))

    def __del__(self):
        self._destroy()

    @property
    def alive(self):
        try:
            self._db_p
        except errors.ObjectDestroyedError:
            return False
        else:
            return True

    def _destroy(self):
        try:
            ret = capi.lib.notmuch_database_destroy(self._db_p)
        except errors.ObjectDestroyedError:
            ret = capi.lib.NOTMUCH_STATUS_SUCCESS
        else:
            self._db_p = None
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)

    def close(self):
        """Close the notmuch database.

        Once closed most operations will fail.  This can still be
        useful however to explicitly close a database which is opened
        read-write as this would otherwise stop other processes from
        reading the database while it is open.

        :raises ObjectDestroyedError: if used after destroyed.
        """
        ret = capi.lib.notmuch_database_close(self._db_p)
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        self.closed = True

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    @property
    def path(self):
        """The pathname of the notmuch database.

        This is returned as a :class:`pathlib.Path` instance.

        :raises ObjectDestroyedError: if used after destroyed.
        """
        try:
            return self._cache_path
        except AttributeError:
            ret = capi.lib.notmuch_database_get_path(self._db_p)
            self._cache_path = pathlib.Path(os.fsdecode(capi.ffi.string(ret)))
            return self._cache_path

    @property
    def version(self):
        """The database format version.

        This is a positive integer.

        :raises ObjectDestroyedError: if used after destroyed.
        """
        try:
            return self._cache_version
        except AttributeError:
            ret = capi.lib.notmuch_database_get_version(self._db_p)
            self._cache_version = ret
            return ret

    @property
    def needs_upgrade(self):
        """Whether the database should be upgraded.

        If *True* the database can be upgraded using :meth:`upgrade`.
        Not doing so may result in some operations raising
        :exc:`UpgradeRequiredError`.

        A read-only database will never be upgradable.

        :raises ObjectDestroyedError: if used after destroyed.
        """
        ret = capi.lib.notmuch_database_needs_upgrade(self._db_p)
        return bool(ret)

    def upgrade(self, progress_cb=None):
        """Upgrade the database to the latest version.

        Upgrade the database, optionally with a progress callback
        which should be a callable which will be called with a
        floating point number in the range of [0.0 .. 1.0].
        """
        raise NotImplementedError

    def atomic(self):
        """Return a context manager to perform atomic operations.

        The returned context manager can be used to perform atomic
        operations on the database.

        .. note:: Unlinke a traditional RDBMS transaction this does
           not imply durability, it only ensures the changes are
           performed atomically.

        :raises ObjectDestroyedError: if used after destroyed.
        """
        ctx = AtomicContext(self, '_db_p')
        return ctx

    def revision(self):
        """The currently committed revision in the database.

        Returned as a ``(revision, uuid)`` namedtuple.

        :raises ObjectDestroyedError: if used after destroyed.
        """
        raw_uuid = capi.ffi.new('char**')
        rev = capi.lib.notmuch_database_get_revision(self._db_p, raw_uuid)
        return DbRevision(rev, capi.ffi.string(raw_uuid[0]))

    def get_directory(self, path):
        raise NotImplementedError

    def default_indexopts(self):
        """Returns default index options for the database.

        :raises ObjectDestroyedError: if used after destroyed.

        :returns: :class:`IndexOptions`.
        """
        opts = capi.lib.notmuch_database_get_default_indexopts(self._db_p)
        return IndexOptions(self, opts)

    def add(self, filename, *, sync_flags=False, indexopts=None):
        """Add a message to the database.

        Add a new message to the notmuch database.  The message is
        referred to by the pathname of the maildir file.  If the
        message ID of the new message already exists in the database,
        this adds ``pathname`` to the list of list of files for the
        existing message.

        :param filename: The path of the file containing the message.
        :type filename: str, bytes, os.PathLike or pathlib.Path.
        :param sync_flags: Whether to sync the known maildir flags to
           notmuch tags.  See :meth:`Message.flags_to_tags` for
           details.
        :type sync_flags: bool
        :param indexopts: The indexing options, see
           :meth:`default_indexopts`.  Leave as `None` to use the
           default options configured in the database.
        :type indexopts: :class:`IndexOptions` or `None`

        :returns: A tuple where the first item is the newly inserted
           messages as a :class:`Message` instance, and the second
           item is a boolean indicating if the message inserted was a
           duplicate.  This is the namedtuple ``AddedMessage(msg,
           dup)``.
        :rtype: Database.AddedMessage

        If an exception is raised, no message was added.

        :raises XapianError: A Xapian exception occurred.
        :raises FileError: The file referred to by ``pathname`` could
           not be opened.
        :raises FileNotEmailError: The file referreed to by
           ``pathname`` is not recognised as an email message.
        :raises ReadOnlyDatabaseError: The database is opened in
           READ_ONLY mode.
        :raises UpgradeRequiredError: The database must be upgraded
           first.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        if not hasattr(os, 'PathLike') and isinstance(filename, pathlib.Path):
            filename = bytes(filename)
        msg_pp = capi.ffi.new('notmuch_message_t **')
        opts_p = indexopts._opts_p if indexopts else capi.ffi.NULL
        ret = capi.lib.notmuch_database_index_file(
            self._db_p, os.fsencode(filename), opts_p, msg_pp)
        ok = [capi.lib.NOTMUCH_STATUS_SUCCESS,
              capi.lib.NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID]
        if ret not in ok:
            raise errors.NotmuchError(ret)
        msg = message.Message(self, msg_pp[0], db=self)
        if sync_flags:
            msg.tags.from_maildir_flags()
        return self.AddedMessage(
            msg, ret == capi.lib.NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)

    def remove(self, filename):
        """Remove a message from the notmuch database.

        Removing a message which is not in the database is just a
        silent nop-operation.

        :param filename: The pathname of the file containing the
           message to be removed.
        :type filename: str, bytes, os.PathLike or pathlib.Path.

        :returns: True if the message is still in the database.  This
           can happen when multiple files contain the same message ID.
           The true/false distinction is fairly arbitrary, but think
           of it as ``dup = db.remove_message(name); if dup: ...``.
        :rtype: bool

        :raises XapianError: A Xapian exception occurred.
        :raises ReadOnlyDatabaseError: The database is opened in
           READ_ONLY mode.
        :raises UpgradeRequiredError: The database must be upgraded
           first.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        if not hasattr(os, 'PathLike') and isinstance(filename, pathlib.Path):
            filename = bytes(filename)
        ret = capi.lib.notmuch_database_remove_message(self._db_p,
                                                       os.fsencode(filename))
        ok = [capi.lib.NOTMUCH_STATUS_SUCCESS,
              capi.lib.NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID]
        if ret not in ok:
            raise errors.NotmuchError(ret)
        if ret == capi.lib.NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
            return True
        else:
            return False

    def find(self, msgid):
        """Return the message matching the given message ID.

        If a message with the given message ID is found a
        :class:`Message` instance is returned.  Otherwise a
        :exc:`LookupError` is raised.

        :param msgid: The message ID to look for.
        :type msgid: str

        :returns: The message instance.
        :rtype: Message

        :raises LookupError: If no message was found.
        :raises OutOfMemoryError: When there is no memory to allocate
           the message instance.
        :raises XapianError: A Xapian exception occurred.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        msg_pp = capi.ffi.new('notmuch_message_t **')
        ret = capi.lib.notmuch_database_find_message(self._db_p,
                                                     msgid.encode(), msg_pp)
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        msg_p = msg_pp[0]
        if msg_p == capi.ffi.NULL:
            raise LookupError
        msg = message.Message(self, msg_p, db=self)
        return msg

    def get(self, filename):
        """Return the :class:`Message` given a pathname.

        If a message with the given pathname exists in the database
        return the :class:`Message` instance for the message.
        Otherwise raise a :exc:`LookupError` exception.

        :param filename: The pathname of the message.
        :type filename: str, bytes, os.PathLike or pathlib.Path

        :returns: The message instance.
        :rtype: Message

        :raises LookupError: If no message was found.  This is also
           a subclass of :exc:`KeyError`.
        :raises OutOfMemoryError: When there is no memory to allocate
           the message instance.
        :raises XapianError: A Xapian exception occurred.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        if not hasattr(os, 'PathLike') and isinstance(filename, pathlib.Path):
            filename = bytes(filename)
        msg_pp = capi.ffi.new('notmuch_message_t **')
        ret = capi.lib.notmuch_database_find_message_by_filename(
            self._db_p, os.fsencode(filename), msg_pp)
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        msg_p = msg_pp[0]
        if msg_p == capi.ffi.NULL:
            raise LookupError
        msg = message.Message(self, msg_p, db=self)
        return msg

    @property
    def tags(self):
        """Return an immutable set with all tags used in this database.

        This returns an immutable set-like object implementing the
        collections.abc.Set Abstract Base Class.  Due to the
        underlying libnotmuch implementation some operations have
        different performance characteristics then plain set objects.
        Mainly any lookup operation is O(n) rather then O(1).

        Normal usage treats tags as UTF-8 encoded unicode strings so
        they are exposed to Python as normal unicode string objects.
        If you need to handle tags stored in libnotmuch which are not
        valid unicode do check the :class:`ImmutableTagSet` docs for
        how to handle this.

        :rtype: ImmutableTagSet

        :raises ObjectDestroyedError: if used after destroyed.
        """
        try:
            ref = self._cached_tagset
        except AttributeError:
            tagset = None
        else:
            tagset = ref()
        if tagset is None:
            tagset = tags.ImmutableTagSet(
                self, '_db_p', capi.lib.notmuch_database_get_all_tags)
            self._cached_tagset = weakref.ref(tagset)
        return tagset

    @property
    def config(self):
        """Return a mutable mapping with the settings stored in this database.

        This returns an mutable dict-like object implementing the
        collections.abc.MutableMapping Abstract Base Class.

        :rtype: Config

        :raises ObjectDestroyedError: if used after destroyed.
        """
        try:
            ref = self._cached_config
        except AttributeError:
            config_mapping = None
        else:
            config_mapping = ref()
        if config_mapping is None:
            config_mapping = config.ConfigMapping(self, '_db_p')
            self._cached_config = weakref.ref(config_mapping)
        return config_mapping

    def _create_query(self, query, *,
                      omit_excluded=EXCLUDE.TRUE,
                      sort=SORT.UNSORTED,  # Check this default
                      exclude_tags=None):
        """Create an internal query object.

        :raises OutOfMemoryError: if no memory is available to
           allocate the query.
        """
        if isinstance(query, str):
            query = query.encode('utf-8')
        query_p = capi.lib.notmuch_query_create(self._db_p, query)
        if query_p == capi.ffi.NULL:
            raise errors.OutOfMemoryError()
        capi.lib.notmuch_query_set_omit_excluded(query_p, omit_excluded.value)
        capi.lib.notmuch_query_set_sort(query_p, sort.value)
        if exclude_tags is not None:
            for tag in exclude_tags:
                if isinstance(tag, str):
                    tag = tag.encode('utf-8')
                capi.lib.notmuch_query_add_tag_exclude(query_p, tag)
        return querymod.Query(self, query_p)

    def messages(self, query, *,
                 omit_excluded=EXCLUDE.TRUE,
                 sort=SORT.UNSORTED,  # Check this default
                 exclude_tags=None):
        """Search the database for messages.

        :returns: An iterator over the messages found.
        :rtype: MessageIter

        :raises OutOfMemoryError: if no memory is available to
           allocate the query.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        query = self._create_query(query,
                                   omit_excluded=omit_excluded,
                                   sort=sort,
                                   exclude_tags=exclude_tags)
        return query.messages()

    def count_messages(self, query, *,
                       omit_excluded=EXCLUDE.TRUE,
                       sort=SORT.UNSORTED,  # Check this default
                       exclude_tags=None):
        """Search the database for messages.

        :returns: An iterator over the messages found.
        :rtype: MessageIter

        :raises ObjectDestroyedError: if used after destroyed.
        """
        query = self._create_query(query,
                                   omit_excluded=omit_excluded,
                                   sort=sort,
                                   exclude_tags=exclude_tags)
        return query.count_messages()

    def threads(self,  query, *,
                omit_excluded=EXCLUDE.TRUE,
                sort=SORT.UNSORTED,  # Check this default
                exclude_tags=None):
        query = self._create_query(query,
                                   omit_excluded=omit_excluded,
                                   sort=sort,
                                   exclude_tags=exclude_tags)
        return query.threads()

    def count_threads(self, query, *,
                      omit_excluded=EXCLUDE.TRUE,
                      sort=SORT.UNSORTED,  # Check this default
                      exclude_tags=None):
        query = self._create_query(query,
                                   omit_excluded=omit_excluded,
                                   sort=sort,
                                   exclude_tags=exclude_tags)
        return query.count_threads()

    def status_string(self):
        raise NotImplementedError

    def __repr__(self):
        return 'Database(path={self.path}, mode={self.mode})'.format(self=self)


class AtomicContext:
    """Context manager for atomic support.

    This supports the notmuch_database_begin_atomic and
    notmuch_database_end_atomic API calls.  The object can not be
    directly instantiated by the user, only via ``Database.atomic``.
    It does keep a reference to the :class:`Database` instance to keep
    the C memory alive.

    :raises XapianError: When this is raised at enter time the atomic
       section is not active.  When it is raised at exit time the
       atomic section is still active and you may need to try using
       :meth:`force_end`.
    :raises ObjectDestroyedError: if used after destroyed.
    """

    def __init__(self, db, ptr_name):
        self._db = db
        self._ptr = lambda: getattr(db, ptr_name)
        self._exit_fn = lambda: None

    def __del__(self):
        self._destroy()

    @property
    def alive(self):
        return self.parent.alive

    def _destroy(self):
        pass

    def __enter__(self):
        ret = capi.lib.notmuch_database_begin_atomic(self._ptr())
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)
        self._exit_fn = self._end_atomic
        return self

    def _end_atomic(self):
        ret = capi.lib.notmuch_database_end_atomic(self._ptr())
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)

    def __exit__(self, exc_type, exc_value, traceback):
        self._exit_fn()

    def force_end(self):
        """Force ending the atomic section.

        This can only be called once __exit__ has been called.  It
        will attempt to close the atomic section (again).  This is
        useful if the original exit raised an exception and the atomic
        section is still open.  But things are pretty ugly by now.

        :raises XapianError: If exiting fails, the atomic section is
           not ended.
        :raises UnbalancedAtomicError: If the database was currently
           not in an atomic section.
        :raises ObjectDestroyedError: if used after destroyed.
        """
        ret = capi.lib.notmuch_database_end_atomic(self._ptr())
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret)

    def abort(self):
        """Abort the transaction.

        Aborting a transaction will not commit any of the changes, but
        will also implicitly close the database.
        """
        self._exit_fn = lambda: None
        self._db.close()


@functools.total_ordering
class DbRevision:
    """A database revision.

    The database revision number increases monotonically with each
    commit to the database.  Which means user-visible changes can be
    ordered.  This object is sortable with other revisions.  It
    carries the UUID of the database to ensure it is only ever
    compared with revisions from the same database.
    """

    def __init__(self, rev, uuid):
        self._rev = rev
        self._uuid = uuid

    @property
    def rev(self):
        """The revision number, a positive integer."""
        return self._rev

    @property
    def uuid(self):
        """The UUID of the database, consider this opaque."""
        return self._uuid

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            if self.uuid != other.uuid:
                return False
            return self.rev == other.rev
        else:
            return NotImplemented

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            if self.uuid != other.uuid:
                return False
            return self.rev < other.rev
        else:
            return NotImplemented

    def __repr__(self):
        return 'DbRevision(rev={self.rev}, uuid={self.uuid})'.format(self=self)


class IndexOptions(base.NotmuchObject):
    """Indexing options.

    This represents the indexing options which can be used to index a
    message.  See :meth:`Database.default_indexopts` to create an
    instance of this.  It can be used e.g. when indexing a new message
    using :meth:`Database.add`.
    """
    _opts_p = base.MemoryPointer()

    def __init__(self, parent, opts_p):
        self._parent = parent
        self._opts_p = opts_p

    @property
    def alive(self):
        if not self._parent.alive:
            return False
        try:
            self._opts_p
        except errors.ObjectDestroyedError:
            return False
        else:
            return True

    def _destroy(self):
        if self.alive:
            capi.lib.notmuch_indexopts_destroy(self._opts_p)
        self._opts_p = None

    @property
    def decrypt_policy(self):
        """The decryption policy.

        This is an enum from the :class:`DecryptionPolicy`.  See the
        `index.decrypt` section in :man:`notmuch-config` for details
        on the options.  **Do not set this to
        :attr:`DecryptionPolicy.TRUE`** without considering the
        security of your index.

        You can change this policy by assigning a new
        :class:`DecryptionPolicy` to this property.

        :raises ObjectDestroyedError: if used after destroyed.

        :returns: A :class:`DecryptionPolicy` enum instance.
        """
        raw = capi.lib.notmuch_indexopts_get_decrypt_policy(self._opts_p)
        return DecryptionPolicy(raw)

    @decrypt_policy.setter
    def decrypt_policy(self, val):
        ret = capi.lib.notmuch_indexopts_set_decrypt_policy(
            self._opts_p, val.value)
        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
            raise errors.NotmuchError(ret, msg)

debug log:

solving 92bfdef2 ...
found 92bfdef2 in https://yhetil.org/notmuch/20211030162235.1203886-5-david@tethera.net/
found c1fb88eb in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 c1fb88eb35c2e956fb7356559bc3041d8772a524	bindings/python-cffi/notmuch2/_database.py

applying [1/1] https://yhetil.org/notmuch/20211030162235.1203886-5-david@tethera.net/
diff --git a/bindings/python-cffi/notmuch2/_database.py b/bindings/python-cffi/notmuch2/_database.py
index c1fb88eb..92bfdef2 100644

Checking patch bindings/python-cffi/notmuch2/_database.py...
Applied patch bindings/python-cffi/notmuch2/_database.py cleanly.

index at:
100644 92bfdef2a767e2464a6249ca2699555a714cb605	bindings/python-cffi/notmuch2/_database.py

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).