]> git.notmuchmail.org Git - notmuch/blob - devel/printmimestructure
notmuch (0.27-2) unstable; urgency=medium
[notmuch] / devel / printmimestructure
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
5 # License: GPLv3+
6
7 # This script reads a MIME message from stdin and produces a treelike
8 # representation on it stdout.
9
10 # Example:
11 #
12 # 0 dkg@alice:~$ printmimestructure < 'Maildir/cur/1269025522.M338697P12023.monkey,S=6459,W=6963:2,Sa'
13 # └┬╴multipart/signed 6546 bytes
14 #  ├─╴text/plain inline 895 bytes
15 #  └─╴application/pgp-signature inline [signature.asc] 836 bytes
16 # 0 dkg@alice:~$
17
18
19 # If you want to number the parts, i suggest piping the output through
20 # something like "cat -n"
21
22 import email
23 import sys
24
25 def test(z, prefix=''):
26     fname = '' if z.get_filename() is None else ' [' + z.get_filename() + ']'
27     cset = '' if z.get_charset() is None else ' (' + z.get_charset() + ')'
28     disp = z.get_params(None, header='Content-Disposition')
29     if (disp is None):
30         disposition = ''
31     else:
32         disposition = ''
33         for d in disp:
34             if d[0] in [ 'attachment', 'inline' ]:
35                 disposition = ' ' + d[0]
36     if (z.is_multipart()):
37         print prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes'
38         if prefix.endswith('└'):
39             prefix = prefix.rpartition('└')[0] + ' '
40         if prefix.endswith('├'):
41             prefix = prefix.rpartition('├')[0] + '│'
42         parts = z.get_payload()
43         i = 0
44         while (i < parts.__len__()-1):
45             test(parts[i], prefix + '├')
46             i += 1
47         test(parts[i], prefix + '└')
48         # FIXME: show epilogue?
49     else:
50         print prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes'
51
52 test(email.message_from_file(sys.stdin), '└')