]> git.notmuchmail.org Git - notmuch/blob - devel/nmbug/nmbug-status
a7c79206621e8d21ac3305210be45eac54e902be
[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 from __future__ import print_function
23 from __future__ import unicode_literals
24
25 import codecs
26 import collections
27 import datetime
28 import email.utils
29 try:  # Python 3
30     from urllib.parse import quote
31 except ImportError:  # Python 2
32     from urllib import quote
33 import json
34 import argparse
35 import os
36 import re
37 import sys
38 import subprocess
39 import xml.sax.saxutils
40
41
42 _ENCODING = 'UTF-8'
43 _PAGES = {}
44
45
46 if not hasattr(collections, 'OrderedDict'):  # Python 2.6 or earlier
47     class _OrderedDict (dict):
48         "Just enough of a stub to get through Page._get_threads"
49         def __init__(self, *args, **kwargs):
50             super(_OrderedDict, self).__init__(*args, **kwargs)
51             self._keys = []  # record key order
52
53         def __setitem__(self, key, value):
54             super(_OrderedDict, self).__setitem__(key, value)
55             self._keys.append(key)
56
57         def values(self):
58             for key in self._keys:
59                 yield self[key]
60
61
62     collections.OrderedDict = _OrderedDict
63
64
65 def read_config(path=None, encoding=None):
66     "Read config from json file"
67     if not encoding:
68         encoding = _ENCODING
69     if path:
70         fp = open(path)
71     else:
72         nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
73
74         # read only the first line from the pipe
75         sha1_bytes = subprocess.Popen(
76             ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],
77             stdout=subprocess.PIPE).stdout.readline()
78         sha1 = sha1_bytes.decode(encoding).rstrip()
79
80         fp_byte_stream = subprocess.Popen(
81             ['git', '--git-dir', nmbhome, 'cat-file', 'blob',
82              sha1+':status-config.json'],
83             stdout=subprocess.PIPE).stdout
84         fp = codecs.getreader(encoding=encoding)(stream=fp_byte_stream)
85
86     return json.load(fp)
87
88
89 class Thread (list):
90     def __init__(self):
91         self.running_data = {}
92
93
94 class Page (object):
95     def __init__(self, header=None, footer=None):
96         self.header = header
97         self.footer = footer
98
99     def write(self, database, views, stream=None):
100         if not stream:
101             try:  # Python 3
102                 byte_stream = sys.stdout.buffer
103             except AttributeError:  # Python 2
104                 byte_stream = sys.stdout
105             stream = codecs.getwriter(encoding=_ENCODING)(stream=byte_stream)
106         self._write_header(views=views, stream=stream)
107         for view in views:
108             self._write_view(database=database, view=view, stream=stream)
109         self._write_footer(views=views, stream=stream)
110
111     def _write_header(self, views, stream):
112         if self.header:
113             stream.write(self.header)
114
115     def _write_footer(self, views, stream):
116         if self.footer:
117             stream.write(self.footer)
118
119     def _write_view(self, database, view, stream):
120         if 'query-string' not in view:
121             query = view['query']
122             view['query-string'] = ' and '.join(query)
123         q = notmuch.Query(database, view['query-string'])
124         q.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
125         threads = self._get_threads(messages=q.search_messages())
126         self._write_view_header(view=view, stream=stream)
127         self._write_threads(threads=threads, stream=stream)
128
129     def _get_threads(self, messages):
130         threads = collections.OrderedDict()
131         for message in messages:
132             thread_id = message.get_thread_id()
133             if thread_id in threads:
134                 thread = threads[thread_id]
135             else:
136                 thread = Thread()
137                 threads[thread_id] = thread
138             thread.running_data, display_data = self._message_display_data(
139                 running_data=thread.running_data, message=message)
140             thread.append(display_data)
141         return list(threads.values())
142
143     def _write_view_header(self, view, stream):
144         pass
145
146     def _write_threads(self, threads, stream):
147         for thread in threads:
148             for message_display_data in thread:
149                 stream.write(
150                     ('{date:10.10s} {from:20.20s} {subject:40.40s}\n'
151                      '{message-id-term:>72}\n'
152                      ).format(**message_display_data))
153             if thread != threads[-1]:
154                 stream.write('\n')
155
156     def _message_display_data(self, running_data, message):
157         headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
158         data = {}
159         for header in headers:
160             if header == 'thread-id':
161                 value = message.get_thread_id()
162             elif header == 'message-id':
163                 value = message.get_message_id()
164                 data['message-id-term'] = 'id:"{0}"'.format(value)
165             elif header == 'date':
166                 value = str(datetime.datetime.utcfromtimestamp(
167                     message.get_date()).date())
168             else:
169                 value = message.get_header(header)
170             if header == 'from':
171                 (value, addr) = email.utils.parseaddr(value)
172                 if not value:
173                     value = addr.split('@')[0]
174             data[header] = value
175         next_running_data = data.copy()
176         for header, value in data.items():
177             if header in ['message-id', 'subject']:
178                 continue
179             if value == running_data.get(header, None):
180                 data[header] = ''
181         return (next_running_data, data)
182
183
184 class HtmlPage (Page):
185     _slug_regexp = re.compile('\W+')
186
187     def _write_header(self, views, stream):
188         super(HtmlPage, self)._write_header(views=views, stream=stream)
189         stream.write('<ul>\n')
190         for view in views:
191             if 'id' not in view:
192                 view['id'] = self._slug(view['title'])
193             stream.write(
194                 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
195         stream.write('</ul>\n')
196
197     def _write_view_header(self, view, stream):
198         stream.write('<h3 id="{id}">{title}</h3>\n'.format(**view))
199         stream.write('<p>\n')
200         if 'comment' in view:
201             stream.write(view['comment'])
202             stream.write('\n')
203         for line in [
204                 'The view is generated from the following query:',
205                 '</p>',
206                 '<p>',
207                 '  <code>',
208                 view['query-string'],
209                 '  </code>',
210                 '</p>',
211                 ]:
212             stream.write(line)
213             stream.write('\n')
214
215     def _write_threads(self, threads, stream):
216         if not threads:
217             return
218         stream.write('<table>\n')
219         for thread in threads:
220             stream.write('  <tbody>\n')
221             for message_display_data in thread:
222                 stream.write((
223                     '    <tr class="message-first">\n'
224                     '      <td>{date}</td>\n'
225                     '      <td><code>{message-id-term}</code></td>\n'
226                     '    </tr>\n'
227                     '    <tr class="message-last">\n'
228                     '      <td>{from}</td>\n'
229                     '      <td>{subject}</td>\n'
230                     '    </tr>\n'
231                     ).format(**message_display_data))
232             stream.write('  </tbody>\n')
233             if thread != threads[-1]:
234                 stream.write(
235                     '  <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
236         stream.write('</table>\n')
237
238     def _message_display_data(self, *args, **kwargs):
239         running_data, display_data = super(
240             HtmlPage, self)._message_display_data(
241                 *args, **kwargs)
242         if 'subject' in display_data and 'message-id' in display_data:
243             d = {
244                 'message-id': quote(display_data['message-id']),
245                 'subject': xml.sax.saxutils.escape(display_data['subject']),
246                 }
247             display_data['subject'] = (
248                 '<a href="http://mid.gmane.org/{message-id}">{subject}</a>'
249                 ).format(**d)
250         for key in ['message-id', 'from']:
251             if key in display_data:
252                 display_data[key] = xml.sax.saxutils.escape(display_data[key])
253         return (running_data, display_data)
254
255     def _slug(self, string):
256         return self._slug_regexp.sub('-', string)
257
258 parser = argparse.ArgumentParser()
259 parser.add_argument('--text', help='output plain text format',
260                     action='store_true')
261 parser.add_argument('--config', help='load config from given file',
262                     metavar='PATH')
263 parser.add_argument('--list-views', help='list views',
264                     action='store_true')
265 parser.add_argument('--get-query', help='get query for view',
266                     metavar='VIEW')
267
268 args = parser.parse_args()
269
270 config = read_config(path=args.config)
271
272 _PAGES['text'] = Page()
273 _PAGES['html'] = HtmlPage(
274     header='''<!DOCTYPE html>
275 <html lang="en">
276 <head>
277   <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
278   <title>{title}</title>
279   <style media="screen" type="text/css">
280     table {{
281       border-spacing: 0;
282     }}
283     tr.message-first td {{
284       padding-top: {inter_message_padding};
285     }}
286     tr.message-last td {{
287       padding-bottom: {inter_message_padding};
288     }}
289     td {{
290       padding-left: {border_radius};
291       padding-right: {border_radius};
292     }}
293     tr:first-child td:first-child {{
294       border-top-left-radius: {border_radius};
295     }}
296     tr:first-child td:last-child {{
297       border-top-right-radius: {border_radius};
298     }}
299     tr:last-child td:first-child {{
300       border-bottom-left-radius: {border_radius};
301     }}
302     tr:last-child td:last-child {{
303       border-bottom-right-radius: {border_radius};
304     }}
305     tbody:nth-child(4n+1) tr td {{
306       background-color: #ffd96e;
307     }}
308     tbody:nth-child(4n+3) tr td {{
309       background-color: #bce;
310     }}
311   </style>
312 </head>
313 <body>
314 <h2>{title}</h2>
315 <p>
316 Generated: {date}<br />
317 {blurb}
318 </p>
319 <h3>Views</h3>
320 '''.format(date=datetime.datetime.utcnow().date(),
321            title=config['meta']['title'],
322            blurb=config['meta']['blurb'],
323            encoding=_ENCODING,
324            inter_message_padding='0.25em',
325            border_radius='0.5em'),
326     footer='</body>\n</html>\n',
327     )
328
329 if args.list_views:
330     for view in config['views']:
331         print(view['title'])
332     sys.exit(0)
333 elif args.get_query != None:
334     for view in config['views']:
335         if args.get_query == view['title']:
336             print(' and '.join(view['query']))
337     sys.exit(0)
338 else:
339     # only import notmuch if needed
340     import notmuch
341
342 if args.text:
343     page = _PAGES['text']
344 else:
345     page = _PAGES['html']
346
347 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
348 page.write(database=db, views=config['views'])