3 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
4 # License: Same as notmuch
6 # - python 2.6 for json
7 # - argparse; either python 2.7, or install separately
18 # parse command line arguments
20 parser = argparse.ArgumentParser()
21 parser.add_argument('--text', help='output plain text format',
23 parser.add_argument('--config', help='load config from given file')
24 parser.add_argument('--list-views', help='list views',
26 parser.add_argument('--get-query', help='get query for view')
28 args = parser.parse_args()
30 # read config from json file
32 if args.config != None:
33 fp = open(args.config)
35 nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
37 # read only the first line from the pipe
38 sha1 = subprocess.Popen(['git', '--git-dir', nmbhome,
39 'show-ref', '-s', 'config'],
40 stdout=subprocess.PIPE).stdout.readline()
44 fp = subprocess.Popen(['git', '--git-dir', nmbhome,
45 'cat-file', 'blob', sha1+':status-config.json'],
46 stdout=subprocess.PIPE).stdout
48 config = json.load(fp)
51 for view in config['views']:
54 elif args.get_query != None:
55 for view in config['views']:
56 if args.get_query == view['title']:
57 print ' and '.join(view['query'])
60 # only import notmuch if needed
64 output_format = 'text'
66 output_format = 'html'
69 def __init__(self, last, lines):
73 def join_utf8_with_newlines(self):
74 return '\n'.join( (line.encode('utf-8') for line in self.lines) )
76 def output_with_separator(threadlist, sep):
77 outputs = (thread.join_utf8_with_newlines() for thread in threadlist)
78 print sep.join(outputs)
80 headers = ['date', 'from', 'subject']
82 def print_view(title, query, comment):
84 query_string = ' and '.join(query)
85 q_new = notmuch.Query(db, query_string)
86 q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
95 if output_format == 'html':
96 print '<h3><a name="%s" />%s</h3>' % (title, title)
98 print 'The view is generated from the following query:'
101 print '</blockquote>'
104 for m in q_new.search_messages():
106 thread_id = m.get_thread_id()
108 if thread_id != last_thread_id:
109 if threads.has_key(thread_id):
110 last = threads[thread_id].last
111 lines = threads[thread_id].lines
115 thread = Thread(last, lines)
116 threads[thread_id] = thread
119 threadlist.append(thread)
120 last_thread_id = thread_id
122 for header in headers:
123 val = m.get_header(header)
126 val = str.join(' ', val.split(None)[1:4])
127 val = str(datetime.datetime.strptime(val, '%d %b %Y').date())
128 elif header == 'from':
129 (val, addr) = rfc822.parseaddr(val)
131 val = addr.split('@')[0]
133 if header != 'subject' and last[header] == val:
139 mid = m.get_message_id()
140 out['id'] = 'id:"%s"' % mid
142 if output_format == 'html':
144 out['subject'] = '<a href="http://mid.gmane.org/%s">%s</a>' \
145 % (urllib.quote(mid), out['subject'])
147 lines.append(' <tr><td>%s' % out['date'])
148 lines.append('</td><td>%s' % out['id'])
149 lines.append('</td></tr>')
150 lines.append(' <tr><td>%s' % out['from'])
151 lines.append('</td><td>%s' % out['subject'])
152 lines.append('</td></tr>')
154 lines.append('%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s' % out)
156 if output_format == 'html':
157 output_with_separator(threadlist,
158 '\n<tr><td colspan="2"><br /></td></tr>\n')
161 output_with_separator(threadlist, '\n\n')
165 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
167 if output_format == 'html':
168 print '''<?xml version="1.0" encoding="utf-8" ?>
169 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
170 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
172 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
173 <title>Notmuch Patches</title>
176 print '<h2>Notmuch Patches</h2>'
177 print 'Generated: %s<br />' % datetime.datetime.utcnow().date()
178 print 'For more infomation see <a href="http://notmuchmail.org/nmbug">nmbug</a>'
180 print '<h3>Views</h3>'
182 for view in config['views']:
183 print '<li><a href="#%(title)s">%(title)s</a></li>' % view
186 for view in config['views']:
189 if output_format == 'html':
190 print '</body>\n</html>'