]> git.notmuchmail.org Git - notmuch/blobdiff - bindings/python/notmuch/messages.py
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / bindings / python / notmuch / messages.py
index 251fa3a3457f77ef42831925b7021264c4d9e3be..3801c6664c2d0eaafaf9eaf8cddfea95f60addce 100644 (file)
@@ -12,7 +12,7 @@ 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/>.
+along with notmuch.  If not, see <https://www.gnu.org/licenses/>.
 
 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>
                Jesse Rosenthal <jrosenthal@jhu.edu>
@@ -31,10 +31,8 @@ from .errors import (
 from .tag import Tags
 from .message import Message
 
-import sys
-
 class Messages(object):
-    """Represents a list of notmuch messages
+    r"""Represents a list of notmuch messages
 
     This object provides an iterator over a list of notmuch messages
     (Technically, it provides a wrapper for the underlying
@@ -102,7 +100,7 @@ class Messages(object):
     def __init__(self, msgs_p, parent=None):
         """
         :param msgs_p:  A pointer to an underlying *notmuch_messages_t*
-             structure. These are not publically exposed, so a user
+             structure. These are not publicly exposed, so a user
              will almost never instantiate a :class:`Messages` object
              herself. They are usually handed back as a result,
              e.g. in :meth:`Query.search_messages`.  *msgs_p* must be
@@ -142,7 +140,7 @@ class Messages(object):
         #reset _msgs as we iterated over it and can do so only once
         self._msgs = None
 
-        if tags_p == None:
+        if not tags_p:
             raise NullPointerError()
         return Tags(tags_p, self)
 
@@ -172,11 +170,15 @@ class Messages(object):
     next = __next__ # python2.x iterator protocol compatibility
 
     def __nonzero__(self):
-        """
-        :return: True if there is at least one more thread in the
-            Iterator, False if not."""
-        return self._msgs is not None and \
-            self._valid(self._msgs) > 0
+        '''
+        Implement truth value testing. If __nonzero__ is not
+        implemented, the python runtime would fall back to `len(..) >
+        0` thus exhausting the iterator.
+
+        :returns: True if the wrapped iterator has at least one more object
+                  left.
+        '''
+        return self._msgs and self._valid(self._msgs)
 
     _destroy = nmlib.notmuch_messages_destroy
     _destroy.argtypes = [NotmuchMessagesP]
@@ -187,73 +189,6 @@ class Messages(object):
         if self._msgs:
             self._destroy(self._msgs)
 
-    def format_messages(self, format, indent=0, entire_thread=False):
-        """Formats messages as needed for 'notmuch show'.
-
-        :param format: A string of either 'text' or 'json'.
-        :param indent: A number indicating the reply depth of these messages.
-        :param entire_thread: A bool, indicating whether we want to output
-                       whole threads or only the matching messages.
-        :return: a list of lines
-        """
-        result = list()
-
-        if format.lower() == "text":
-            set_start = ""
-            set_end = ""
-            set_sep = ""
-        elif format.lower() == "json":
-            set_start = "["
-            set_end = "]"
-            set_sep = ", "
-        else:
-            raise TypeError("format must be either 'text' or 'json'")
-
-        first_set = True
-
-        result.append(set_start)
-
-        # iterate through all toplevel messages in this thread
-        for msg in self:
-            # if not msg:
-            #     break
-            if not first_set:
-                result.append(set_sep)
-            first_set = False
-
-            result.append(set_start)
-            match = msg.is_match()
-            next_indent = indent
-
-            if (match or entire_thread):
-                if format.lower() == "text":
-                    result.append(msg.format_message_as_text(indent))
-                else:
-                    result.append(msg.format_message_as_json(indent))
-                next_indent = indent + 1
-
-            # get replies and print them also out (if there are any)
-            replies = msg.get_replies().format_messages(format, next_indent, entire_thread)
-            if replies:
-                result.append(set_sep)
-                result.extend(replies)
-
-            result.append(set_end)
-        result.append(set_end)
-
-        return result
-
-    def print_messages(self, format, indent=0, entire_thread=False, handle=sys.stdout):
-        """Outputs messages as needed for 'notmuch show' to a file like object.
-
-        :param format: A string of either 'text' or 'json'.
-        :param handle: A file like object to print to (default is sys.stdout).
-        :param indent: A number indicating the reply depth of these messages.
-        :param entire_thread: A bool, indicating whether we want to output
-                       whole threads or only the matching messages.
-        """
-        handle.write(''.join(self.format_messages(format, indent, entire_thread)))
-
 class EmptyMessagesResult(Messages):
     def __init__(self, parent):
         self._msgs = None