3 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
6 # - python 2.6 for json
7 # - argparse; either python 2.7, or install separately
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see http://www.gnu.org/licenses/ .
22 from __future__ import print_function
23 from __future__ import unicode_literals
30 from urllib.parse import quote
31 except ImportError: # Python 2
32 from urllib import quote
39 import xml.sax.saxutils
46 if not hasattr(collections, 'OrderedDict'): # Python 2.6 or earlier
47 class _OrderedDict (dict):
48 "Just enough of a stub to get through Page._get_threads"
49 def __init__(self, *args, **kwargs):
50 super(_OrderedDict, self).__init__(*args, **kwargs)
51 self._keys = [] # record key order
53 def __setitem__(self, key, value):
54 super(_OrderedDict, self).__setitem__(key, value)
55 self._keys.append(key)
58 for key in self._keys:
62 collections.OrderedDict = _OrderedDict
65 def read_config(path=None, encoding=None):
66 "Read config from json file"
72 nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
74 # read only the first line from the pipe
75 sha1_bytes = subprocess.Popen(
76 ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],
77 stdout=subprocess.PIPE).stdout.readline()
78 sha1 = sha1_bytes.decode(encoding).rstrip()
80 fp_byte_stream = subprocess.Popen(
81 ['git', '--git-dir', nmbhome, 'cat-file', 'blob',
82 sha1+':status-config.json'],
83 stdout=subprocess.PIPE).stdout
84 fp = codecs.getreader(encoding=encoding)(stream=fp_byte_stream)
91 self.running_data = {}
95 def __init__(self, header=None, footer=None):
99 def write(self, database, views, stream=None):
102 byte_stream = sys.stdout.buffer
103 except AttributeError: # Python 2
104 byte_stream = sys.stdout
105 stream = codecs.getwriter(encoding=_ENCODING)(stream=byte_stream)
106 self._write_header(views=views, stream=stream)
108 self._write_view(database=database, view=view, stream=stream)
109 self._write_footer(views=views, stream=stream)
111 def _write_header(self, views, stream):
113 stream.write(self.header)
115 def _write_footer(self, views, stream):
117 stream.write(self.footer)
119 def _write_view(self, database, view, stream):
120 if 'query-string' not in view:
121 query = view['query']
122 view['query-string'] = ' and '.join(query)
123 q = notmuch.Query(database, view['query-string'])
124 q.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
125 threads = self._get_threads(messages=q.search_messages())
126 self._write_view_header(view=view, stream=stream)
127 self._write_threads(threads=threads, stream=stream)
129 def _get_threads(self, messages):
130 threads = collections.OrderedDict()
131 for message in messages:
132 thread_id = message.get_thread_id()
133 if thread_id in threads:
134 thread = threads[thread_id]
137 threads[thread_id] = thread
138 thread.running_data, display_data = self._message_display_data(
139 running_data=thread.running_data, message=message)
140 thread.append(display_data)
141 return list(threads.values())
143 def _write_view_header(self, view, stream):
146 def _write_threads(self, threads, stream):
147 for thread in threads:
148 for message_display_data in thread:
150 ('{date:10.10s} {from:20.20s} {subject:40.40s}\n'
151 '{message-id-term:>72}\n'
152 ).format(**message_display_data))
153 if thread != threads[-1]:
156 def _message_display_data(self, running_data, message):
157 headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
159 for header in headers:
160 if header == 'thread-id':
161 value = message.get_thread_id()
162 elif header == 'message-id':
163 value = message.get_message_id()
164 data['message-id-term'] = 'id:"{0}"'.format(value)
165 elif header == 'date':
166 value = str(datetime.datetime.utcfromtimestamp(
167 message.get_date()).date())
169 value = message.get_header(header)
171 (value, addr) = email.utils.parseaddr(value)
173 value = addr.split('@')[0]
175 next_running_data = data.copy()
176 for header, value in data.items():
177 if header in ['message-id', 'subject']:
179 if value == running_data.get(header, None):
181 return (next_running_data, data)
184 class HtmlPage (Page):
185 _slug_regexp = re.compile('\W+')
187 def _write_header(self, views, stream):
188 super(HtmlPage, self)._write_header(views=views, stream=stream)
189 stream.write('<ul>\n')
192 view['id'] = self._slug(view['title'])
194 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
195 stream.write('</ul>\n')
197 def _write_view_header(self, view, stream):
198 stream.write('<h3 id="{id}">{title}</h3>\n'.format(**view))
199 stream.write('<p>\n')
200 if 'comment' in view:
201 stream.write(view['comment'])
204 'The view is generated from the following query:',
208 view['query-string'],
215 def _write_threads(self, threads, stream):
218 stream.write('<table>\n')
219 for thread in threads:
220 stream.write(' <tbody>\n')
221 for message_display_data in thread:
223 ' <tr class="message-first">\n'
226 ' <td>{subject}</td>\n'
228 ).format(**message_display_data))
229 stream.write(' </tbody>\n')
230 if thread != threads[-1]:
232 ' <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
233 stream.write('</table>\n')
235 def _message_display_data(self, *args, **kwargs):
236 running_data, display_data = super(
237 HtmlPage, self)._message_display_data(
239 if 'subject' in display_data and 'message-id' in display_data:
241 'message-id': quote(display_data['message-id']),
242 'subject': xml.sax.saxutils.escape(display_data['subject']),
244 display_data['subject'] = (
245 '<a href="http://mid.gmane.org/{message-id}">{subject}</a>'
247 for key in ['message-id', 'from']:
248 if key in display_data:
249 display_data[key] = xml.sax.saxutils.escape(display_data[key])
250 return (running_data, display_data)
252 def _slug(self, string):
253 return self._slug_regexp.sub('-', string)
255 parser = argparse.ArgumentParser()
256 parser.add_argument('--text', help='output plain text format',
258 parser.add_argument('--config', help='load config from given file',
260 parser.add_argument('--list-views', help='list views',
262 parser.add_argument('--get-query', help='get query for view',
265 args = parser.parse_args()
267 config = read_config(path=args.config)
269 _PAGES['text'] = Page()
270 _PAGES['html'] = HtmlPage(
271 header='''<!DOCTYPE html>
274 <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
275 <title>{title}</title>
276 <style media="screen" type="text/css">
280 tr.message-first td {{
281 padding-top: {inter_message_padding};
283 tr.message-last td {{
284 padding-bottom: {inter_message_padding};
287 padding-left: {border_radius};
288 padding-right: {border_radius};
290 tr:first-child td:first-child {{
291 border-top-left-radius: {border_radius};
293 tr:first-child td:last-child {{
294 border-top-right-radius: {border_radius};
296 tr:last-child td:first-child {{
297 border-bottom-left-radius: {border_radius};
299 tr:last-child td:last-child {{
300 border-bottom-right-radius: {border_radius};
302 tbody:nth-child(4n+1) tr td {{
303 background-color: #ffd96e;
305 tbody:nth-child(4n+3) tr td {{
306 background-color: #bce;
313 Generated: {date}<br />
317 '''.format(date=datetime.datetime.utcnow().date(),
318 title=config['meta']['title'],
319 blurb=config['meta']['blurb'],
321 inter_message_padding='0.25em',
322 border_radius='0.5em'),
323 footer='</body>\n</html>\n',
327 for view in config['views']:
330 elif args.get_query != None:
331 for view in config['views']:
332 if args.get_query == view['title']:
333 print(' and '.join(view['query']))
336 # only import notmuch if needed
340 page = _PAGES['text']
342 page = _PAGES['html']
344 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
345 page.write(database=db, views=config['views'])