]> git.cworth.org Git - obsolete/notmuch-to-html/blob - notmuch-to-html
Move the generated date from the top of the page to the footer.
[obsolete/notmuch-to-html] / notmuch-to-html
1 #!/usr/bin/python
2 #
3 # Generate an HTML page with the result of one or more notmuch
4 # searches, (with links to gmane views of each email if available).
5 #
6 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
7 #
8 # dependencies
9 #       - python 2.6 for json
10 #       - argparse; either python 2.7, or install separately
11 #
12 # This program is free software: you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation, either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program.  If not, see http://www.gnu.org/licenses/ .
24
25 from __future__ import print_function
26 from __future__ import unicode_literals
27
28 import codecs
29 import collections
30 import datetime
31 import email.utils
32 try:  # Python 3
33     from urllib.parse import quote
34 except ImportError:  # Python 2
35     from urllib import quote
36 import json
37 import argparse
38 import os
39 import re
40 import sys
41 import subprocess
42 import xml.sax.saxutils
43
44
45 _ENCODING = 'UTF-8'
46 _PAGES = {}
47
48
49 if not hasattr(collections, 'OrderedDict'):  # Python 2.6 or earlier
50     class _OrderedDict (dict):
51         "Just enough of a stub to get through Page._get_threads"
52         def __init__(self, *args, **kwargs):
53             super(_OrderedDict, self).__init__(*args, **kwargs)
54             self._keys = []  # record key order
55
56         def __setitem__(self, key, value):
57             super(_OrderedDict, self).__setitem__(key, value)
58             self._keys.append(key)
59
60         def values(self):
61             for key in self._keys:
62                 yield self[key]
63
64
65     collections.OrderedDict = _OrderedDict
66
67
68 def read_config(path, encoding=None):
69     "Read config from json file"
70     if not encoding:
71         encoding = _ENCODING
72     fp = open(path)
73     return json.load(fp)
74
75
76 class Thread (list):
77     def __init__(self):
78         self.running_data = {}
79
80
81 class Page (object):
82     def __init__(self, header=None, footer=None):
83         self.header = header
84         self.footer = footer
85
86     def write(self, database, views, stream=None):
87         if not stream:
88             try:  # Python 3
89                 byte_stream = sys.stdout.buffer
90             except AttributeError:  # Python 2
91                 byte_stream = sys.stdout
92             stream = codecs.getwriter(encoding=_ENCODING)(stream=byte_stream)
93         self._write_header(views=views, stream=stream)
94         for view in views:
95             self._write_view(database=database, view=view, stream=stream)
96         self._write_footer(views=views, stream=stream)
97
98     def _write_header(self, views, stream):
99         if self.header:
100             stream.write(self.header)
101
102     def _write_footer(self, views, stream):
103         if self.footer:
104             stream.write(self.footer)
105
106     def _write_view(self, database, view, stream):
107         if 'query-string' not in view:
108             query = view['query']
109             view['query-string'] = ' and '.join(query)
110         q = notmuch.Query(database, view['query-string'])
111         q.set_sort(notmuch.Query.SORT.OLDEST_FIRST)
112         threads = self._get_threads(messages=q.search_messages())
113         self._write_view_header(view=view, stream=stream)
114         self._write_threads(threads=threads, stream=stream)
115
116     def _get_threads(self, messages):
117         threads = collections.OrderedDict()
118         for message in messages:
119             thread_id = message.get_thread_id()
120             if thread_id in threads:
121                 thread = threads[thread_id]
122             else:
123                 thread = Thread()
124                 threads[thread_id] = thread
125             thread.running_data, display_data = self._message_display_data(
126                 running_data=thread.running_data, message=message)
127             thread.append(display_data)
128         return list(threads.values())
129
130     def _write_view_header(self, view, stream):
131         pass
132
133     def _write_threads(self, threads, stream):
134         for thread in threads:
135             for message_display_data in thread:
136                 stream.write(
137                     ('{date:10.10s} {from:20.20s} {subject:40.40s}\n'
138                      '{message-id-term:>72}\n'
139                      ).format(**message_display_data))
140             if thread != threads[-1]:
141                 stream.write('\n')
142
143     def _message_display_data(self, running_data, message):
144         headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
145         data = {}
146         for header in headers:
147             if header == 'thread-id':
148                 value = message.get_thread_id()
149             elif header == 'message-id':
150                 value = message.get_message_id()
151                 data['message-id-term'] = 'id:"{0}"'.format(value)
152             elif header == 'date':
153                 value = str(datetime.datetime.utcfromtimestamp(
154                     message.get_date()).date())
155             else:
156                 value = message.get_header(header)
157             if header == 'from':
158                 (value, addr) = email.utils.parseaddr(value)
159                 if not value:
160                     value = addr.split('@')[0]
161             data[header] = value
162         next_running_data = data.copy()
163         for header, value in data.items():
164             if header in ['message-id', 'subject']:
165                 continue
166             if value == running_data.get(header, None):
167                 data[header] = ''
168         return (next_running_data, data)
169
170
171 class HtmlPage (Page):
172     _slug_regexp = re.compile('\W+')
173
174     def _write_header(self, views, stream):
175         super(HtmlPage, self)._write_header(views=views, stream=stream)
176         stream.write('<ul>\n')
177         for view in views:
178             if 'id' not in view:
179                 view['id'] = self._slug(view['title'])
180             stream.write(
181                 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
182         stream.write('</ul>\n')
183
184     def _write_view_header(self, view, stream):
185         stream.write('<h3 id="{id}">{title}</h3>\n'.format(**view))
186         stream.write('<p>\n')
187         if 'comment' in view:
188             stream.write(view['comment'])
189             stream.write('\n')
190         for line in [
191                 '<p>This view is generated from the following query:',
192                 '</p>',
193                 '<p>',
194                 '  <code>',
195                 'notmuch search ' + view['query-string'],
196                 '  </code>',
197                 '</p>',
198                 ]:
199             stream.write(line)
200             stream.write('\n')
201
202     def _write_threads(self, threads, stream):
203         if not threads:
204             return
205         stream.write('<table>\n')
206         for thread in threads:
207             stream.write('  <tbody>\n')
208             for message_display_data in thread:
209                 stream.write((
210                     '    <tr class="message-first">\n'
211                     '      <td>{from}</td>\n'
212                     '      <td>{date}</td>\n'
213                     '      <td>{subject}</td>\n'
214                     '    </tr>\n'
215                     ).format(**message_display_data))
216             stream.write('  </tbody>\n')
217             if thread != threads[-1]:
218                 stream.write(
219                     '  <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
220         stream.write('</table>\n')
221
222     def _message_display_data(self, *args, **kwargs):
223         running_data, display_data = super(
224             HtmlPage, self)._message_display_data(
225                 *args, **kwargs)
226         if 'subject' in display_data and 'message-id' in display_data:
227             d = {
228                 'message-id': quote(display_data['message-id']),
229                 'subject': xml.sax.saxutils.escape(display_data['subject']),
230                 }
231             display_data['subject'] = (
232                 '<a href="http://mid.gmane.org/{message-id}">{subject}</a>'
233                 ).format(**d)
234         for key in ['message-id', 'from']:
235             if key in display_data:
236                 display_data[key] = xml.sax.saxutils.escape(display_data[key])
237         return (running_data, display_data)
238
239     def _slug(self, string):
240         return self._slug_regexp.sub('-', string)
241
242 parser = argparse.ArgumentParser()
243 parser.add_argument('config', help='path to configuration file', metavar='CONFIG_FILE')
244 parser.add_argument('--text', help='output plain text format',
245                     action='store_true')
246 parser.add_argument('--list-views', help='list views',
247                     action='store_true')
248 parser.add_argument('--get-query', help='get query for view',
249                     metavar='VIEW')
250
251 args = parser.parse_args()
252
253 config = read_config(path=args.config)
254
255 _PAGES['text'] = Page()
256 _PAGES['html'] = HtmlPage(
257     header='''<!DOCTYPE html>
258 <html lang="en">
259 <head>
260   <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
261   <title>{title}</title>
262   <style media="screen" type="text/css">
263     table {{
264       border-spacing: 0;
265     }}
266     tr.message-first td {{
267       padding-top: {inter_message_padding};
268     }}
269     tr.message-last td {{
270       padding-bottom: {inter_message_padding};
271     }}
272     td {{
273       padding-left: {border_radius};
274       padding-right: {border_radius};
275     }}
276     tr:first-child td:first-child {{
277       border-top-left-radius: {border_radius};
278     }}
279     tr:first-child td:last-child {{
280       border-top-right-radius: {border_radius};
281     }}
282     tr:last-child td:first-child {{
283       border-bottom-left-radius: {border_radius};
284     }}
285     tr:last-child td:last-child {{
286       border-bottom-right-radius: {border_radius};
287     }}
288     tbody:nth-child(4n+1) tr td {{
289       background-color: #ffd96e;
290     }}
291     tbody:nth-child(4n+3) tr td {{
292       background-color: #bce;
293     }}
294   </style>
295 </head>
296 <body>
297 <h2>{title}</h2>
298 {blurb}
299 </p>
300 <h3>Views</h3>
301 '''.format(title=config['meta']['title'],
302            blurb=config['meta']['blurb'],
303            encoding=_ENCODING,
304            inter_message_padding='0.25em',
305            border_radius='0.5em'),
306     footer='<hr><p>Generated: {date}</body>\n</html>\n'.format(date=datetime.datetime.utcnow().date())
307     )
308
309 if args.list_views:
310     for view in config['views']:
311         print(view['title'])
312     sys.exit(0)
313 elif args.get_query != None:
314     for view in config['views']:
315         if args.get_query == view['title']:
316             print(' and '.join(view['query']))
317     sys.exit(0)
318 else:
319     # only import notmuch if needed
320     import notmuch
321
322 if args.text:
323     page = _PAGES['text']
324 else:
325     page = _PAGES['html']
326
327 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
328 page.write(database=db, views=config['views'])