R David Murray | b42b6eb | 2012-05-27 17:13:54 -0400 | [diff] [blame] | 1 | :mod:`email.iterators`: Iterators |
| 2 | --------------------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: email.iterators |
| 5 | :synopsis: Iterate over a message object tree. |
| 6 | |
| 7 | |
| 8 | Iterating over a message object tree is fairly easy with the |
Serhiy Storchaka | f65d454 | 2013-08-19 10:03:25 +0300 | [diff] [blame] | 9 | :meth:`Message.walk <email.message.Message.walk>` method. The |
| 10 | :mod:`email.iterators` module provides some useful higher level iterations over |
| 11 | message object trees. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 12 | |
| 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 Storchaka | f65d454 | 2013-08-19 10:03:25 +0300 | [diff] [blame] | 20 | a file using :meth:`~io.TextIOBase.readline`, skipping over all the |
| 21 | intervening headers. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 22 | |
Serhiy Storchaka | f65d454 | 2013-08-19 10:03:25 +0300 | [diff] [blame] | 23 | Optional *decode* is passed through to :meth:`Message.get_payload |
| 24 | <email.message.Message.get_payload>`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 25 | |
| 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 | |
| 39 | The 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 | |