blob: f92f460249822d9b5612efdcc8ce0c741177d461 [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
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +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 Brandl116aa622007-08-15 14:28:22 +000012
13
Georg Brandl3f076d82009-05-17 11:28:33 +000014.. function:: body_line_iterator(msg, decode=False)
Georg Brandl116aa622007-08-15 14:28:22 +000015
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 Storchakae0f0cf42013-08-19 09:59:18 +030020 a file using :meth:`~io.TextIOBase.readline`, skipping over all the
21 intervening headers.
Georg Brandl116aa622007-08-15 14:28:22 +000022
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030023 Optional *decode* is passed through to :meth:`Message.get_payload
24 <email.message.Message.get_payload>`.
Georg Brandl116aa622007-08-15 14:28:22 +000025
26
Georg Brandl3f076d82009-05-17 11:28:33 +000027.. function:: typed_subpart_iterator(msg, maintype='text', subtype=None)
Georg Brandl116aa622007-08-15 14:28:22 +000028
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
R David Murrayfdfb0052013-07-29 15:49:58 -040039
Georg Brandl116aa622007-08-15 14:28:22 +000040The following function has been added as a useful debugging tool. It should
41*not* be considered part of the supported public interface for the package.
42
Georg Brandl3f076d82009-05-17 11:28:33 +000043.. function:: _structure(msg, fp=None, level=0, include_default=False)
Georg Brandl116aa622007-08-15 14:28:22 +000044
45 Prints an indented representation of the content types of the message object
R David Murrayfdfb0052013-07-29 15:49:58 -040046 structure. For example:
47
48 .. testsetup::
49
50 >>> import email
51 >>> from email.iterators import _structure
52 >>> somefile = open('Lib/test/test_email/data/msg_02.txt')
53
54 .. doctest::
Georg Brandl116aa622007-08-15 14:28:22 +000055
56 >>> msg = email.message_from_file(somefile)
57 >>> _structure(msg)
58 multipart/mixed
59 text/plain
60 text/plain
61 multipart/digest
62 message/rfc822
63 text/plain
64 message/rfc822
65 text/plain
66 message/rfc822
67 text/plain
68 message/rfc822
69 text/plain
70 message/rfc822
71 text/plain
72 text/plain
73
R David Murray11bfd322013-07-30 14:42:40 -040074 .. testsetup::
R David Murrayfdfb0052013-07-29 15:49:58 -040075
76 >>> somefile.close()
77
Georg Brandl6911e3c2007-09-04 07:15:32 +000078 Optional *fp* is a file-like object to print the output to. It must be
79 suitable for Python's :func:`print` function. *level* is used internally.
Georg Brandl3f076d82009-05-17 11:28:33 +000080 *include_default*, if true, prints the default type as well.