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'
54 headers = ['date', 'from', 'subject']
58 for header in headers:
61 def print_view(title, query, comment):
63 query_string = ' and '.join(query)
64 q_new = notmuch.Query(db, query_string)
65 q_new.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
67 last['thread_id'] = ''
69 if output_format == 'html':
70 print '<h3><a name="%s" />%s</h3>' % (title, title)
72 print 'The view is generated from the following query:'
78 for m in q_new.search_messages():
82 thread_id = m.get_thread_id()
83 if thread_id != last['thread_id']:
86 for header in headers:
87 val = m.get_header(header)
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)
95 val = addr.split('@')[0]
97 if last[header] == val:
100 out[header] = val.encode('utf-8')
103 mid = m.get_message_id()
104 out['id'] = 'id:"%s"' % mid
106 if output_format == 'html':
107 # XXX using <br /> is a hack, but ... // 20111216 too
108 if thread_id != last['thread_id']:
113 out['subject'] = '<a href="http://mid.gmane.org/%s">%s</a>' \
114 % (urllib.quote(mid), out['subject'])
116 print ' <tr><td>%s %s' % (br, out['date'])
117 print '</td><td>%s %s' % (br, out['id'])
119 print ' <tr><td>%s' % out['from']
120 print '</td><td>%s' % out['subject']
123 print '%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s\n' % out
125 last['thread_id'] = thread_id
127 if output_format == 'html':
132 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
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">
139 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
140 <title>Notmuch Patches</title>
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>'
147 print '<h3>Views</h3>'
149 for view in config['views']:
150 print '<li><a href="#%(title)s">%(title)s</a></li>' % view
153 for view in config['views']:
156 if output_format == 'html':
157 print '</body>\n</html>'