[PATCH 6/8] CLI: refactor dumping of tags.
[notmuch-archives.git] / 77 / af05d6d10f0f4175e320035d25f4384223e0b8
1 Return-Path: <too@guru-group.fi>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 8BA8A431FAE\r
6         for <notmuch@notmuchmail.org>; Wed, 24 Oct 2012 00:00:00 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id cVLve8SfgVK5 for <notmuch@notmuchmail.org>;\r
16         Tue, 23 Oct 2012 23:59:59 -0700 (PDT)\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id 25C61431FAF\r
19         for <notmuch@notmuchmail.org>; Tue, 23 Oct 2012 23:59:59 -0700 (PDT)\r
20 Received: by guru.guru-group.fi (Postfix, from userid 501)\r
21         id A4B9D100390; Wed, 24 Oct 2012 10:00:01 +0300 (EEST)\r
22 From: Tomi Ollila <tomi.ollila@iki.fi>\r
23 To: notmuch@notmuchmail.org\r
24 Subject: [PATCH 2/2] contrib/nmbug/nmbug-status: combine thread messages\r
25 Date: Wed, 24 Oct 2012 09:59:59 +0300\r
26 Message-Id: <1351061999-25473-2-git-send-email-tomi.ollila@iki.fi>\r
27 X-Mailer: git-send-email 1.7.1\r
28 In-Reply-To: <1351061999-25473-1-git-send-email-tomi.ollila@iki.fi>\r
29 References: <1351061999-25473-1-git-send-email-tomi.ollila@iki.fi>\r
30 Cc: tomi.ollila@iki.fi\r
31 X-BeenThere: notmuch@notmuchmail.org\r
32 X-Mailman-Version: 2.1.13\r
33 Precedence: list\r
34 List-Id: "Use and development of the notmuch mail system."\r
35         <notmuch.notmuchmail.org>\r
36 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
37         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
38 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
39 List-Post: <mailto:notmuch@notmuchmail.org>\r
40 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
41 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
42         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
43 X-List-Received-Date: Wed, 24 Oct 2012 07:00:00 -0000\r
44 \r
45 Newer patch email containing In-Reply-To: to an email sent some time ago\r
46 (i.e. to a "thread") was not visible in that "thread" in patch view when\r
47 another patch "thread" was submitted in between. This change collects\r
48 all messages in every (notmuch-created) thread together before printing\r
49 all these threads out in a patch view.\r
50 \r
51 Thanks to Ethan Glasser-Camp for initial review and suggestions with\r
52 code examples.\r
53 ---\r
54 Notes:\r
55 \r
56 Thread class was added for convenience at late time to replace tuple\r
57 containing last dict & lines list; It could be more utilized -- my excuse\r
58 of not doing so is to minimize change...\r
59 \r
60 OrderedDict could be used in place of threadlist but that requires \r
61 Python 2.7 (I used python 2.6 where argparse.py was copied to cwd;\r
62 also nmbug site uses python 2.6...). \r
63 \r
64 The table row separator '\n<tr><td colspan="2"><br /></td></tr>\n'\r
65 is a hack; If someone knows better simple semantic alternative that\r
66 could be used later...\r
67 \r
68  contrib/nmbug/nmbug-status |   73 +++++++++++++++++++++++++++----------------\r
69  1 files changed, 46 insertions(+), 27 deletions(-)\r
70 \r
71 diff --git a/contrib/nmbug/nmbug-status b/contrib/nmbug/nmbug-status\r
72 index c663409..69b407c 100755\r
73 --- a/contrib/nmbug/nmbug-status\r
74 +++ b/contrib/nmbug/nmbug-status\r
75 @@ -51,12 +51,19 @@ if args.text:\r
76  else:\r
77      output_format = 'html'\r
78  \r
79 -headers = ['date', 'from', 'subject']\r
80 -last = {}\r
81 +class Thread:\r
82 +    def __init__(self, last, lines):\r
83 +        self.last = last\r
84 +        self.lines = lines\r
85 +\r
86 +    def join_utf8_with_newlines(self):\r
87 +        return '\n'.join( (line.encode('utf-8') for line in self.lines) )\r
88  \r
89 -def clear_last():\r
90 -    for header in headers:\r
91 -        last[header] = ''\r
92 +def output_with_separator(threadlist, sep):\r
93 +    outputs = (thread.join_utf8_with_newlines() for thread in threadlist)\r
94 +    print sep.join(outputs)\r
95 +\r
96 +headers = ['date', 'from', 'subject']\r
97  \r
98  def print_view(title, query, comment):\r
99  \r
100 @@ -64,7 +71,12 @@ def print_view(title, query, comment):\r
101      q_new = notmuch.Query(db, query_string)\r
102      q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)\r
103  \r
104 -    last['thread_id'] = ''\r
105 +    last_thread_id = ''\r
106 +    threads = {}\r
107 +    threadlist = []\r
108 +    out = {}\r
109 +    last = None\r
110 +    lines = None\r
111  \r
112      if output_format == 'html':\r
113          print '<h3><a name="%s" />%s</h3>' % (title, title)\r
114 @@ -77,11 +89,21 @@ def print_view(title, query, comment):\r
115  \r
116      for m in q_new.search_messages():\r
117  \r
118 -        out = {}\r
119 -\r
120          thread_id = m.get_thread_id()\r
121 -        if thread_id != last['thread_id']:\r
122 -            clear_last()\r
123 +\r
124 +        if thread_id != last_thread_id:\r
125 +            if threads.has_key(thread_id):\r
126 +                last = threads[thread_id].last\r
127 +                lines = threads[thread_id].lines\r
128 +            else:\r
129 +                last = {}\r
130 +                lines = []\r
131 +                thread = Thread(last, lines)\r
132 +                threads[thread_id] = thread\r
133 +                for h in headers:\r
134 +                    last[h] = ''\r
135 +                threadlist.append(thread)\r
136 +            last_thread_id = thread_id\r
137  \r
138          for header in headers:\r
139              val = m.get_header(header)\r
140 @@ -94,38 +116,35 @@ def print_view(title, query, comment):\r
141                  if val == '':\r
142                      val = addr.split('@')[0]\r
143  \r
144 -            if last[header] == val:\r
145 +            if header != 'subject' and last[header] == val:\r
146                  out[header] = ''\r
147              else:\r
148 -                out[header] = val.encode('utf-8')\r
149 +                out[header] = val\r
150                  last[header] = val\r
151  \r
152          mid = m.get_message_id()\r
153          out['id'] = 'id:"%s"' % mid\r
154  \r
155          if output_format == 'html':\r
156 -            # XXX using <br /> is a hack, but ... // 20111216 too\r
157 -            if thread_id != last['thread_id']:\r
158 -                br = '<br />'\r
159 -            else:\r
160 -                br = ''\r
161  \r
162              out['subject'] = '<a href="http://mid.gmane.org/%s">%s</a>' \\r
163                  % (urllib.quote(mid), out['subject'])\r
164  \r
165 -            print ' <tr><td>%s %s' % (br, out['date'])\r
166 -            print '</td><td>%s %s' % (br, out['id'])\r
167 -            print '</td></tr>'\r
168 -            print ' <tr><td>%s' % out['from']\r
169 -            print '</td><td>%s' % out['subject']\r
170 -            print '</td></tr>\n'\r
171 +            lines.append(' <tr><td>%s' % out['date'])\r
172 +            lines.append('</td><td>%s' % out['id'])\r
173 +            lines.append('</td></tr>')\r
174 +            lines.append(' <tr><td>%s' % out['from'])\r
175 +            lines.append('</td><td>%s' % out['subject'])\r
176 +            lines.append('</td></tr>')\r
177          else:\r
178 -            print '%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s\n' % out\r
179 -\r
180 -        last['thread_id'] = thread_id\r
181 +            lines.append('%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s' % out)\r
182  \r
183      if output_format == 'html':\r
184 +        output_with_separator(threadlist,\r
185 +                              '\n<tr><td colspan="2"><br /></td></tr>\n')\r
186          print '</table>'\r
187 +    else:\r
188 +        output_with_separator(threadlist, '\n\n')\r
189  \r
190  # main program\r
191  \r
192 -- \r
193 1.7.1\r
194 \r