blob: d1f1797e39ff6aec49f586abd7742a0153095297 [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
36The following function has been added as a useful debugging tool. It should
37*not* be considered part of the supported public interface for the package.
38
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
43 structure. For example::
44
45 >>> msg = email.message_from_file(somefile)
46 >>> _structure(msg)
47 multipart/mixed
48 text/plain
49 text/plain
50 multipart/digest
51 message/rfc822
52 text/plain
53 message/rfc822
54 text/plain
55 message/rfc822
56 text/plain
57 message/rfc822
58 text/plain
59 message/rfc822
60 text/plain
61 text/plain
62
Georg Brandl6911e3c2007-09-04 07:15:32 +000063 Optional *fp* is a file-like object to print the output to. It must be
64 suitable for Python's :func:`print` function. *level* is used internally.
Georg Brandl3f076d82009-05-17 11:28:33 +000065 *include_default*, if true, prints the default type as well.