blob: 6c7200f98af4362476ddd974d8247d514cea0bdf [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.iterators`: Iterators
2---------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: email.iterators
5 :synopsis: Iterate over a message object tree.
6
7
8Iterating over a message object tree is fairly easy with the
9:meth:`Message.walk` method. The :mod:`email.iterators` module provides some
10useful higher level iterations over message object trees.
11
12
Georg Brandl3f076d82009-05-17 11:28:33 +000013.. function:: body_line_iterator(msg, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +000014
15 This iterates over all the payloads in all the subparts of *msg*, returning the
16 string payloads line-by-line. It skips over all the subpart headers, and it
17 skips over any subpart with a payload that isn't a Python string. This is
18 somewhat equivalent to reading the flat text representation of the message from
19 a file using :meth:`readline`, skipping over all the intervening headers.
20
21 Optional *decode* is passed through to :meth:`Message.get_payload`.
22
23
Georg Brandl3f076d82009-05-17 11:28:33 +000024.. function:: typed_subpart_iterator(msg, maintype='text', subtype=None)
Georg Brandl116aa622007-08-15 14:28:22 +000025
26 This iterates over all the subparts of *msg*, returning only those subparts that
27 match the MIME type specified by *maintype* and *subtype*.
28
29 Note that *subtype* is optional; if omitted, then subpart MIME type matching is
30 done only with the main type. *maintype* is optional too; it defaults to
31 :mimetype:`text`.
32
33 Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
34 MIME type of :mimetype:`text/\*`.
35
R David Murrayfdfb0052013-07-29 15:49:58 -040036
Georg Brandl116aa622007-08-15 14:28:22 +000037The following function has been added as a useful debugging tool. It should
38*not* be considered part of the supported public interface for the package.
39
Georg Brandl3f076d82009-05-17 11:28:33 +000040.. function:: _structure(msg, fp=None, level=0, include_default=False)
Georg Brandl116aa622007-08-15 14:28:22 +000041
42 Prints an indented representation of the content types of the message object
R David Murrayfdfb0052013-07-29 15:49:58 -040043 structure. For example:
44
45 .. testsetup::
46
47 >>> import email
48 >>> from email.iterators import _structure
49 >>> somefile = open('Lib/test/test_email/data/msg_02.txt')
50
51 .. doctest::
Georg Brandl116aa622007-08-15 14:28:22 +000052
53 >>> msg = email.message_from_file(somefile)
54 >>> _structure(msg)
55 multipart/mixed
56 text/plain
57 text/plain
58 multipart/digest
59 message/rfc822
60 text/plain
61 message/rfc822
62 text/plain
63 message/rfc822
64 text/plain
65 message/rfc822
66 text/plain
67 message/rfc822
68 text/plain
69 text/plain
70
R David Murrayfdfb0052013-07-29 15:49:58 -040071 .. testcleanup::
72
73 >>> somefile.close()
74
Georg Brandl6911e3c2007-09-04 07:15:32 +000075 Optional *fp* is a file-like object to print the output to. It must be
76 suitable for Python's :func:`print` function. *level* is used internally.
Georg Brandl3f076d82009-05-17 11:28:33 +000077 *include_default*, if true, prints the default type as well.