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)
68 last['thread_id'] = ''
70 if output_format == 'html':
71 print '<h3>%s</h3>' % title
73 print 'The view is generated from the following query:'
79 for m in q_new.search_messages():
83 thread_id = m.get_thread_id()
84 if thread_id != last['thread_id']:
87 for header in headers:
88 val = m.get_header(header)
91 val = str.join(' ', val.split(None)[1:4])
92 val = str(datetime.datetime.strptime(val, '%d %b %Y').date())
93 elif header == 'from':
94 val = rfc822.parseaddr(val)[0]
96 if last[header] == val:
99 out[header] = val.encode('utf-8')
102 mid = m.get_message_id()
103 out['id'] = 'id:"%s"' % mid
105 if output_format == 'html':
106 # XXX using <br /> is a hack, but ... // 20111216 too
107 if thread_id != last['thread_id']:
111 out['subject'] = '<a href="http://mid.gmane.org/%s">%s</a>' \
112 % (urllib.quote(mid), out['subject'])
114 print " <tr><td>%s %s" % (br, out['date'])
115 print "</td><td>%s %s" % (br, out['id'])
117 print " <tr><td>%s" % out['from']
118 print "</td><td>%s" % out['subject']
121 print '%(date)-10.10s %(from)-20.20s %(subject)-40.40s\n%(id)72s\n' % out
123 last['thread_id'] = thread_id
125 if output_format == 'html':
130 db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
132 if output_format == 'html':
133 print '''<?xml version="1.0" encoding="utf-8" ?>
134 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
135 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
137 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
138 <title>Notmuch Patches</title>
141 print '<h2>Notmuch Patches</h2>'
142 print 'Generated: %s<br />' % datetime.datetime.utcnow().date()
143 print 'For more infomation see <a href="http://notmuchmail.org/nmbug">nmbug</a>'
145 for view in config['views']:
148 if output_format == 'html':
149 print '</body>\n</html>'