]> git.notmuchmail.org Git - notmuch/blob - contrib/nmbug/nmbug-status
contrib/nmbug/nmbug-status: if realname empty, use part of mailaddr
[notmuch] / contrib / nmbug / nmbug-status
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
4 # License: Same as notmuch
5 # dependencies
6 #       - python 2.6 for json
7 #       - argparse; either python 2.7, or install separately
8
9 import datetime
10 import notmuch
11 import rfc822
12 import urllib
13 import json
14 import argparse
15 import os
16 import subprocess
17
18 # parse command line arguments
19
20 parser = argparse.ArgumentParser()
21 parser.add_argument('--text', help='output plain text format',
22                     action='store_true')
23
24 parser.add_argument('--config', help='load config from given file')
25
26
27 args = parser.parse_args()
28
29 # read config from json file
30
31 if args.config != None:
32     fp = open(args.config)
33 else:
34     nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
35
36     # read only the first line from the pipe
37     sha1 = subprocess.Popen(['git', '--git-dir', nmbhome,
38                              'show-ref', '-s', 'config'],
39                             stdout=subprocess.PIPE).stdout.readline()
40
41     sha1 = sha1.rstrip()
42
43     fp = subprocess.Popen(['git', '--git-dir', nmbhome,
44                            'cat-file', 'blob', sha1+':status-config.json'],
45                           stdout=subprocess.PIPE).stdout
46
47 config = json.load(fp)
48
49 if args.text:
50     output_format = 'text'
51 else:
52     output_format = 'html'
53
54 headers = ['date', 'from', 'subject']
55 last = {}
56
57 def clear_last():
58     for header in headers:
59         last[header] = ''
60
61 def print_view(title, query, comment):
62
63     query_string = ' and '.join(query)
64     q_new = notmuch.Query(db, query_string)
65     q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
66
67     last['thread_id'] = ''
68
69     if output_format == 'html':
70         print '<h3>%s</h3>' % title
71         print comment
72         print 'The view is generated from the following query:'
73         print '<blockquote>'
74         print query_string
75         print '</blockquote>'
76         print '<table>\n'
77
78     for m in q_new.search_messages():
79
80         out = {}
81
82         thread_id = m.get_thread_id()
83         if thread_id != last['thread_id']:
84             clear_last()
85
86         for header in headers:
87             val = m.get_header(header)
88
89             if header == 'date':
90                 val = str.join(' ', val.split(None)[1:4])
91                 val = str(datetime.datetime.strptime(val, '%d %b %Y').date())
92             elif header == 'from':
93                 (val, addr) = rfc822.parseaddr(val)
94                 if val == '':
95                     val = addr.split('@')[0]
96
97             if last[header] == val:
98                 out[header] = ''
99             else:
100                 out[header] = val.encode('utf-8')
101                 last[header] = val
102
103         mid = m.get_message_id()
104         out['id'] = 'id:"%s"' % mid
105
106         if output_format == 'html':
107             # XXX using <br /> is a hack, but ... // 20111216 too
108             if thread_id != last['thread_id']:
109                 br = '<br />'
110             else:
111                 br = ''
112
113             out['subject'] = '<a href="http://mid.gmane.org/%s">%s</a>' \
114                 % (urllib.quote(mid), out['subject'])
115
116             print ' <tr><td>%s %s' % (br, out['date'])
117             print '</td><td>%s %s' % (br, out['id'])
118             print '</td></tr>'
119             print ' <tr><td>%s' % out['from']
120             print '</td><td>%s' % out['subject']
121             print '</td></tr>\n'
122         else:
123             print '%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s\n' % out
124
125         last['thread_id'] = thread_id
126
127     if output_format == 'html':
128         print '</table>'
129
130 # main program
131
132 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
133
134 if output_format == 'html':
135     print '''<?xml version="1.0" encoding="utf-8" ?>
136 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
137 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
138 <head>
139 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
140 <title>Notmuch Patches</title>
141 </head>
142 <body>'''
143     print '<h2>Notmuch Patches</h2>'
144     print 'Generated: %s<br />' % datetime.datetime.utcnow().date()
145     print 'For more infomation see <a href="http://notmuchmail.org/nmbug">nmbug</a>'
146
147 for view in config['views']:
148     print_view(**view)
149
150 if output_format == 'html':
151     print '</body>\n</html>'