]> git.notmuchmail.org Git - notmuch/blob - devel/nmbug/nmbug-status
Merge tag '0.18.2'
[notmuch] / devel / nmbug / nmbug-status
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
4 #
5 # dependencies
6 #       - python 2.6 for json
7 #       - argparse; either python 2.7, or install separately
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see http://www.gnu.org/licenses/ .
21
22 """Generate HTML for one or more notmuch searches.
23
24 Messages matching each search are grouped by thread.  Each message
25 that contains both a subject and message-id will have the displayed
26 subject link to the Gmane view of the message.
27 """
28
29 from __future__ import print_function
30 from __future__ import unicode_literals
31
32 import codecs
33 import collections
34 import datetime
35 import email.utils
36 try:  # Python 3
37     from urllib.parse import quote
38 except ImportError:  # Python 2
39     from urllib import quote
40 import json
41 import argparse
42 import os
43 import re
44 import sys
45 import subprocess
46 import xml.sax.saxutils
47
48
49 _ENCODING = 'UTF-8'
50 _PAGES = {}
51
52
53 if not hasattr(collections, 'OrderedDict'):  # Python 2.6 or earlier
54     class _OrderedDict (dict):
55         "Just enough of a stub to get through Page._get_threads"
56         def __init__(self, *args, **kwargs):
57             super(_OrderedDict, self).__init__(*args, **kwargs)
58             self._keys = []  # record key order
59
60         def __setitem__(self, key, value):
61             super(_OrderedDict, self).__setitem__(key, value)
62             self._keys.append(key)
63
64         def values(self):
65             for key in self._keys:
66                 yield self[key]
67
68
69     collections.OrderedDict = _OrderedDict
70
71
72 def read_config(path=None, encoding=None):
73     "Read config from json file"
74     if not encoding:
75         encoding = _ENCODING
76     if path:
77         fp = open(path)
78     else:
79         nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
80
81         # read only the first line from the pipe
82         sha1_bytes = subprocess.Popen(
83             ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],
84             stdout=subprocess.PIPE).stdout.readline()
85         sha1 = sha1_bytes.decode(encoding).rstrip()
86
87         fp_byte_stream = subprocess.Popen(
88             ['git', '--git-dir', nmbhome, 'cat-file', 'blob',
89              sha1+':status-config.json'],
90             stdout=subprocess.PIPE).stdout
91         fp = codecs.getreader(encoding=encoding)(stream=fp_byte_stream)
92
93     return json.load(fp)
94
95
96 class Thread (list):
97     def __init__(self):
98         self.running_data = {}
99
100
101 class Page (object):
102     def __init__(self, header=None, footer=None):
103         self.header = header
104         self.footer = footer
105
106     def write(self, database, views, stream=None):
107         if not stream:
108             try:  # Python 3
109                 byte_stream = sys.stdout.buffer
110             except AttributeError:  # Python 2
111                 byte_stream = sys.stdout
112             stream = codecs.getwriter(encoding=_ENCODING)(stream=byte_stream)
113         self._write_header(views=views, stream=stream)
114         for view in views:
115             self._write_view(database=database, view=view, stream=stream)
116         self._write_footer(views=views, stream=stream)
117
118     def _write_header(self, views, stream):
119         if self.header:
120             stream.write(self.header)
121
122     def _write_footer(self, views, stream):
123         if self.footer:
124             stream.write(self.footer)
125
126     def _write_view(self, database, view, stream):
127         if 'query-string' not in view:
128             query = view['query']
129             view['query-string'] = ' and '.join(query)
130         q = notmuch.Query(database, view['query-string'])
131         q.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
132         threads = self._get_threads(messages=q.search_messages())
133         self._write_view_header(view=view, stream=stream)
134         self._write_threads(threads=threads, stream=stream)
135
136     def _get_threads(self, messages):
137         threads = collections.OrderedDict()
138         for message in messages:
139             thread_id = message.get_thread_id()
140             if thread_id in threads:
141                 thread = threads[thread_id]
142             else:
143                 thread = Thread()
144                 threads[thread_id] = thread
145             thread.running_data, display_data = self._message_display_data(
146                 running_data=thread.running_data, message=message)
147             thread.append(display_data)
148         return list(threads.values())
149
150     def _write_view_header(self, view, stream):
151         pass
152
153     def _write_threads(self, threads, stream):
154         for thread in threads:
155             for message_display_data in thread:
156                 stream.write(
157                     ('{date:10.10s} {from:20.20s} {subject:40.40s}\n'
158                      '{message-id-term:>72}\n'
159                      ).format(**message_display_data))
160             if thread != threads[-1]:
161                 stream.write('\n')
162
163     def _message_display_data(self, running_data, message):
164         headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
165         data = {}
166         for header in headers:
167             if header == 'thread-id':
168                 value = message.get_thread_id()
169             elif header == 'message-id':
170                 value = message.get_message_id()
171                 data['message-id-term'] = 'id:"{0}"'.format(value)
172             elif header == 'date':
173                 value = str(datetime.datetime.utcfromtimestamp(
174                     message.get_date()).date())
175             else:
176                 value = message.get_header(header)
177             if header == 'from':
178                 (value, addr) = email.utils.parseaddr(value)
179                 if not value:
180                     value = addr.split('@')[0]
181             data[header] = value
182         next_running_data = data.copy()
183         for header, value in data.items():
184             if header in ['message-id', 'subject']:
185                 continue
186             if value == running_data.get(header, None):
187                 data[header] = ''
188         return (next_running_data, data)
189
190
191 class HtmlPage (Page):
192     _slug_regexp = re.compile('\W+')
193
194     def _write_header(self, views, stream):
195         super(HtmlPage, self)._write_header(views=views, stream=stream)
196         stream.write('<ul>\n')
197         for view in views:
198             if 'id' not in view:
199                 view['id'] = self._slug(view['title'])
200             stream.write(
201                 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
202         stream.write('</ul>\n')
203
204     def _write_view_header(self, view, stream):
205         stream.write('<h3 id="{id}">{title}</h3>\n'.format(**view))
206         stream.write('<p>\n')
207         if 'comment' in view:
208             stream.write(view['comment'])
209             stream.write('\n')
210         for line in [
211                 'The view is generated from the following query:',
212                 '</p>',
213                 '<p>',
214                 '  <code>',
215                 view['query-string'],
216                 '  </code>',
217                 '</p>',
218                 ]:
219             stream.write(line)
220             stream.write('\n')
221
222     def _write_threads(self, threads, stream):
223         if not threads:
224             return
225         stream.write('<table>\n')
226         for thread in threads:
227             stream.write('  <tbody>\n')
228             for message_display_data in thread:
229                 stream.write((
230                     '    <tr class="message-first">\n'
231                     '      <td>{date}</td>\n'
232                     '      <td><code>{message-id-term}</code></td>\n'
233                     '    </tr>\n'
234                     '    <tr class="message-last">\n'
235                     '      <td>{from}</td>\n'
236                     '      <td>{subject}</td>\n'
237                     '    </tr>\n'
238                     ).format(**message_display_data))
239             stream.write('  </tbody>\n')
240             if thread != threads[-1]:
241                 stream.write(
242                     '  <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
243         stream.write('</table>\n')
244
245     def _message_display_data(self, *args, **kwargs):
246         running_data, display_data = super(
247             HtmlPage, self)._message_display_data(
248                 *args, **kwargs)
249         if 'subject' in display_data and 'message-id' in display_data:
250             d = {
251                 'message-id': quote(display_data['message-id']),
252                 'subject': xml.sax.saxutils.escape(display_data['subject']),
253                 }
254             display_data['subject'] = (
255                 '<a href="http://mid.gmane.org/{message-id}">{subject}</a>'
256                 ).format(**d)
257         for key in ['message-id', 'from']:
258             if key in display_data:
259                 display_data[key] = xml.sax.saxutils.escape(display_data[key])
260         return (running_data, display_data)
261
262     def _slug(self, string):
263         return self._slug_regexp.sub('-', string)
264
265 parser = argparse.ArgumentParser(description=__doc__)
266 parser.add_argument('--text', help='output plain text format',
267                     action='store_true')
268 parser.add_argument('--config', help='load config from given file',
269                     metavar='PATH')
270 parser.add_argument('--list-views', help='list views',
271                     action='store_true')
272 parser.add_argument('--get-query', help='get query for view',
273                     metavar='VIEW')
274
275 args = parser.parse_args()
276
277 config = read_config(path=args.config)
278
279 header_template = config['meta'].get('header', '''<!DOCTYPE html>
280 <html lang="en">
281 <head>
282   <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
283   <title>{title}</title>
284   <style media="screen" type="text/css">
285     table {{
286       border-spacing: 0;
287     }}
288     tr.message-first td {{
289       padding-top: {inter_message_padding};
290     }}
291     tr.message-last td {{
292       padding-bottom: {inter_message_padding};
293     }}
294     td {{
295       padding-left: {border_radius};
296       padding-right: {border_radius};
297     }}
298     tr:first-child td:first-child {{
299       border-top-left-radius: {border_radius};
300     }}
301     tr:first-child td:last-child {{
302       border-top-right-radius: {border_radius};
303     }}
304     tr:last-child td:first-child {{
305       border-bottom-left-radius: {border_radius};
306     }}
307     tr:last-child td:last-child {{
308       border-bottom-right-radius: {border_radius};
309     }}
310     tbody:nth-child(4n+1) tr td {{
311       background-color: #ffd96e;
312     }}
313     tbody:nth-child(4n+3) tr td {{
314       background-color: #bce;
315     }}
316     hr {{
317       border: 0;
318       height: 1px;
319       color: #ccc;
320       background-color: #ccc;
321     }}
322   </style>
323 </head>
324 <body>
325 <h2>{title}</h2>
326 {blurb}
327 </p>
328 <h3>Views</h3>
329 ''')
330
331 footer_template = config['meta'].get('footer', '''
332 <hr>
333 <p>Generated: {datetime}
334 </body>
335 </html>
336 ''')
337
338 now = datetime.datetime.utcnow()
339 context = {
340     'date': now,
341     'datetime': now.strftime('%Y-%m-%d %H:%M:%SZ'),
342     'title': config['meta']['title'],
343     'blurb': config['meta']['blurb'],
344     'encoding': _ENCODING,
345     'inter_message_padding': '0.25em',
346     'border_radius': '0.5em',
347     }
348
349 _PAGES['text'] = Page()
350 _PAGES['html'] = HtmlPage(
351     header=header_template.format(**context),
352     footer=footer_template.format(**context),
353     )
354
355 if args.list_views:
356     for view in config['views']:
357         print(view['title'])
358     sys.exit(0)
359 elif args.get_query != None:
360     for view in config['views']:
361         if args.get_query == view['title']:
362             print(' and '.join(view['query']))
363     sys.exit(0)
364 else:
365     # only import notmuch if needed
366     import notmuch
367
368 if args.text:
369     page = _PAGES['text']
370 else:
371     page = _PAGES['html']
372
373 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
374 page.write(database=db, views=config['views'])