Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001 Python Software Foundation |
| 2 | # Author: barry@zope.com (Barry Warsaw) |
| 3 | |
| 4 | """Various types of useful iterators and generators. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import generators |
| 8 | from cStringIO import StringIO |
| 9 | from types import StringType |
| 10 | |
| 11 | |
| 12 | |
| 13 | def body_line_iterator(msg): |
Barry Warsaw | 57758e3 | 2001-09-26 05:35:47 +0000 | [diff] [blame] | 14 | """Iterate over the parts, returning string payloads line-by-line.""" |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 15 | for subpart in msg.walk(): |
| 16 | payload = subpart.get_payload() |
| 17 | if type(payload) is StringType: |
| 18 | for line in StringIO(payload): |
| 19 | yield line |
| 20 | |
| 21 | |
| 22 | |
Barry Warsaw | 57758e3 | 2001-09-26 05:35:47 +0000 | [diff] [blame] | 23 | def typed_subpart_iterator(msg, maintype='text', subtype=None): |
| 24 | """Iterate over the subparts with a given MIME type. |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 25 | |
Barry Warsaw | 57758e3 | 2001-09-26 05:35:47 +0000 | [diff] [blame] | 26 | Use `maintype' as the main MIME type to match against; this defaults to |
| 27 | "text". Optional `subtype' is the MIME subtype to match against; if |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 28 | omitted, only the main type is matched. |
| 29 | """ |
| 30 | for subpart in msg.walk(): |
Barry Warsaw | 57758e3 | 2001-09-26 05:35:47 +0000 | [diff] [blame] | 31 | if subpart.get_main_type() == maintype: |
| 32 | if subtype is None or subpart.get_subtype() == subtype: |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 33 | yield subpart |