unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob ab2a667379186e2458e923ec221fce85431d36e5 1963 bytes (raw)
name: devel/printmimestructure 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
# License: GPLv3+

# This script reads a MIME message from stdin and produces a treelike
# representation on it stdout.

# Example:
#
# 0 dkg@alice:~$ printmimestructure < 'Maildir/cur/1269025522.M338697P12023.monkey,S=6459,W=6963:2,Sa'
# └┬╴multipart/signed 6546 bytes
#  ├─╴text/plain inline 895 bytes
#  └─╴application/pgp-signature inline [signature.asc] 836 bytes
# 0 dkg@alice:~$


# If you want to number the parts, i suggest piping the output through
# something like "cat -n"

from __future__ import print_function

import email
import sys

def print_part(z, prefix):
    fname = '' if z.get_filename() is None else ' [' + z.get_filename() + ']'
    cset = '' if z.get_charset() is None else ' (' + z.get_charset() + ')'
    disp = z.get_params(None, header='Content-Disposition')
    if (disp is None):
        disposition = ''
    else:
        disposition = ''
        for d in disp:
            if d[0] in [ 'attachment', 'inline' ]:
                disposition = ' ' + d[0]
    if z.is_multipart():
        nbytes = len(z.as_string())
    else:
        nbytes = len(z.get_payload())

    print('{}{}{}{}{} {:d} bytes'.format(
        prefix,
        z.get_content_type(),
        cset,
        disposition,
        fname,
        nbytes,
    ))

def test(z, prefix=''):
    if (z.is_multipart()):
        print_part(z, prefix+'┬╴')
        if prefix.endswith('└'):
            prefix = prefix.rpartition('└')[0] + ' '
        if prefix.endswith('├'):
            prefix = prefix.rpartition('├')[0] + '│'
        parts = z.get_payload()
        i = 0
        while (i < parts.__len__()-1):
            test(parts[i], prefix + '├')
            i += 1
        test(parts[i], prefix + '└')
        # FIXME: show epilogue?
    else:
        print_part(z, prefix+'─╴')

test(email.message_from_file(sys.stdin), '└')

debug log:

solving ab2a6673 ...
found ab2a6673 in https://yhetil.org/notmuch/20180612020000.19607-1-jrollins@finestructure.net/
found afa0590e in https://yhetil.org/notmuch/20180611232244.27207-1-dkg@fifthhorseman.net/
found 34d12930 in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100755 34d12930b4c986b82252689b567517b729c6155b	devel/printmimestructure

applying [1/2] https://yhetil.org/notmuch/20180611232244.27207-1-dkg@fifthhorseman.net/
diff --git a/devel/printmimestructure b/devel/printmimestructure
index 34d12930..afa0590e 100755


applying [2/2] https://yhetil.org/notmuch/20180612020000.19607-1-jrollins@finestructure.net/
diff --git a/devel/printmimestructure b/devel/printmimestructure
index afa0590e..ab2a6673 100755

Checking patch devel/printmimestructure...
Applied patch devel/printmimestructure cleanly.
Checking patch devel/printmimestructure...
Applied patch devel/printmimestructure cleanly.

index at:
100755 ab2a667379186e2458e923ec221fce85431d36e5	devel/printmimestructure

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).