Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001-2004 Python Software Foundation |
| 2 | # Author: Barry Warsaw <barry@python.org> |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 3 | |
| 4 | """Various types of useful iterators and generators. |
| 5 | """ |
| 6 | |
Barry Warsaw | d438574 | 2002-07-19 22:21:47 +0000 | [diff] [blame] | 7 | import sys |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 8 | from cStringIO import StringIO |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 12 | # This function will become a method of the Message class |
| 13 | def walk(self): |
| 14 | """Walk over the message tree, yielding each subpart. |
| 15 | |
| 16 | The walk is performed in depth-first order. This method is a |
| 17 | generator. |
| 18 | """ |
| 19 | yield self |
| 20 | if self.is_multipart(): |
| 21 | for subpart in self.get_payload(): |
| 22 | for subsubpart in subpart.walk(): |
| 23 | yield subsubpart |
| 24 | |
| 25 | |
| 26 | |
| 27 | # These two functions are imported into the Iterators.py interface module. |
| 28 | # The Python 2.2 version uses generators for efficiency. |
| 29 | def body_line_iterator(msg, decode=False): |
| 30 | """Iterate over the parts, returning string payloads line-by-line. |
| 31 | |
| 32 | Optional decode (default False) is passed through to .get_payload(). |
| 33 | """ |
| 34 | for subpart in msg.walk(): |
| 35 | payload = subpart.get_payload(decode=decode) |
| 36 | if isinstance(payload, basestring): |
| 37 | for line in StringIO(payload): |
| 38 | yield line |
| 39 | |
| 40 | |
| 41 | def typed_subpart_iterator(msg, maintype='text', subtype=None): |
| 42 | """Iterate over the subparts with a given MIME type. |
| 43 | |
| 44 | Use `maintype' as the main MIME type to match against; this defaults to |
| 45 | "text". Optional `subtype' is the MIME subtype to match against; if |
| 46 | omitted, only the main type is matched. |
| 47 | """ |
| 48 | for subpart in msg.walk(): |
| 49 | if subpart.get_content_maintype() == maintype: |
| 50 | if subtype is None or subpart.get_content_subtype() == subtype: |
| 51 | yield subpart |
| 52 | |
| 53 | |
| 54 | |
| 55 | def _structure(msg, fp=None, level=0, include_default=False): |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 56 | """A handy debugging aid""" |
Barry Warsaw | d438574 | 2002-07-19 22:21:47 +0000 | [diff] [blame] | 57 | if fp is None: |
| 58 | fp = sys.stdout |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 59 | tab = ' ' * (level * 4) |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 60 | print >> fp, tab + msg.get_content_type(), |
| 61 | if include_default: |
Barry Warsaw | d49f1d6 | 2004-05-13 20:14:20 +0000 | [diff] [blame] | 62 | print >> fp, '[%s]' % msg.get_default_type() |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 63 | else: |
Barry Warsaw | d49f1d6 | 2004-05-13 20:14:20 +0000 | [diff] [blame] | 64 | print >> fp |
Barry Warsaw | 8fa06b5 | 2002-07-09 02:39:07 +0000 | [diff] [blame] | 65 | if msg.is_multipart(): |
| 66 | for subpart in msg.get_payload(): |
Barry Warsaw | 4c3e33a | 2004-05-09 03:42:37 +0000 | [diff] [blame] | 67 | _structure(subpart, fp, level+1, include_default) |