[PATCH 3/3] python/notmuch2: provide binding for collect_tags()

Subject: [PATCH 3/3] python/notmuch2: provide binding for collect_tags()

Date: Wed, 6 Jan 2021 10:08:42 +0100

To: notmuch@notmuchmail.org

Cc:

From: Michael J Gruber


collect_tags() is accessible in the legacy bindings as a method on the
messages object returned by searches. In the cffi bindings, neither
queries nor their internal values are exposed, so provide a binding
analogous to count_messages().

Signed-off-by: Michael J Gruber <git@grubix.eu>
---
 bindings/python-cffi/notmuch2/_database.py | 17 +++++++++++++++++
 bindings/python-cffi/notmuch2/_query.py    | 12 ++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/bindings/python-cffi/notmuch2/_database.py b/bindings/python-cffi/notmuch2/_database.py
index 7592956a..f3452e11 100644
--- a/bindings/python-cffi/notmuch2/_database.py
+++ b/bindings/python-cffi/notmuch2/_database.py
@@ -618,6 +618,23 @@ class Database(base.NotmuchObject):
                                    exclude_tags=exclude_tags)
         return query.count_messages()
 
+    def collect_tags(self, query, *,
+                       omit_excluded=EXCLUDE.TRUE,
+                       sort=SORT.UNSORTED,  # Check this default
+                       exclude_tags=None):
+        """Search the database for messages and collect tags.
+
+        :returns: The tags of all messages found.
+        :rtype: ImmutableTagset
+
+        :raises ObjectDestroyedError: if used after destroyed.
+        """
+        query = self._create_query(query,
+                                   omit_excluded=omit_excluded,
+                                   sort=sort,
+                                   exclude_tags=exclude_tags)
+        return query.collect_tags()
+
     def threads(self,  query, *,
                 omit_excluded=EXCLUDE.TRUE,
                 sort=SORT.UNSORTED,  # Check this default
diff --git a/bindings/python-cffi/notmuch2/_query.py b/bindings/python-cffi/notmuch2/_query.py
index 1db6ec96..a1310944 100644
--- a/bindings/python-cffi/notmuch2/_query.py
+++ b/bindings/python-cffi/notmuch2/_query.py
@@ -2,6 +2,7 @@ from notmuch2 import _base as base
 from notmuch2 import _capi as capi
 from notmuch2 import _errors as errors
 from notmuch2 import _message as message
+from notmuch2 import _tags as tags
 from notmuch2 import _thread as thread
 
 
@@ -66,6 +67,17 @@ class Query(base.NotmuchObject):
             raise errors.NotmuchError(ret)
         return count_p[0]
 
+    def collect_tags(self):
+        """Return the tags of messages matching this query."""
+        msgs_pp = capi.ffi.new('notmuch_messages_t**')
+        ret = capi.lib.notmuch_query_search_messages(self._query_p, msgs_pp)
+        if ret != capi.lib.NOTMUCH_STATUS_SUCCESS:
+            raise errors.NotmuchError(ret)
+        self._msgs_p = msgs_pp[0]
+        tagset = tags.ImmutableTagSet(
+            self, '_msgs_p', capi.lib.notmuch_messages_collect_tags)
+        return tags.ImmutableTagSet._from_iterable(tagset)
+
     def threads(self):
         """Return an iterator over all the threads found by the query."""
         threads_pp = capi.ffi.new('notmuch_threads_t **')
-- 
2.30.0.rc0.297.gbcca948854
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: