blob: 6621d51dcf7853324ddb00bb0b9fb6fdb76b6cc3 [file] [log] [blame]
R David Murrayb42b6eb2012-05-27 17:13:54 -04001:mod:`email.iterators`: Iterators
2---------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Serhiy Storchakaf65d4542013-08-19 10:03:25 +03009:meth:`Message.walk <email.message.Message.walk>` method. The
10:mod:`email.iterators` module provides some useful higher level iterations over
11message object trees.
Georg Brandl8ec7f652007-08-15 14:28:01 +000012
13
14.. function:: body_line_iterator(msg[, decode])
15
16 This iterates over all the payloads in all the subparts of *msg*, returning the
17 string payloads line-by-line. It skips over all the subpart headers, and it
18 skips over any subpart with a payload that isn't a Python string. This is
19 somewhat equivalent to reading the flat text representation of the message from
Serhiy Storchakaf65d4542013-08-19 10:03:25 +030020 a file using :meth:`~io.TextIOBase.readline`, skipping over all the
21 intervening headers.
Georg Brandl8ec7f652007-08-15 14:28:01 +000022
Serhiy Storchakaf65d4542013-08-19 10:03:25 +030023 Optional *decode* is passed through to :meth:`Message.get_payload
24 <email.message.Message.get_payload>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
26
27.. function:: typed_subpart_iterator(msg[, maintype[, subtype]])
28
29 This iterates over all the subparts of *msg*, returning only those subparts that
30 match the MIME type specified by *maintype* and *subtype*.
31
32 Note that *subtype* is optional; if omitted, then subpart MIME type matching is
33 done only with the main type. *maintype* is optional too; it defaults to
34 :mimetype:`text`.
35
36 Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
37 MIME type of :mimetype:`text/\*`.
38
39The following function has been added as a useful debugging tool. It should
40*not* be considered part of the supported public interface for the package.
41
42
43.. function:: _structure(msg[, fp[, level]])
44
45 Prints an indented representation of the content types of the message object
46 structure. For example::
47
48 >>> msg = email.message_from_file(somefile)
49 >>> _structure(msg)
50 multipart/mixed
51 text/plain
52 text/plain
53 multipart/digest
54 message/rfc822
55 text/plain
56 message/rfc822
57 text/plain
58 message/rfc822
59 text/plain
60 message/rfc822
61 text/plain
62 message/rfc822
63 text/plain
64 text/plain
65
66 Optional *fp* is a file-like object to print the output to. It must be suitable
67 for Python's extended print statement. *level* is used internally.
68