blob: d53ab33b8904a7a1584ea7ff175ea45aa2ce268d [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/email/iterators.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
11Iterating over a message object tree is fairly easy with the
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030012:meth:`Message.walk <email.message.Message.walk>` method. The
13:mod:`email.iterators` module provides some useful higher level iterations over
14message object trees.
Georg Brandl116aa622007-08-15 14:28:22 +000015
16
Georg Brandl3f076d82009-05-17 11:28:33 +000017.. function:: body_line_iterator(msg, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +000018
19 This iterates over all the payloads in all the subparts of *msg*, returning the
20 string payloads line-by-line. It skips over all the subpart headers, and it
21 skips over any subpart with a payload that isn't a Python string. This is
22 somewhat equivalent to reading the flat text representation of the message from
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030023 a file using :meth:`~io.TextIOBase.readline`, skipping over all the
24 intervening headers.
Georg Brandl116aa622007-08-15 14:28:22 +000025
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030026 Optional *decode* is passed through to :meth:`Message.get_payload
27 <email.message.Message.get_payload>`.
Georg Brandl116aa622007-08-15 14:28:22 +000028
29
Georg Brandl3f076d82009-05-17 11:28:33 +000030.. function:: typed_subpart_iterator(msg, maintype='text', subtype=None)
Georg Brandl116aa622007-08-15 14:28:22 +000031
32 This iterates over all the subparts of *msg*, returning only those subparts that
33 match the MIME type specified by *maintype* and *subtype*.
34
35 Note that *subtype* is optional; if omitted, then subpart MIME type matching is
36 done only with the main type. *maintype* is optional too; it defaults to
37 :mimetype:`text`.
38
39 Thus, by default :func:`typed_subpart_iterator` returns each subpart that has a
40 MIME type of :mimetype:`text/\*`.
41
R David Murrayfdfb0052013-07-29 15:49:58 -040042
Georg Brandl116aa622007-08-15 14:28:22 +000043The following function has been added as a useful debugging tool. It should
44*not* be considered part of the supported public interface for the package.
45
Georg Brandl3f076d82009-05-17 11:28:33 +000046.. function:: _structure(msg, fp=None, level=0, include_default=False)
Georg Brandl116aa622007-08-15 14:28:22 +000047
48 Prints an indented representation of the content types of the message object
R David Murrayfdfb0052013-07-29 15:49:58 -040049 structure. For example:
50
51 .. testsetup::
52
Zachary Ware640b1ca2016-08-10 00:39:41 -050053 import email
54 from email.iterators import _structure
55 somefile = open('../Lib/test/test_email/data/msg_02.txt')
R David Murrayfdfb0052013-07-29 15:49:58 -040056
57 .. doctest::
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 >>> msg = email.message_from_file(somefile)
60 >>> _structure(msg)
61 multipart/mixed
62 text/plain
63 text/plain
64 multipart/digest
65 message/rfc822
66 text/plain
67 message/rfc822
68 text/plain
69 message/rfc822
70 text/plain
71 message/rfc822
72 text/plain
73 message/rfc822
74 text/plain
75 text/plain
76
Zachary Ware640b1ca2016-08-10 00:39:41 -050077 .. testcleanup::
R David Murrayfdfb0052013-07-29 15:49:58 -040078
Zachary Ware640b1ca2016-08-10 00:39:41 -050079 somefile.close()
R David Murrayfdfb0052013-07-29 15:49:58 -040080
Georg Brandl6911e3c2007-09-04 07:15:32 +000081 Optional *fp* is a file-like object to print the output to. It must be
82 suitable for Python's :func:`print` function. *level* is used internally.
Georg Brandl3f076d82009-05-17 11:28:33 +000083 *include_default*, if true, prints the default type as well.