Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 1 | # Copyright (C) 2002 Python Software Foundation |
| 2 | # Author: barry@zope.com |
| 3 | |
| 4 | """Module containing compatibility functions for Python 2.1. |
| 5 | """ |
| 6 | |
| 7 | from cStringIO import StringIO |
| 8 | from types import StringType, UnicodeType |
| 9 | |
| 10 | |
| 11 | |
| 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 | parts = [] |
| 20 | parts.append(self) |
| 21 | if self.is_multipart(): |
| 22 | for subpart in self.get_payload(): |
| 23 | parts.extend(subpart.walk()) |
| 24 | return parts |
| 25 | |
| 26 | |
Barry Warsaw | ff49279 | 2002-06-02 18:59:06 +0000 | [diff] [blame] | 27 | # Python 2.2 spells floor division // |
| 28 | def _floordiv(i, j): |
| 29 | """Do a floor division, i/j.""" |
| 30 | return i / j |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 31 | |
| 32 | |
Barry Warsaw | 356afac | 2002-09-10 16:09:06 +0000 | [diff] [blame] | 33 | def _isstring(obj): |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 34 | return isinstance(obj, StringType) or isinstance(obj, UnicodeType) |
Barry Warsaw | 356afac | 2002-09-10 16:09:06 +0000 | [diff] [blame] | 35 | |
| 36 | |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 37 | |
| 38 | # These two functions are imported into the Iterators.py interface module. |
| 39 | # The Python 2.2 version uses generators for efficiency. |
Barry Warsaw | 12dc230 | 2003-03-11 04:41:35 +0000 | [diff] [blame^] | 40 | def body_line_iterator(msg, decode=False): |
| 41 | """Iterate over the parts, returning string payloads line-by-line. |
| 42 | |
| 43 | Optional decode (default False) is passed through to .get_payload(). |
| 44 | """ |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 45 | lines = [] |
| 46 | for subpart in msg.walk(): |
Barry Warsaw | 12dc230 | 2003-03-11 04:41:35 +0000 | [diff] [blame^] | 47 | payload = subpart.get_payload(decode=decode) |
Barry Warsaw | 356afac | 2002-09-10 16:09:06 +0000 | [diff] [blame] | 48 | if _isstring(payload): |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 49 | for line in StringIO(payload).readlines(): |
| 50 | lines.append(line) |
| 51 | return lines |
| 52 | |
| 53 | |
| 54 | def typed_subpart_iterator(msg, maintype='text', subtype=None): |
| 55 | """Iterate over the subparts with a given MIME type. |
| 56 | |
| 57 | Use `maintype' as the main MIME type to match against; this defaults to |
| 58 | "text". Optional `subtype' is the MIME subtype to match against; if |
| 59 | omitted, only the main type is matched. |
| 60 | """ |
| 61 | parts = [] |
| 62 | for subpart in msg.walk(): |
| 63 | if subpart.get_main_type('text') == maintype: |
| 64 | if subtype is None or subpart.get_subtype('plain') == subtype: |
| 65 | parts.append(subpart) |
| 66 | return parts |