]> git.cworth.org Git - obsolete/notmuch-to-html/blob - notmuch-to-html
Add explicit license information
[obsolete/notmuch-to-html] / notmuch-to-html
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2011-2012 David Bremner <david@tethera.net>
4 #
5 # dependencies
6 #       - python 2.6 for json
7 #       - argparse; either python 2.7, or install separately
8 #
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.
13 #
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.
18 #
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/ .
21
22 from __future__ import print_function
23 from __future__ import unicode_literals
24
25 import codecs
26 import collections
27 import datetime
28 import email.utils
29 try:  # Python 3
30     from urllib.parse import quote
31 except ImportError:  # Python 2
32     from urllib import quote
33 import json
34 import argparse
35 import os
36 import re
37 import sys
38 import subprocess
39 import xml.sax.saxutils
40
41
42 _ENCODING = 'UTF-8'
43 _PAGES = {}
44
45
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
52
53         def __setitem__(self, key, value):
54             super(_OrderedDict, self).__setitem__(key, value)
55             self._keys.append(key)
56
57         def values(self):
58             for key in self._keys:
59                 yield self[key]
60
61
62     collections.OrderedDict = _OrderedDict
63
64
65 def read_config(path=None, encoding=None):
66     "Read config from json file"
67     if not encoding:
68         encoding = _ENCODING
69     if path:
70         fp = open(path)
71     else:
72         nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))
73
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()
79
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)
85
86     return json.load(fp)
87
88
89 class Thread (list):
90     def __init__(self):
91         self.running_data = {}
92
93
94 class Page (object):
95     def __init__(self, header=None, footer=None):
96         self.header = header
97         self.footer = footer
98
99     def write(self, database, views, stream=None):
100         if not stream:
101             try:  # Python 3
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)
107         for view in views:
108             self._write_view(database=database, view=view, stream=stream)
109         self._write_footer(views=views, stream=stream)
110
111     def _write_header(self, views, stream):
112         if self.header:
113             stream.write(self.header)
114
115     def _write_footer(self, views, stream):
116         if self.footer:
117             stream.write(self.footer)
118
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)
128
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]
135             else:
136                 thread = Thread()
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())
142
143     def _write_view_header(self, view, stream):
144         pass
145
146     def _write_threads(self, threads, stream):
147         for thread in threads:
148             for message_display_data in thread:
149                 stream.write(
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]:
154                 stream.write('\n')
155
156     def _message_display_data(self, running_data, message):
157         headers = ('thread-id', 'message-id', 'date', 'from', 'subject')
158         data = {}
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())
168             else:
169                 value = message.get_header(header)
170             if header == 'from':
171                 (value, addr) = email.utils.parseaddr(value)
172                 if not value:
173                     value = addr.split('@')[0]
174             data[header] = value
175         next_running_data = data.copy()
176         for header, value in data.items():
177             if header in ['message-id', 'subject']:
178                 continue
179             if value == running_data.get(header, None):
180                 data[header] = ''
181         return (next_running_data, data)
182
183
184 class HtmlPage (Page):
185     _slug_regexp = re.compile('\W+')
186
187     def _write_header(self, views, stream):
188         super(HtmlPage, self)._write_header(views=views, stream=stream)
189         stream.write('<ul>\n')
190         for view in views:
191             if 'id' not in view:
192                 view['id'] = self._slug(view['title'])
193             stream.write(
194                 '<li><a href="#{id}">{title}</a></li>\n'.format(**view))
195         stream.write('</ul>\n')
196
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'])
202             stream.write('\n')
203         for line in [
204                 'The view is generated from the following query:',
205                 '</p>',
206                 '<p>',
207                 '  <code>',
208                 view['query-string'],
209                 '  </code>',
210                 '</p>',
211                 ]:
212             stream.write(line)
213             stream.write('\n')
214
215     def _write_threads(self, threads, stream):
216         if not threads:
217             return
218         stream.write('<table>\n')
219         for thread in threads:
220             stream.write('  <tbody>\n')
221             for message_display_data in thread:
222                 stream.write((
223                     '    <tr class="message-first">\n'
224                     '      <td>{from}</td>\n'
225                     '      <td>{date}</td>\n'
226                     '      <td>{subject}</td>\n'
227                     '    </tr>\n'
228                     ).format(**message_display_data))
229             stream.write('  </tbody>\n')
230             if thread != threads[-1]:
231                 stream.write(
232                     '  <tbody><tr><td colspan="2"><br /></td></tr></tbody>\n')
233         stream.write('</table>\n')
234
235     def _message_display_data(self, *args, **kwargs):
236         running_data, display_data = super(
237             HtmlPage, self)._message_display_data(
238                 *args, **kwargs)
239         if 'subject' in display_data and 'message-id' in display_data:
240             d = {
241                 'message-id': quote(display_data['message-id']),
242                 'subject': xml.sax.saxutils.escape(display_data['subject']),
243                 }
244             display_data['subject'] = (
245                 '<a href="http://mid.gmane.org/{message-id}">{subject}</a>'
246                 ).format(**d)
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)
251
252     def _slug(self, string):
253         return self._slug_regexp.sub('-', string)
254
255 parser = argparse.ArgumentParser()
256 parser.add_argument('--text', help='output plain text format',
257                     action='store_true')
258 parser.add_argument('--config', help='load config from given file',
259                     metavar='PATH')
260 parser.add_argument('--list-views', help='list views',
261                     action='store_true')
262 parser.add_argument('--get-query', help='get query for view',
263                     metavar='VIEW')
264
265 args = parser.parse_args()
266
267 config = read_config(path=args.config)
268
269 _PAGES['text'] = Page()
270 _PAGES['html'] = HtmlPage(
271     header='''<!DOCTYPE html>
272 <html lang="en">
273 <head>
274   <meta http-equiv="Content-Type" content="text/html; charset={encoding}" />
275   <title>{title}</title>
276   <style media="screen" type="text/css">
277     table {{
278       border-spacing: 0;
279     }}
280     tr.message-first td {{
281       padding-top: {inter_message_padding};
282     }}
283     tr.message-last td {{
284       padding-bottom: {inter_message_padding};
285     }}
286     td {{
287       padding-left: {border_radius};
288       padding-right: {border_radius};
289     }}
290     tr:first-child td:first-child {{
291       border-top-left-radius: {border_radius};
292     }}
293     tr:first-child td:last-child {{
294       border-top-right-radius: {border_radius};
295     }}
296     tr:last-child td:first-child {{
297       border-bottom-left-radius: {border_radius};
298     }}
299     tr:last-child td:last-child {{
300       border-bottom-right-radius: {border_radius};
301     }}
302     tbody:nth-child(4n+1) tr td {{
303       background-color: #ffd96e;
304     }}
305     tbody:nth-child(4n+3) tr td {{
306       background-color: #bce;
307     }}
308   </style>
309 </head>
310 <body>
311 <h2>{title}</h2>
312 <p>
313 Generated: {date}<br />
314 {blurb}
315 </p>
316 <h3>Views</h3>
317 '''.format(date=datetime.datetime.utcnow().date(),
318            title=config['meta']['title'],
319            blurb=config['meta']['blurb'],
320            encoding=_ENCODING,
321            inter_message_padding='0.25em',
322            border_radius='0.5em'),
323     footer='</body>\n</html>\n',
324     )
325
326 if args.list_views:
327     for view in config['views']:
328         print(view['title'])
329     sys.exit(0)
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']))
334     sys.exit(0)
335 else:
336     # only import notmuch if needed
337     import notmuch
338
339 if args.text:
340     page = _PAGES['text']
341 else:
342     page = _PAGES['html']
343
344 db = notmuch.Database(mode=notmuch.Database.MODE.READ_ONLY)
345 page.write(database=db, views=config['views'])