3 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
6 # - python3 or python2.7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see https://www.gnu.org/licenses/ .
21 """Generate text and/or HTML for one or more notmuch searches.
23 Messages matching each search are grouped by thread. Each message
24 that contains both a subject and message-id will have the displayed
25 subject link to an archive view of the message (defaulting to Gmane).
28 from __future__ import print_function
29 from __future__ import unicode_literals
36 from urllib.parse import quote
37 except ImportError: # Python 2
38 from urllib import quote
45 import xml.sax.saxutils
52 if not hasattr(collections, 'OrderedDict'): # Python 2.6 or earlier
53 class _OrderedDict (dict):
54 "Just enough of a stub to get through Page._get_threads"
55 def __init__(self, *args, **kwargs):
56 super(_OrderedDict, self).__init__(*args, **kwargs)
57 self._keys = [] # record key order
59 def __setitem__(self, key, value):
60 super(_OrderedDict, self).__setitem__(key, value)
61 self._keys.append(key)
64 for key in self._keys:
68 collections.OrderedDict = _OrderedDict
71 class ConfigError (Exception):
72 """Errors with config file usage
77 def read_config(path=None, encoding=None):
78 "Read config from json file"
83 with open(path, 'rb') as f:
84 config_bytes = f.read()
86 raise ConfigError('Could not read config from {}'.format(path))
88 nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
90 filename = 'notmuch-report.json'
92 # read only the first line from the pipe
93 sha1_bytes = subprocess.Popen(
94 ['git', '--git-dir', nmbhome, 'show-ref', '-s', '--heads', branch],
95 stdout=subprocess.PIPE).stdout.readline()
96 sha1 = sha1_bytes.decode(encoding).rstrip()
99 ("No local branch '{branch}' in {nmbgit}. "
100 'Checkout a local {branch} branch or explicitly set --config.'
101 ).format(branch=branch, nmbgit=nmbhome))
103 p = subprocess.Popen(
104 ['git', '--git-dir', nmbhome, 'cat-file', 'blob',
105 '{}:{}'.format(sha1, filename)],
106 stdout=subprocess.PIPE)
107 config_bytes, err = p.communicate()
111 ("Missing {filename} in branch '{branch}' of {nmbgit}. "
112 'Add the file or explicitly set --config.'
113 ).format(filename=filename, branch=branch, nmbgit=nmbhome))
115 config_json = config_bytes.decode(encoding)
117 return json.loads(config_json)
118 except ValueError as e:
120 path = "{} in branch '{}' of {}".format(
121 filename, branch, nmbhome)
123 'Could not parse JSON from the config file {}:\n{}'.format(
129 self.running_data = {}
133 def __init__(self, header=None, footer=None):
137 def write(self, database, views, stream=None):
140 byte_stream = sys.stdout.buffer
141 except AttributeError: # Python 2
142 byte_stream = sys.stdout
143 stream = codecs.getwriter(encoding=_ENCODING)(stream=byte_stream)
144 self._write_header(views=views, stream=stream)
146 self._write_view(database=database, view=view, stream=stream)
147 self._write_footer(views=views, stream=stream)
149 def _write_header(self, views, stream):
151 stream.write(self.header)
153 def _write_footer(self, views, stream):
155 stream.write(self.footer)
157 def _write_view(self, database, view, stream):
158 # sort order, default to oldest-first
159 sort_key = view.get('sort', 'oldest-first')
160 # dynamically accept all values in Query.SORT
161 sort_attribute = sort_key.upper().replace('-', '_')
163 sort = getattr(notmuch.Query.SORT, sort_attribute)
164 except AttributeError:
165 raise ConfigError('Invalid sort setting for {}: {!r}'.format(
166 view['title'], sort_key))
167 if 'query-string' not in view:
168 query = view['query']
169 view['query-string'] = ' and '.join(
170 '( {} )'.format(q) for q in query)
171 q = notmuch.Query(database, view['query-string'])
173 threads = self._get_threads(messages=q.search_messages())
174 self._write_view_header(view=view, stream=stream)
175 self._write_threads(threads=threads, stream=stream)
177 def _get_threads(self, messages):
178 threads = collections.OrderedDict()
179 for message in messages:
180 thread_id = message.get_thread_id()
181 if thread_id in threads:
182 thread = threads[thread_id]
185 threads[thread_id] = thread
186 thread.running_data, display_data = self._message_display_data(
187 running_data=thread.running_data, message=message)
188 thread.append(display_data)
189 return list(threads.values())
191 def _write_view_header(self, view, stream):
194 def _write_threads(self, threads, stream):
195 for thread in threads:
196 for message_display_data in thread:
198 ('{date:10.10s} {from:20.20s} {subject:40.40s}\n'
199 '{message-id-term:>72}\n'
200 ).format(**message_display_data))
201 if thread != threads[-1]:
204 def _message_display_data(self, running_data, message):
205 headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
207 for header in headers:
208 if header == 'thread-id':
209 value = message.get_thread_id()
210 elif header == 'message-id':
211 value = message.get_message_id()
212 data['message-id-term'] = 'id:"{0}"'.format(value)
213 elif header == 'date':
214 value = str(datetime.datetime.utcfromtimestamp(
215 message.get_date()).date())
217 value = message.get_header(header)
219 (value, addr) = email.utils.parseaddr(value)
221 value = addr.split('@')[0]
223 next_running_data = data.copy()
224 for header, value in data.items():
225 if header in ['message-id', 'subject']:
227 if value == running_data.get(header, None):
229 return (next_running_data, data)
232 class HtmlPage (Page):
233 _slug_regexp = re.compile('\W+')
235 def __init__(self, message_url_template, **kwargs):
236 self.message_url_template = message_url_template
237 super(HtmlPage, self).__init__(**kwargs)
239 def _write_header(self, views, stream):
240 super(HtmlPage, self)._write_header(views=views, stream=stream)
241 stream.write('<ul>\n')
244 view['id'] = self._slug(view['title'])
246 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
247 stream.write('</ul>\n')
249 def _write_view_header(self, view, stream):
250 stream.write('<h3 id="{id}">{title}</h3>\n'.format(**view))
251 stream.write('<p>\n')
252 if 'comment' in view:
253 stream.write(view['comment'])
256 'The view is generated from the following query:',
260 view['query-string'],
267 def _write_threads(self, threads, stream):
270 stream.write('<table>\n')
271 for thread in threads:
272 stream.write(' <tbody>\n')
273 for message_display_data in thread:
275 ' <tr class="message-first">\n'
277 ' <td><code>{message-id-term}</code></td>\n'
279 ' <tr class="message-last">\n'
281 ' <td>{subject}</td>\n'
283 ).format(**message_display_data))
284 stream.write(' </tbody>\n')
285 if thread != threads[-1]:
287 ' <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
288 stream.write('</table>\n')
290 def _message_display_data(self, *args, **kwargs):
291 running_data, display_data = super(
292 HtmlPage, self)._message_display_data(
294 if 'subject' in display_data and 'message-id' in display_data:
296 'message-id': quote(display_data['message-id']),
297 'subject': xml.sax.saxutils.escape(display_data['subject']),
299 d['url'] = self.message_url_template.format(**d)
300 display_data['subject'] = (
301 '<a href="{url}">{subject}</a>'
303 for key in ['message-id', 'from']:
304 if key in display_data:
305 display_data[key] = xml.sax.saxutils.escape(display_data[key])
306 return (running_data, display_data)
308 def _slug(self, string):
309 return self._slug_regexp.sub('-', string)
311 parser = argparse.ArgumentParser(description=__doc__)
313 '--text', action='store_true', help='output plain text format')
315 '--config', metavar='PATH',
316 help='load config from given file. '
317 'The format is described in notmuch-report.json(5).')
319 '--list-views', action='store_true', help='list views')
321 '--get-query', metavar='VIEW', help='get query for view')
324 args = parser.parse_args()
327 config = read_config(path=args.config)
328 except ConfigError as e:
329 print(e, file=sys.stderr)
332 header_template = config['meta'].get('header', '''<!DOCTYPE html>
335 <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
336 <title>{title}</title>
337 <style media="screen" type="text/css">
350 tr.message-first td {{
351 padding-top: {inter_message_padding};
353 tr.message-last td {{
354 padding-bottom: {inter_message_padding};
357 padding-left: {border_radius};
358 padding-right: {border_radius};
360 tr:first-child td:first-child {{
361 border-top-left-radius: {border_radius};
363 tr:first-child td:last-child {{
364 border-top-right-radius: {border_radius};
366 tr:last-child td:first-child {{
367 border-bottom-left-radius: {border_radius};
369 tr:last-child td:last-child {{
370 border-bottom-right-radius: {border_radius};
372 tbody:nth-child(4n+1) tr td {{
374 background-color: #ffd96e;
376 tbody:nth-child(4n+3) tr td {{
378 background-color: #bce;
384 background-color: #ccc;
396 footer_template = config['meta'].get('footer', '''
398 <p>Generated: {datetime}</p>
403 now = datetime.datetime.utcnow()
406 'datetime': now.strftime('%Y-%m-%d %H:%M:%SZ'),
407 'title': config['meta']['title'],
408 'blurb': config['meta']['blurb'],
409 'encoding': _ENCODING,
410 'inter_message_padding': '0.25em',
411 'border_radius': '0.5em',
414 _PAGES['text'] = Page()
415 _PAGES['html'] = HtmlPage(
416 header=header_template.format(**context),
417 footer=footer_template.format(**context),
418 message_url_template=config['meta'].get(
419 'message-url', 'https://mid.gmane.org/{message-id}'),
423 for view in config['views']:
426 elif args.get_query != None:
427 for view in config['views']:
428 if args.get_query == view['title']:
429 print(' and '.join('( {} )'.format(q) for q in view['query']))
432 # only import notmuch if needed
436 page = _PAGES['text']
438 page = _PAGES['html']
440 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
441 page.write(database=db, views=config['views'])