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',
24 parser.add_argument('--config', help='load config from given file')
27 args = parser.parse_args()
29 # read config from json file
31 if args.config != None:
32 fp = open(args.config)
34 nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
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()
43 fp = subprocess.Popen(['git', '--git-dir', nmbhome,
44 'cat-file', 'blob', sha1+':status-config.json'],
45 stdout=subprocess.PIPE).stdout
47 config = json.load(fp)
50 output_format = 'text'
52 output_format = 'html'
55 def __init__(self, last, lines):
59 def join_utf8_with_newlines(self):
60 return '\n'.join( (line.encode('utf-8') for line in self.lines) )
62 def output_with_separator(threadlist, sep):
63 outputs = (thread.join_utf8_with_newlines() for thread in threadlist)
64 print sep.join(outputs)
66 headers = ['date', 'from', 'subject']
68 def print_view(title, query, comment):
70 query_string = ' and '.join(query)
71 q_new = notmuch.Query(db, query_string)
72 q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
81 if output_format == 'html':
82 print '<h3><a name="%s" />%s</h3>' % (title, title)
84 print 'The view is generated from the following query:'
90 for m in q_new.search_messages():
92 thread_id = m.get_thread_id()
94 if thread_id != last_thread_id:
95 if threads.has_key(thread_id):
96 last = threads[thread_id].last
97 lines = threads[thread_id].lines
101 thread = Thread(last, lines)
102 threads[thread_id] = thread
105 threadlist.append(thread)
106 last_thread_id = thread_id
108 for header in headers:
109 val = m.get_header(header)
112 val = str.join(' ', val.split(None)[1:4])
113 val = str(datetime.datetime.strptime(val, '%d %b %Y').date())
114 elif header == 'from':
115 (val, addr) = rfc822.parseaddr(val)
117 val = addr.split('@')[0]
119 if header != 'subject' and last[header] == val:
125 mid = m.get_message_id()
126 out['id'] = 'id:"%s"' % mid
128 if output_format == 'html':
130 out['subject'] = '<a href="http://mid.gmane.org/%s">%s</a>' \
131 % (urllib.quote(mid), out['subject'])
133 lines.append(' <tr><td>%s' % out['date'])
134 lines.append('</td><td>%s' % out['id'])
135 lines.append('</td></tr>')
136 lines.append(' <tr><td>%s' % out['from'])
137 lines.append('</td><td>%s' % out['subject'])
138 lines.append('</td></tr>')
140 lines.append('%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s' % out)
142 if output_format == 'html':
143 output_with_separator(threadlist,
144 '\n<tr><td colspan="2"><br /></td></tr>\n')
147 output_with_separator(threadlist, '\n\n')
151 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
153 if output_format == 'html':
154 print '''<?xml version="1.0" encoding="utf-8" ?>
155 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
156 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
158 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
159 <title>Notmuch Patches</title>
162 print '<h2>Notmuch Patches</h2>'
163 print 'Generated: %s<br />' % datetime.datetime.utcnow().date()
164 print 'For more infomation see <a href="http://notmuchmail.org/nmbug">nmbug</a>'
166 print '<h3>Views</h3>'
168 for view in config['views']:
169 print '<li><a href="#%(title)s">%(title)s</a></li>' % view
172 for view in config['views']:
175 if output_format == 'html':
176 print '</body>\n</html>'