Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 83D59429E49 for ; Wed, 21 Dec 2011 05:15:32 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0.001 X-Spam-Level: X-Spam-Status: No, score=0.001 tagged_above=-999 required=5 tests=[UNPARSEABLE_RELAY=0.001] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rSLoOzEkR-Gj for ; Wed, 21 Dec 2011 05:15:32 -0800 (PST) Received: from mail.cryptobitch.de (cryptobitch.de [88.198.7.68]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 83817429E4E for ; Wed, 21 Dec 2011 05:15:31 -0800 (PST) Received: from mail.jade-hamburg.de (unknown [85.183.11.228]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.cryptobitch.de (Postfix) with ESMTPSA id 54B895102BE for ; Wed, 21 Dec 2011 14:15:30 +0100 (CET) Received: by mail.jade-hamburg.de (Postfix, from userid 401) id C7EA8DF2A1; Wed, 21 Dec 2011 14:15:29 +0100 (CET) Received: from thinkbox.jade-hamburg.de (unknown [IPv6:fe80::216:d3ff:fe3e:5058%br0]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: teythoon) by mail.jade-hamburg.de (Postfix) with ESMTPSA id C74DBDF29F; Wed, 21 Dec 2011 14:15:18 +0100 (CET) Received: from teythoon by thinkbox.jade-hamburg.de with local (Exim 4.77) (envelope-from ) id 1RdM0r-00033S-Uh; Wed, 21 Dec 2011 14:15:18 +0100 From: Justus Winter <4winter@informatik.uni-hamburg.de> To: notmuch@notmuchmail.org Subject: [PATCH 1/2] python: refactor print_messages into format_messages and print_messages Date: Wed, 21 Dec 2011 14:15:01 +0100 Message-Id: <1324473302-10869-2-git-send-email-4winter@informatik.uni-hamburg.de> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1324473302-10869-1-git-send-email-4winter@informatik.uni-hamburg.de> References: <87sjm79e3d.fsf@SSpaeth.de> <1324473302-10869-1-git-send-email-4winter@informatik.uni-hamburg.de> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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, 21 Dec 2011 13:15:32 -0000 --- bindings/python/notmuch/message.py | 37 +++++++++++++++++++++++++---------- 1 files changed, 26 insertions(+), 11 deletions(-) diff --git a/bindings/python/notmuch/message.py b/bindings/python/notmuch/message.py index ce8e718..cc9fc2a 100644 --- a/bindings/python/notmuch/message.py +++ b/bindings/python/notmuch/message.py @@ -186,14 +186,17 @@ class Messages(object): if self._msgs is not None: self._destroy(self._msgs) - def print_messages(self, format, indent=0, entire_thread=False): - """Outputs messages as needed for 'notmuch show' to sys.stdout + 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 = "" @@ -207,36 +210,48 @@ class Messages(object): first_set = True - sys.stdout.write(set_start) + result.append(set_start) # iterate through all toplevel messages in this thread for msg in self: # if not msg: # break if not first_set: - sys.stdout.write(set_sep) + result.append(set_sep) first_set = False - sys.stdout.write(set_start) + result.append(set_start) match = msg.is_match() next_indent = indent if (match or entire_thread): if format.lower() == "text": - sys.stdout.write(msg.format_message_as_text(indent)) + result.append(msg.format_message_as_text(indent)) else: - sys.stdout.write(msg.format_message_as_json(indent)) + 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() if not replies is None: - sys.stdout.write(set_sep) - replies.print_messages(format, next_indent, entire_thread) + result.append(set_sep) + result.extend(replies.format_messages(format, next_indent, entire_thread)) + + result.append(set_end) + result.append(set_end) - sys.stdout.write(set_end) - sys.stdout.write(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 Message(object): """Represents a single Email message -- 1.7.7.3